-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
339 lines (282 loc) · 11.4 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using TuentiDownloader.Properties;
using HtmlDocument = HtmlAgilityPack.HtmlDocument;
namespace TuentiDownloader
{
public partial class Main : Form
{
private bool _cancel;
public Main()
{
InitializeComponent();
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
savePath.Text = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Tuenti");
numericUpDown1_ValueChanged(null, null);
}
private void button1_Click(object sender, EventArgs e)
{
if (bToggleSettings.Enabled)
{
_cancel = false;
if (groupBox1.Visible)
bToggleSettings.PerformClick();
bToggleSettings.Enabled = false;
bStart.Text = "Parar";
Directory.CreateDirectory(savePath.Text);
Tuenti.SavePath = savePath.Text;
var downloader = new HtmlDownloader {ResourcePath = Path.Combine(savePath.Text, "Recursos")};
//Descargar fotos
if (!_cancel && downloadPhotos.Checked)
{
_downloadPhotos(downloader);
}
//Descargar perfil
if (!_cancel && downloadProfile.Checked)
{
_downloadProfile(downloader);
}
//Descargar mensajes
if (!_cancel && downloadMessages.Checked)
{
_downloadMessages(downloader);
}
if (!_cancel)
{
System.Media.SystemSounds.Exclamation.Play();
MessageBox.Show("TuentiDownloader ha terminado de descargar contenidos", "Fin de la descarga",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else{
_cancel = true;
}
bStart.Text = "Comenzar";
bToggleSettings.Enabled = true;
}
private void _downloadPhotos(HtmlDownloader downloader)
{
int photo = 0;
int processed = 0;
bool morePages;
do
{
int page = 0;
//Recorrer comentarios
bool moreComments;
do
{
if (_cancel)
return;
//Calcular número de página
var regex = new Regex("(\\d+) de \\d+", RegexOptions.IgnoreCase);
Match match = regex.Match(webBrowser.Document.Body.InnerText);
if (match.Success)
{
photo = int.Parse(match.Groups[1].Value) - 1;
}
//Descargar página
_process(doc => Tuenti.FixPhotoPage(doc, photo, page), Tuenti.GetPhotoPath(photo, page), downloader);
//Recorrer comentarios
moreComments = _moveNext("photo_wall");
page++;
} while (moreComments);
//Recorrer páginas
morePages = _moveNext("photo_pager");
photo++;
processed++;
if (processed % 750 == 0)
{
//Recargar la página para evitar cuelgues y fugas de memoria
webBrowser.Refresh();
webBrowser.WaitLoad(true);
webBrowser.WaitLoad(true);
webBrowser.WaitLoad(true);
webBrowser.WaitLoad(true);
}
} while (morePages);
}
private void _downloadMessages(HtmlDownloader downloader)
{
//Navegar a los mensajes
webBrowser.Navigate("http://www.tuenti.com/#m=Message");
webBrowser.WaitLoad(true);
//Inyectar jQuery
webBrowser.InjectJS(Resources.jQueryLoader);
int page = 0;
bool morePages;
do
{
//Calcular número de página
var regex = new Regex("(\\d+) de \\d+", RegexOptions.IgnoreCase);
Match match = regex.Match(webBrowser.Document.Body.InnerText);
if (match.Success)
{
page = int.Parse(match.Groups[1].Value) - 1;
}
//Obtener ids de los mensajes
var messageIds = new List<string>();
foreach (HtmlElement node in webBrowser.Document.All)
{
string id = node.GetAttribute("threadid");
if (!string.IsNullOrEmpty(id) && !messageIds.Contains(id) && Regex.IsMatch(id, "^\\d+$"))
messageIds.Add(id);
}
//Parsear HTML
var document = new HtmlDocument {OptionDefaultStreamEncoding = Encoding.UTF8};
document.LoadHtml(webBrowser.Document.Body.Parent.OuterHtml);
//Corregir página
Tuenti.FixMessagePage(document, page, -1);
//Guardar página
downloader.Download(document, Tuenti.GetMessagePath(page));
//Recorrer mensajes
int message = 1;
foreach (string tid in messageIds)
{
if (_cancel)
return;
//Abrir mensaje
webBrowser.Document.InvokeScript("eval",
new object[] {"jQuery('div[threadid=" + tid + "]').click()"});
webBrowser.WaitLoad();
//Cargar mensajes antiguos
bool foundMoreMessages;
int i = 0;
do
{
foundMoreMessages = false;
foreach (HtmlElement element in webBrowser.GetElementsByClassName("viewMore"))
{
foreach (HtmlElement link in element.GetElementsByTagName("a"))
{
foundMoreMessages = true;
link.InvokeMember("click");
webBrowser.WaitLoad();
}
}
i++;
} while (foundMoreMessages && i<30);
//Descargar página
_process(doc => Tuenti.FixMessagePage(doc, page, message), Tuenti.GetMessagePath(page, message), downloader);
message++;
//Cerrar mensaje
webBrowser.Document.InvokeScript("eval",
new object[]
{"jQuery('.author[threadid=" + tid + "]:visible').click()"});
Application.DoEvents();
webBrowser.WaitLoad();
}
webBrowser.WaitLoad(); //Necesario para que el DOM se actualice
//Recorrer páginas
morePages = _moveNext("pager_overlay");
page++;
webBrowser.WaitLoad(); //Necesario para que el DOM se actualice
} while (morePages);
}
private void _downloadProfile(HtmlDownloader downloader)
{
//Ir al perfil
webBrowser.Navigate("http://www.tuenti.com/#m=Profile&func=index");
webBrowser.WaitLoad(true);
int page = 0;
bool morePages;
do
{
//Descargar página
_process(document => Tuenti.FixProfilePage(document, page), Tuenti.GetProfilePath(page), downloader);
//Recorrer páginas
morePages = _moveNext("wall_body");
page++;
} while (morePages);
}
private void _process(Action<HtmlDocument> fixer, string savePath, HtmlDownloader downloader)
{
try
{
//Parsear HTML
var document = new HtmlDocument { OptionDefaultStreamEncoding = Encoding.UTF8 };
document.LoadHtml(webBrowser.Document.Body.Parent.OuterHtml);
//Corregir página
fixer(document);
//Guardar página
downloader.Download(document, savePath);
//Liberar memoria
document = null;
GC.Collect();
}
catch
{
File.WriteAllText(savePath,"Se produjo un error al recuperar la URL "+webBrowser.Url);
}
}
private bool _moveNext(string id)
{
Application.DoEvents();
HtmlElement elm = webBrowser.Document.GetElementById(id);
if (elm != null)
{
foreach (HtmlElement link in elm.GetElementsByTagName("a"))
{
if (link.GetAttribute("className") == "next" ||
link.GetAttribute("class") == "next")
{
link.InvokeMember("click");
//Esperar que se cargue
webBrowser.WaitLoad();
return true;
}
}
}
return false;
}
private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
savePath.Text = folderBrowserDialog1.SelectedPath;
}
private void Main_FormClosed(object sender, FormClosedEventArgs e)
{
Environment.Exit(0);
}
private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
toolStripStatusLabel1.Text = "Cargando " + e.Url;
}
private void webBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
toolStripProgressBar1.Maximum = (int) e.MaximumProgress;
toolStripProgressBar1.Value = (int) e.CurrentProgress;
}
private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
toolStripStatusLabel1.Text = webBrowser.Url.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
if (groupBox1.Visible)
{
groupBox1.Visible = false;
bToggleSettings.Text = "Mostrar opciones";
}
else
{
groupBox1.Visible = true;
bToggleSettings.Text = "Ocultar opciones";
}
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
webBrowser.Timeout = (int) numericUpDown1.Value;
}
private void toolStripStatusLabel2_Click(object sender, EventArgs e)
{
Process.Start("https://twitter.com/intent/tweet?text=" + Uri.EscapeDataString("Descargando todas mis fotos, mensajes y comentarios de Tuenti con #TuentiDownloader de @javiermarinros") + "&related=javiermarinros");
}
}
}