/// <summary> /// 下载图片线程 /// </summary> void DownImg() { while (downStart) { ImgList img = GetUrl(); if (img == null) { if (!isFirst && status == 2) { MessageBox.Show("本次下载任务已完成!", "提示"); status = 0; isFirst = true; } Thread.Sleep(3000); continue; } if (!WebApi.DownloadFile(img.url, img.path)) { string fileName = Path.GetFileName(img.url); string dirName = Path.GetFileName(img.path); MessageBox.Show(dirName + " - " + fileName + " 下载出错!", "提示"); } } }
/// <summary> /// 下载图片线程 /// </summary> /// <param name="DownList">下载列表</param> void DownFile(object para) { List <Torrent> downList = (List <Torrent>)para; int num = downList.Count; int i = 0; while (true) { //获取下载链接 Torrent tor = new Torrent(); if (i < downList.Count) { tor = downList[i]; i++; } else { break; } //解析下载页源码 string html = WebApi.GetHtml(tor.Url); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html); HtmlNode tasks = doc.DocumentNode.SelectSingleNode("//*[@id=\"tabs-1\"]/a"); //搜索结果数组 if (tasks != null) { string url = "https:" + tasks.Attributes["href"].Value; string fileName = FormatFileName(tor.Title) + ".torrent"; string path = txt_path.Text; //保存文件夹路径 //对种子分类 string type = null; string drama = "广播剧"; switch (tor.Type) { case "音乐": case "同人音乐": case "流行音乐": case "动漫音乐": { //判断是否是广播剧 int index = tor.Title.IndexOf(drama); if (index != -1 && (tor.Title[index - 1] != '附' && tor.Title[index - 1] != '&')) { type = TorType[(int)EnumType.广播剧]; } else { type = TorType[(int)EnumType.音乐]; } break; } case "RAW": type = TorType[(int)EnumType.RAW]; break; case "漫画": case "日文原版": case "港台原版": { //判断是否为小说 if (tor.Title.Substring(0, 5).Contains("小说")) { type = TorType[(int)EnumType.小说]; } else { type = TorType[(int)EnumType.漫画]; } break; } case "季度全集": case "动画": { //判断是否为生肉 Regex regRaw = new Regex("raws|reinforce"); if (regRaw.IsMatch(tor.Title.ToLower())) { type = TorType[(int)EnumType.RAW]; break; } //判断是否是广播剧 int index = tor.Title.IndexOf(drama); if (index != -1 && (tor.Title[index - 1] != '附' && tor.Title[index - 1] != '&')) { type = TorType[(int)EnumType.广播剧]; break; } //判断是否为剧场版、OVA或OAD for (int j = 0; j < TheaterKey.Count; j++) { int pos = tor.Title.ToUpper().IndexOf(TheaterKey[j]); if (pos != -1) { if ((tor.Title[pos - 1] != '附' && tor.Title[pos - 1] != '+') && tor.Title[pos - 2] != '+') { type = TorType[(int)(EnumType)(j + 5)]; break; } } } break; } case "其他": type = TorType[(int)EnumType.其他]; break; } if (type != null) { path = Path.Combine(path, type); } if (!WebApi.DownloadFile(url, path, fileName)) { MessageBox.Show(txt_name.Text + " - " + fileName + " 下载出错!", "提示"); } } int percent = i * 100 / num; if (bar_down.InvokeRequired) { // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它 Action <int> actionDelegate = (x) => { this.bar_down.Value = x; }; // 或者 // Action<string> actionDelegate = delegate(string txt) { this.label2.Text = txt; }; bar_down.Invoke(actionDelegate, percent); } else { this.bar_down.Value = percent; } } UpdateBtnStatus(true); this.Invoke((EventHandler) delegate { this.btn_getInfo.Enabled = true; }); MessageBox.Show(txt_name.Text + " 下载完成!", "提示"); }