-
Notifications
You must be signed in to change notification settings - Fork 4
/
FormMain.cs
474 lines (397 loc) · 14.8 KB
/
FormMain.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using CefSharp;
using CefSharp.WinForms;
using System.Text.RegularExpressions;
namespace QZAlbumTool
{
public partial class FormMain : Form
{
private ChromiumWebBrowser web = null;
public FormMain()
{
InitializeComponent();
}
private FolderBrowserDialog m_dlg = new FolderBrowserDialog();
private void FormMain_Load(object sender, EventArgs e)
{
//web.Url = new Uri("");
InitBrowser();
}
private void InitBrowser()
{
Cef.Initialize(new CefSettings());
web = new ChromiumWebBrowser("https://qzone.qq.com");
tabPageLogin.Controls.Add(web);
web.Dock = DockStyle.Fill;
}
private string GetPSkey()
{
string p_skey = GetCookieValue("p_skey");
return p_skey;
}
private string GetQQNumber()
{
string qqnumber = int.Parse(GetCookieValue("uin").Substring(1)).ToString();
return qqnumber;
}
private string GetCookieValue(string key)
{
string val = "";
try
{
var cookies = web.GetCookieManager().VisitAllCookiesAsync().Result.Where(p => p.Name == key).ToList();
if (cookies.Count > 0)
{
val = cookies[0].Value;
}
}
catch { }
return val;
}
private void CopyCookie(ChromiumWebBrowser src, WebClient dst)
{
var cookies = src.GetCookieManager().VisitAllCookiesAsync().Result;
string strcookies = "";
foreach (var cookie in cookies)
{
strcookies += cookie.Name + "=" + cookie.Value + ";";
}
dst.Headers.Add("Cookie", strcookies);
}
private int GetG_TK(string p_skey)
{
string e = p_skey;
var t = 5381;
for (int n = 0, r = e.Length; n < r; ++n)
{
t += (t << 5) + e[n];
}
return t & 0x7FFFFFFF;
}
private List<JObject> GetAlbumList(string p_skey,string qqnumber)
{
//检查登录 跳转到相册,失败就没登陆
List<JObject> listJAlbumList = null;
//string g_tk = WebBeginInvoke("return QZFL.pluginsDefine.getACSRFToken();");
string g_tk = GetG_TK(p_skey).ToString();
string url = "https://h5.qzone.qq.com/proxy/domain/photo.qzone.qq.com/fcgi-bin/fcg_list_album_v3?" +
"g_tk=" + g_tk +
"&callback=shine0_Callback" +
"&t=628680498" +
"&hostUin=" + qqnumber +
"&uin="+ qqnumber +
"&appid=4" +
"&inCharset=utf-8" +
"&outCharset=utf-8" +
"&source=qzone" +
"&plat=qzone" +
"&format=json" +
"¬ice=0" +
"&filter=1" +
"&handset=4" +
"&pageNumModeSort=40" +
"&pageNumModeClass=15" +
"&needUserInfo=1" +
"&idcNum=4" +
"&callbackFun=shine0" +
//"&mode=2" +
"&_=" + DateTime.UtcNow.ToFileTimeUtc().ToString();
WebClient wc = new WebClient() { Encoding = Encoding.UTF8 };
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
CopyCookie(web,wc);
string json = string.Empty;
try { json = wc.DownloadString(url); } catch { Thread.Sleep(1000); }
if(json == null) try { json = wc.DownloadString(url); } catch { }
JObject jobj = JObject.Parse(json);
try
{
do
{
if ((int)jobj["code"] != 0 || (int)jobj["subcode"] != 0)
break;
listJAlbumList = new List<JObject>();
var jalbumListModeClass = jobj["data"]["albumListModeClass"];
if (jalbumListModeClass == null)
break;
foreach (var jelem in jalbumListModeClass)
{
foreach (var jelem2 in jelem["albumList"])
{
listJAlbumList.Add((JObject)jelem2);
}
}
Debug.WriteLine("完成");
} while (false);
}
catch { listJAlbumList = null; }
//获取相册列表 相册名称+封面缩略图
//获取相册内容列表 照片名称+缩略图+对应的原图
//下载原图
return listJAlbumList;
}
/// <summary>
/// 获取图片
/// </summary>
/// <param name="pre">url</param>
/// <param name="type">a、m、b</param>
/// <returns></returns>
private Image GetImage(string url,bool save = false)
{
Image img = null;
try
{
string filetype = "";
byte[] data = GetFile(url, out filetype,save);
using (MemoryStream mStream = new MemoryStream(data))
{
img = Image.FromStream(mStream);
}
}
catch { }
return img;
}
private byte[] GetFile(string url,out string filetype,bool save = false)
{
byte[] data = null;
filetype = "";
try
{
WebClient wc = new WebClient();
CopyCookie(web,wc);
if (save)
url += "&d=1";
data = wc.DownloadData(url);
try
{
filetype = wc.ResponseHeaders["Content-Disposition"].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)[1].Split('=')[1].Split('.')[1];
}
catch { }
}
catch
{
}
return data;
}
private void TabControl1_Selected(object sender, TabControlEventArgs e)
{
if(e.TabPageIndex == 1)
{
string p_skey = GetPSkey();
string qq = GetQQNumber();
if(string.IsNullOrWhiteSpace(p_skey) || string.IsNullOrWhiteSpace(qq))
{
MessageBox.Show("未登录,请重新登录");
tabControl1.TabIndex = 0;
}
else
{
var list = GetAlbumList(p_skey, qq);
if(list == null)
{
MessageBox.Show("获取相册列表失败");
}else if(list.Count == 0)
{
MessageBox.Show("相册为空");
}
else
{
MessageBox.Show("显示中,共" + list.Count + "个相册,载入缓慢请耐心等待");
ShowAlbumList(list);
}
}
}
}
private void ShowAlbumList(List<JObject> list)
{
listView1.Items.Clear();
listView1.LargeImageList = new ImageList();
listView1.LargeImageList.ColorDepth = ColorDepth.Depth32Bit;
listView1.LargeImageList.ImageSize = new Size(146, 110);
listView1.View = View.LargeIcon;
int i = 0;
foreach (dynamic jitem in list)
{
string name = jitem.name;
string url = ((string)jitem.pre).Replace("/a/", "/m/");
Image img = GetImage(url);
//Debug.WriteLine("添加图片" + i + "| url:" + (string)jitem.pre);
if (img == null)
{
Thread.Sleep(500);
img = GetImage(url);
}
if (img == null)
{
Debug.WriteLine("添加图片" + i + "失败(" + (string)jitem.name + ")");
}
else
{
ListViewItem item = new ListViewItem()
{
Tag = jitem,
Text = name,
ImageIndex = i++
};
listView1.LargeImageList.Images.Add(img);
listView1.Items.Add(item);
}
}
}
private void BtnSelectAll_Click(object sender, EventArgs e)
{
foreach(ListViewItem item in listView1.Items)
{
item.Selected = true;
}
}
private void BtnSelectNot_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
item.Selected = !item.Selected;
}
}
private List<JObject> GetPhotoList(string p_skey, string qqnumber,string id)
{
string g_tk = GetG_TK(p_skey).ToString();
string url = "https://h5.qzone.qq.com/proxy/domain/photo.qzone.qq.com/fcgi-bin/cgi_list_photo?" +
"g_tk=" + g_tk +
"&callback=shine0_Callback" +
"&t=160252680" +
"&mode=0" +
"&idcNum=4" +
"&hostUin=" + qqnumber +
"&topicId=" + id +
"&noTopic=0" +
"&uin="+ qqnumber +
"&pageStart=0" +
"&pageNum=9999" +
"&skipCmtCount=0" +
"&singleurl=1" +
"&batchId=" +
"¬ice=0" +
"&appid=4" +
"&inCharset=utf-8" +
"&outCharset=utf-8" +
"&source=qzone" +
"&plat=qzone" +
"&outstyle=json" +
"&format=json" +
"&json_esc=1" +
"&question=" +
"&answer=" +
"&callbackFun=shine0" +
"&_=" + DateTime.UtcNow.ToFileTimeUtc().ToString();
WebClient wc = new WebClient() { Encoding = Encoding.UTF8};
CopyCookie(web, wc);
string resp = wc.DownloadString(url);
List<JObject> list = new List<JObject>();
do
{
if (string.IsNullOrWhiteSpace(resp))
break;
JObject jobj = JObject.Parse(resp);
if ((int)jobj["code"] != 0 || (int)jobj["subcode"] != 0)
break;
JArray photoList = (JArray)jobj["data"]["photoList"];
foreach(JObject jphoto in photoList)
{
list.Add(jphoto);
}
} while (false);
return list;
}
private void BtnExport_Click(object sender, EventArgs e)
{
if (m_dlg.ShowDialog() != DialogResult.OK)
return;
string path_parent = m_dlg.SelectedPath;
string path = "";
string err = "这些图片下载失败:\r\n";
int album_counter = 0;
int photo_counter = 0;
foreach (ListViewItem item in listView1.Items)
{
if (!item.Selected)
continue;
dynamic jitem = item.Tag;
string err2 = "";
var list = GetPhotoList(GetPSkey(), GetQQNumber(), jitem.id.ToString());
if(list == null)
{
MessageBox.Show("打开相册[" + jitem.name + "]失败");
continue;
}
int counter = 0;
path = path_parent;
if (cbDir.Checked)
{
path = path + "\\" + jitem.name;
if(!Directory.Exists(path)) //创建子目录
Directory.CreateDirectory(path);
}
foreach (JObject jphoto in list)
{
string photo_batchid = (string)jphoto["batchId"];
string photo_url = (string)jphoto["raw"] == "" ? (string)jphoto["url"] : (string)jphoto["raw"];
string photo_name = (string)jphoto["name"];
//int photo_witdh = (int)jphoto["width"];
//int photo_height = (int)jphoto["height"];
bool photo_isvideo = (bool)jphoto["is_video"];
if(photo_isvideo)
{
//视频不能下载
err2 += photo_name + "(视频),";
continue;
}
photo_name = photo_name.Replace('\\', '_').Replace('/', '_').Replace(':', '_');
string filename = path + "\\" + counter++ + "_" +photo_name;
filename = filename
.Replace('*', '_')
.Replace('?', '_')
.Replace('<', '_')
.Replace('>', '_')
.Replace('|', '_');
string filetype = "";
byte[] data = GetFile(photo_url, out filetype, true);
if(string.IsNullOrWhiteSpace(filetype))
{
filetype = ".png"; //取类型失败时默认就是png
}
else
{
filetype = "." + filetype;
}
File.WriteAllBytes(filename + filetype, data);
if (cbUseSize.Checked)
{
Image img = Image.FromFile(filename + filetype);
string filename2 = filename + "_" + img.Width + "x" + img.Height + filetype;
img.Dispose();
File.Move(filename + filetype, filename2); //重命名
}
photo_counter++;
}
if (!string.IsNullOrWhiteSpace(err2))
{
err += jitem.name + "[" + err2 + "]\r\n";
}
album_counter++;
}
MessageBox.Show("处理完毕,共" + album_counter + "个相册," + photo_counter + "张相片");
}
}
}