-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
604 lines (574 loc) · 23.8 KB
/
MainForm.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace _163AlbumGet
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
bool AlbumIDInputOnFocus = false;
RootObject rb;
TimeSpan ts;
int idata0, idata1;
string loc = Program.tloc, exp = string.Empty,
ls1, ls2, ls3,
sdata0, sdata1, sdata2, sdataa,
dlerr = string.Empty;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter && AlbumIDInputOnFocus)
{
GET();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void DLAllSong_Click(object sender, EventArgs e)
{
if (rb != null)
{
string ls5 = Conv(
FileSavingSettings.FileNameFilter(Program.afn, "_"),
string.Empty,
string.Empty,
FileSavingSettings.FileNameFilter(rb.rb[0].album.name, "_"),
FileSavingSettings.FileNameFilter(rb.rb[0].album.id.ToString(), "_"),
string.Empty,
string.Empty);
Directory.CreateDirectory(SaveLoc.Text + ls5);
ProcessB.Maximum = rb.rb.Count;
ProcessL.Text = rb.rb.Count.ToString();
string locx;
int DLfailedcount = 0;
IniFiles ini = new IniFiles(SaveLoc.Text + ls5 + @"\下载失败的文件.ini");
for (int i = 0; i < rb.rb.Count; i++)
{
string ls4 = string.Empty;
for (int i2 = 0; i2 < rb.rb[i].artists.Count; i2++)
{
if (i2 != 0)
{
ls4 += ";";
}
ls4 += rb.rb[i].artists[i2].name;
}
string ls6 = Conv(
FileSavingSettings.FileNameFilter(Program.msf, "_"),
FileSavingSettings.FileNameFilter(i.ToString().PadLeft((rb.rb.Count - 1).ToString().Length, '0'), "_"),
FileSavingSettings.FileNameFilter(ls4, "_"),
FileSavingSettings.FileNameFilter(rb.rb[0].album.name, "_"),
FileSavingSettings.FileNameFilter(rb.rb[0].album.id.ToString(), "_"),
FileSavingSettings.FileNameFilter(rb.rb[i].name, "_"),
FileSavingSettings.FileNameFilter(rb.rb[i].id.ToString(), "_"));
locx = FileSavingSettings.PathFilter(SaveLoc.Text + ls5 + @"\" + ls6 + Program.fmt, "_");
if (!DownloadFile("http://music.163.com/song/media/outer/url?id=" + rb.rb[i].id.ToString() + Program.fmt, locx))
{
int iii = i + 1;
Error("下载失败 (" + iii + ")");
ini.IniWriteValue("DownloadFailed", "count", (DLfailedcount + 1).ToString());
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "歌曲编号", iii.ToString());
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "歌曲名称", rb.rb[i].name);
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "歌曲ID", rb.rb[i].id.ToString());
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "源地址", "http://music.163.com/song/media/outer/url?id=" + rb.rb[i].id.ToString());
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "保存路径", locx);
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "错误提示", dlerr);
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "可能原因", "网络问题");
DLfailedcount++;
dlerr = string.Empty;
}
using (FileStream fs = File.OpenRead(locx))
{
if (fs.ReadByte() == '<')
{
fs.Close();
fs.Dispose();
File.Delete(locx);
int iii = i + 1;
Error("下载失败,可能原因:版权限制或地区限制 (" + iii + ")");
ini.IniWriteValue("DownloadFailed", "count", DLfailedcount.ToString());
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "歌曲编号", iii.ToString());
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "歌曲名称", rb.rb[i].name);
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "歌曲ID", rb.rb[i].id.ToString());
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "源地址", "http://music.163.com/song/media/outer/url?id=" + rb.rb[i].id.ToString());
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "保存路径", locx);
ini.IniWriteValue("Error" + DLfailedcount.ToString(), "可能原因", "版权限制或地区限制");
DLfailedcount++;
}
}
ProcessB.Value = i + 1;
}
if (DLfailedcount != 0)
{
ini.IniWriteValue("DownloadFailed", "count", DLfailedcount.ToString());
}
else
{
Success("下载成功!位置:" + SaveLoc.Text + ls5 + @"\");
ProcessB.Value = 0;
ProcessL.Text = "0";
}
}
}
private void DLSettings_Click(object sender, EventArgs e)
{
FileSavingSettings f = new FileSavingSettings();
f.ShowDialog();
}
private void MainForm_Activated(object sender, EventArgs e)
{
SaveLoc.Text = Program.savedir;
}
private void DLSong_Click(object sender, EventArgs e)
{
if (SongListBox.SelectedItem != null)
{
string ls4 = string.Empty;
for (int i2 = 0; i2 < rb.rb[SongListBox.SelectedIndex].artists.Count; i2++)
{
if (i2 != 0) ls4 += ";";
ls4 += rb.rb[SongListBox.SelectedIndex].artists[i2].name;
}
string locx = Conv(
FileSavingSettings.FileNameFilter(Program.ssf, "_"),
string.Empty,
FileSavingSettings.FileNameFilter(ls4, "_"),
FileSavingSettings.FileNameFilter(rb.rb[0].album.name, "_"),
FileSavingSettings.FileNameFilter(rb.rb[0].album.id.ToString(), "_"),
FileSavingSettings.FileNameFilter(rb.rb[SongListBox.SelectedIndex].name, "_"),
FileSavingSettings.FileNameFilter(rb.rb[SongListBox.SelectedIndex].id.ToString(), "_"));
locx = FileSavingSettings.PathFilter(SaveLoc.Text + locx + Program.fmt, "_");
Directory.CreateDirectory(SaveLoc.Text + @"\");
if (DownloadFile("http://music.163.com/song/media/outer/url?id=" + rb.rb[SongListBox.SelectedIndex].id.ToString(), locx))
{
Success("下载成功!位置:" + locx);
}
else
{
Error("下载失败 " + dlerr);
dlerr = string.Empty;
}
using (FileStream fs = File.OpenRead(locx))
{
if (fs.ReadByte() == '<')
{
fs.Close();
fs.Dispose();
File.Delete(locx);
Error("下载失败,可能原因:版权限制或地区限制");
}
}
}
}
private void SaveLoc_TextChanged(object sender, EventArgs e)
{
if (!(SaveLoc.Text.EndsWith("/") || SaveLoc.Text.EndsWith(@"\")))
{
SaveLoc.Text += @"\";
}
Program.savedir = SaveLoc.Text;
IniFiles ini = new IniFiles(Program.tloc + @"\settings.ini");
ini.IniWriteValue("163AlbumGet", "FileSaveDir", Program.savedir);
}
private void GoToPG_Click(object sender, EventArgs e)
{
if (SongListBox.SelectedItem != null)
{
System.Diagnostics.Process.Start("https://music.163.com/song?id=" + rb.rb[SongListBox.SelectedIndex].id.ToString());
}
}
private void SaveDataAsINI_Click(object sender, EventArgs e)
{
if (rb != null)
{
string ls4 = Conv(
FileSavingSettings.FileNameFilter(Program.afn, "_"),
string.Empty,
string.Empty,
FileSavingSettings.FileNameFilter(rb.rb[0].album.name, "_"),
FileSavingSettings.FileNameFilter(rb.rb[0].album.id.ToString(), "_"),
string.Empty,
string.Empty);
Directory.CreateDirectory(SaveLoc.Text + ls4);
IniFiles ini = new IniFiles(SaveLoc.Text + ls4 + @"\专辑信息.ini");
ini.IniWriteValue("AlbumInfo", "id", rb.rb[0].album.id.ToString());
ini.IniWriteValue("AlbumInfo", "name", rb.rb[0].album.name);
ini.IniWriteValue("AlbumInfo", "picUrl", rb.rb[0].album.picUrl);
ini.IniWriteValue("AlbumInfo", "count", rb.rb.Count.ToString());
for (int i = 0; i < rb.rb.Count; i++)
{
ini.IniWriteValue("Song" + i.ToString(), "id", rb.rb[i].id.ToString());
ini.IniWriteValue("Song" + i.ToString(), "name", rb.rb[i].name);
ini.IniWriteValue("Song" + i.ToString(), "duration", rb.rb[i].duration.ToString());
ls2 = string.Empty;
for (int i2 = 0; i2 < rb.rb[i].artists.Count; i2++)
{
if (i2 != 0)
{
ls2 += "/";
}
ls2 += rb.rb[i].artists[i2].name;
}
ls3 = string.Empty;
for (int i2 = 0; i2 < rb.rb[i].artists.Count; i2++)
{
if (i2 != 0)
{
ls3 += "/";
}
ls3 += rb.rb[i].artists[i2].id.ToString();
}
ini.IniWriteValue("Song" + i.ToString(), "artistsName", ls2);
ini.IniWriteValue("Song" + i.ToString(), "artistsID", ls3);
Success("保存成功!位置:" + SaveLoc.Text + ls4 + @"\专辑信息.ini");
}
}
}
private void AlbumID_Enter(object sender, EventArgs e)
{
AlbumIDInputOnFocus = true;
}
private void AlbumID_Leave(object sender, EventArgs e)
{
AlbumIDInputOnFocus = false;
}
private void SongListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (rb != null && SongListBox.SelectedIndex >= 0)
{
ts = new TimeSpan(0, 0, 0, 0, rb.rb[SongListBox.SelectedIndex].duration);
ls1 = ts.Seconds.ToString();
if (ls1.Length == 1)
{
ls1 = "0" + ls1;
}
ls2 = string.Empty;
for (int i = 0; i < rb.rb[SongListBox.SelectedIndex].artists.Count; i++)
{
if (i != 0)
{
ls2 += "/";
}
ls2 += rb.rb[SongListBox.SelectedIndex].artists[i].name;
}
SongID.Text = "ID: " + rb.rb[SongListBox.SelectedIndex].id.ToString();
SongTitle.Text = rb.rb[SongListBox.SelectedIndex].name;
SongArtists.Text = "艺术家: " + ls2;
SongLength.Text = "时长: " + ts.Minutes + ":" + ls1;
}
}
private void Error(string s)
{
Err.ForeColor = Color.Red;
Err.Text = s;
}
private void Success(string s)
{
Err.ForeColor = Color.Green;
Err.Text = s;
}
private void Clear()
{
Err.Text = string.Empty;
AlbumTitle.Text = string.Empty;
SongTitle.Text = string.Empty;
SongID.Text = "ID:";
SongLength.Text = "时长:";
SongArtists.Text = "艺术家:";
AlbumCoverBox.Image = null;
SongListBox.Items.Clear();
}
private void GetIt_Click(object sender, EventArgs e)
{
GET();
}
private void MainForm_Load(object sender, EventArgs e)
{
Directory.CreateDirectory(loc);
IniFiles ini = new IniFiles(Program.tloc + @"\settings.ini");
if (ini.IniReadValue("163AlbumGet", "FileSaveDir").Trim() != string.Empty)
{
Program.savedir = ini.IniReadValue("163AlbumGet", "FileSaveDir");
}
if (ini.IniReadValue("163AlbumGet", "AlbumFolderName").Trim() != string.Empty)
{
Program.afn = ini.IniReadValue("163AlbumGet", "AlbumFolderName");
}
if (ini.IniReadValue("163AlbumGet", "SingleSongFilename").Trim() != string.Empty)
{
Program.ssf = ini.IniReadValue("163AlbumGet", "SingleSongFilename");
}
if (ini.IniReadValue("163AlbumGet", "MultipleSongsFilename").Trim() != string.Empty)
{
Program.msf = ini.IniReadValue("163AlbumGet", "MultipleSongsFilename");
}
SaveLoc.Text = Program.savedir;
}
private bool DownloadFile(string URL, string filename)
{
try
{
HttpWebRequest Myrq = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse();
Stream st = myrp.GetResponseStream();
Stream so = new FileStream(filename, FileMode.Create);
StreamReader myStreamReader = new StreamReader(st, Encoding.UTF8);
byte[] by = new byte[10485760];
int osize = st.Read(by, 0, by.Length);
while (osize > 0)
{
so.Write(by, 0, osize);
osize = st.Read(by, 0, by.Length);
}
so.Close();
st.Close();
myrp.Close();
Myrq.Abort();
dlerr = string.Empty;
return true;
}
catch (Exception ee)
{
dlerr = ee.Message;
return false;
}
}
public static string HttpGet(string Url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = "Why U Need UA?";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
private void GET()
{
Err.Text = string.Empty;
bool b = true;
Regex rx0 = new Regex(@"((https?:)?//)?music\.163\.com/(#/)?album\?\S+", RegexOptions.Compiled);
Regex rx1 = new Regex(@"id=\d+", RegexOptions.Compiled);
Regex rx2 = new Regex(@"\d+", RegexOptions.Compiled);
Match r0 = rx0.Match(AlbumID.Text);
if (r0.Success)
{
Match r1 = rx1.Match(r0.Value);
if (r1.Success)
{
sdataa = rx2.Match(r1.Value).Value;
}
else
{
Error("地址错误");
}
}
else if (rx2.IsMatch(AlbumID.Text))
{
sdataa = AlbumID.Text;
}
else
{
Error("地址错误");
b = false;
}
if (b)
{
try
{
sdata0 = HttpGet("http://music.163.com/album?id=" + sdataa);
}
catch (Exception e)
{
exp = e.Message;
MessageBox.Show("Error!\n\n" + e.Message + "\n" + e.Source + "\n" + e.TargetSite + "\n" + e.Source + "\n" + e.Data + "\n" + e.StackTrace + "\n" + e.HelpLink);
sdata0 = null;
}
if (sdata0 != null)
{
idata0 = sdata0.IndexOf("song-list-pre-data") + 42;
if (idata0 <= 42)
{
Error("获取专辑页面失败");
}
else
{
sdata1 = sdata0.Substring(idata0);
idata1 = sdata1.IndexOf("</textarea>");
if (idata1 <= 0)
{
Error("从页面获取专辑信息失败");
}
else
{
Clear();
sdata2 = "{\"rb\": " + sdata1.Substring(0, idata1) + "}";
rb = JsonConvert.DeserializeObject<RootObject>(sdata2);
AlbumTitle.Text = rb.rb[0].album.name;
AlbumCoverBox.ImageLocation = rb.rb[0].album.picUrl + "?param=177y177.jpg";
for (int i = 0; i < rb.rb.Count; i++)
{
SongListBox.Items.Add((i + 1).ToString() + " | " + rb.rb[i].name);
}
}
}
}
else
{
Error(exp);
}
}
}
private string Conv(string i, string d, string sa, string at, string ai, string st, string si)
{
for (int ii = 0; ii < i.Length; ii++)
{
int a1 = i.IndexOf("&d;");
if (!(a1 < 0))
{
i = i.Substring(0, a1) + d + i.Substring(a1 + 3, i.Length - 3 - a1);
}
else
{
int a2 = i.IndexOf("&sa;");
if (!(a2 < 0))
{
i = i.Substring(0, a2) + sa + i.Substring(a2 + 4, i.Length - 4 - a2);
}
else
{
int a3 = i.IndexOf("&at;");
if (!(a3 < 0))
{
i = i.Substring(0, a3) + at + i.Substring(a3 + 4, i.Length - 4 - a3);
}
else
{
int a4 = i.IndexOf("&ai;");
if (!(a4 < 0))
{
i = i.Substring(0, a4) + ai + i.Substring(a4 + 4, i.Length - 4 - a4);
}
else
{
int a5 = i.IndexOf("&st;");
if (!(a5 < 0))
{
i = i.Substring(0, a5) + st + i.Substring(a5 + 4, i.Length - 4 - a5);
}
else
{
int a6 = i.IndexOf("&si;");
if (!(a6 < 0))
{
i = i.Substring(0, a6) + si + i.Substring(a6 + 4, i.Length - 4 - a6);
}
else
{
break;
}
}
}
}
}
}
}
for (int ii = 0; ii < i.Length; ii++)
{
int a7 = i.IndexOf("&&;");
if (!(a7 < 0))
{
i = i.Substring(0, a7) + "&" + i.Substring(a7 + 3, i.Length - 3 - a7);
}
else
{
break;
}
}
return i;
}
}
public class IniFiles
{
public string inipath;
//声明API函数
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 构造方法
/// </summary>
/// <param name="INIPath">文件路径</param>
public IniFiles(string INIPath)
{
inipath = INIPath;
}
public IniFiles() { }
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">项目名称(如 [TypeName] )</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, inipath);
}
/// <summary>
/// 读出INI文件
/// </summary>
/// <param name="Section">项目名称(如 [TypeName] )</param>
/// <param name="Key">键</param>
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, string.Empty, temp, 500, inipath);
return temp.ToString();
}
/// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}
public class RootObject
{
public List<RB> rb { get; set; }
}
public class RB
{
public Album album { get; set; }
public List<Artists> artists { get; set; }
public string name { get; set; }
public int id { get; set; }
public int duration { get; set; }
}
public class Album
{
public int id { get; set; }
public string name { get; set; }
public string picUrl { get; set; }
}
public class Artists
{
public int id { get; set; }
public string name { get; set; }
}
}