/// <summary> /// 添加任务 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAdd_Click(object sender, EventArgs e) { //判断是否是“粘贴并添加” if (txtInput.Text.Trim() == "" && Clipboard.ContainsText()) //如果文本框为空则为“粘贴并添加” { txtInput.Text = Clipboard.GetText(); } string url = txtInput.Text; this.Cursor = Cursors.WaitCursor; IPlugin selectedPlugin = null; //如果有可用插件 if (supportedPlugins.Count > 0) { selectedPlugin = supportedPlugins[cboPlugins.SelectedIndex]; //取得此url的hash string hash = selectedPlugin.GetHash(url); //检查是否有已经在进行的相同任务 foreach (TaskInfo task in _taskMgr.TaskInfos) { if (hash == task.Hash) { toolTip.Show("当前任务已经存在", txtInput, 4000); this.Cursor = Cursors.Default; return; } } try { //取得[代理设置] AcDownProxy selectedProxy = null; if (Config.setting.Proxy_Settings != null) { foreach (AcDownProxy item in Config.setting.Proxy_Settings) { if (item.Name == cboProxy.SelectedItem.ToString()) { selectedProxy = item; } } } //取得[AutoAnswer设置] List <AutoAnswer> aa = new List <AutoAnswer>(); if (chkAutoAnswer.Checked) { if (selectedPlugin.Feature.ContainsKey("AutoAnswer")) { aa = (List <AutoAnswer>)selectedPlugin.Feature["AutoAnswer"]; if (aa.Count > 0) { FormAutoAnswer faa = new FormAutoAnswer(aa); faa.TopMost = this.TopMost; var result = faa.ShowDialog(); if (result == System.Windows.Forms.DialogResult.Cancel) { this.Cursor = Cursors.Default; return; } } } } //添加任务 TaskInfo task = _taskMgr.AddTask(selectedPlugin, url, (selectedProxy == null) ? null : selectedProxy.ToWebProxy()); //设置[保存目录] task.SaveDirectory = new DirectoryInfo(txtPath.Text); //设置[字幕] DownloadSubtitleType ds = DownloadSubtitleType.DownloadSubtitle; if (cboDownSub.SelectedIndex == 1) { ds = DownloadSubtitleType.DontDownloadSubtitle; } if (cboDownSub.SelectedIndex == 2) { ds = DownloadSubtitleType.DownloadSubtitleOnly; } task.DownSub = ds; //设置[提取浏览器缓存] task.ExtractCache = chkExtractCache.Checked; //设置[解析关联视频] task.ParseRelated = chkParseRelated.Checked; //设置[自动应答] task.AutoAnswer = aa; //设置注释 task.Comment = txtComment.Text; //开始下载 _taskMgr.StartTask(task); this.Cursor = Cursors.Default; this.Close(); } catch (Exception ex) { Logging.Add(ex); toolTip.Show("新建任务出现错误:\n" + ex.Message, btnAdd, 4000); } } else { toolTip.Show("您所输入的网络地址(URL)不符合规则。\n没有支持解析此网址的插件,请您检查后重新输入", txtInput, 3000); txtInput.SelectAll(); } this.Cursor = Cursors.Default; }
//下载视频 public bool Download() { //开始下载 delegates.TipText(new ParaTipText(this.Info, "正在分析视频地址")); string url = Info.Url; //视频地址数组 string[] videos = null; try { //取得网页源文件 string src = Network.GetHtmlSource(url, Encoding.UTF8, Info.Proxy); //视频id string id = ""; //type值 string type = ""; //player id string playerId = ""; //选择的视频(下拉列表) int selectedvideo = 0; //清空地址 Info.FilePath.Clear(); //取得视频标题 Regex rTitle = new Regex(@"<title>(?<title>.*)</title>"); Match mTitle = rTitle.Match(src); //分析id和视频存放站点(type) //取得"mplayer块的源代码 Regex rEmbed = new Regex(@"<div id=""mplayer"">.+?</embed>", RegexOptions.Singleline); Match mEmbed = rEmbed.Match(src); string embedSrc = mEmbed.Value; //取得id值 Regex rId = new Regex(@"\w+id=(?<id>\w+)"); MatchCollection mIds = rId.Matches(embedSrc); //取得type值 Regex rType = new Regex(@"type=(?<type>\w+)"); MatchCollection mTypes = rType.Matches(embedSrc); //取得PlayerID值 Regex rPlayerid = new Regex(@"<li>(?<playerid>content.+?)</li>"); Match mPlayerid = rPlayerid.Match(embedSrc); playerId = mPlayerid.Groups["playerid"].Value; //取得所有子标题 Regex rSubTitle = new Regex(@"\|(?<subtitle>.*?)(\*\*|</li>)"); MatchCollection mSubTitles = rSubTitle.Matches(embedSrc); Match mId = null; Match mType = null; Match mSubTitle = null; if (mIds.Count > 1) //如果数量大于一个 { //定义字典 var dict = new Dictionary <string, string>(); for (int i = 0; i < mIds.Count; i++) { dict.Add(i.ToString(), (i + 1).ToString() + "、" + mSubTitles[i].Groups["subtitle"].Value); } //用户选择下载哪一个视频 selectedvideo = int.Parse(ToolForm.CreateSingleSelectForm("请选择视频:", dict, "", Info.AutoAnswer, "tucao")); mId = mIds[selectedvideo]; mType = mTypes[selectedvideo]; mSubTitle = mSubTitles[selectedvideo]; } else { mId = mIds[0]; mType = mTypes[0]; mSubTitle = mSubTitles[0]; } //设置标题 string title = mTitle.Groups["title"].Value.Replace("- 吐槽 - tucao.cc", ""); string subTitle = mSubTitle.Groups["subtitle"].Value; if (!string.IsNullOrEmpty(subTitle)) //如果存在子标题(视频为合集) { //更改标题 title = title + " - " + subTitle; //更改URL防止hash时出错 Info.Url = Info.Url + "#" + subTitle; } //过滤非法字符 Info.Title = title; title = Tools.InvalidCharacterFilter(title, ""); //取得ID id = mId.Groups["id"].Value; //取得type值 type = mType.Groups["type"].Value; DownloadSubtitleType downsub = Info.DownSub; //如果不是“仅下载字幕” if (downsub != DownloadSubtitleType.DownloadSubtitleOnly) { //检查外链 switch (type) { case "qq": //QQ视频 //解析视频 QQVideoParser parserQQ = new QQVideoParser(); videos = parserQQ.Parse(new ParseRequest() { Id = id, Proxy = Info.Proxy, AutoAnswers = Info.AutoAnswer }).ToArray(); break; case "youku": //优酷视频 //解析视频 YoukuParser parserYouKu = new YoukuParser(); videos = parserYouKu.Parse(new ParseRequest() { Id = id, Proxy = Info.Proxy, AutoAnswers = Info.AutoAnswer }).ToArray(); break; case "tudou": //土豆视频 //解析视频 TudouParser parserTudou = new TudouParser(); videos = parserTudou.Parse(new ParseRequest() { Id = id, Proxy = Info.Proxy, AutoAnswers = Info.AutoAnswer }).ToArray(); break; case "sina": //新浪视频 SinaVideoParser parserSina = new SinaVideoParser(); videos = parserSina.Parse(new ParseRequest() { Id = id, Proxy = Info.Proxy, AutoAnswers = Info.AutoAnswer }).ToArray(); break; } Info.videos = videos; return(true); //下载视频 //确定视频共有几个段落 Info.PartCount = videos.Length; //------------分段落下载------------ for (int i = 0; i < Info.PartCount; i++) { Info.CurrentPart = i + 1; //取得文件后缀名 string ext = Tools.GetExtension(videos[i]); if (string.IsNullOrEmpty(ext)) { if (string.IsNullOrEmpty(Path.GetExtension(videos[i]))) { ext = ".flv"; } else { ext = Path.GetExtension(videos[i]); } } if (ext == ".hlv") { ext = ".flv"; } //设置当前DownloadParameter if (Info.PartCount == 1) { currentParameter = new DownloadParameter() { //文件名 例: c:\123(1).flv FilePath = Path.Combine(Info.SaveDirectory.ToString(), title + ext), //文件URL Url = videos[i], //代理服务器 Proxy = Info.Proxy }; } else { currentParameter = new DownloadParameter() { //文件名 例: c:\123(1).flv FilePath = Path.Combine(Info.SaveDirectory.ToString(), title + "(" + (i + 1).ToString() + ")" + ext), //文件URL Url = videos[i], //代理服务器 Proxy = Info.Proxy }; } //添加文件路径到List<>中 Info.FilePath.Add(currentParameter.FilePath); //下载文件 bool success; //添加断点续传段 //if (File.Exists(currentParameter.FilePath)) //{ // //取得文件长度 // int len = int.Parse(new FileInfo(currentParameter.FilePath).Length.ToString()); // //设置RangeStart属性 // currentParameter.RangeStart = len; // Info.Title = "[续传]" + Info.Title; //} //else //{ // Info.Title = Info.Title.Replace("[续传]", ""); //} //提示更换新Part delegates.NewPart(new ParaNewPart(this.Info, i + 1)); //下载视频 try { success = Network.DownloadFile(currentParameter, this.Info); if (!success) //未出现错误即用户手动停止 { return(false); } } catch (Exception ex) //下载文件时出现错误 { //如果此任务由一个视频组成,则报错(下载失败) if (Info.PartCount == 1) { throw ex; } else //否则继续下载,设置“部分失败”状态 { Info.PartialFinished = true; Info.PartialFinishedDetail += "\r\n文件: " + currentParameter.Url + " 下载失败"; } } } //end for } //下载弹幕 if ((downsub != DownloadSubtitleType.DontDownloadSubtitle) && !string.IsNullOrEmpty(playerId)) { //----------下载字幕----------- delegates.TipText(new ParaTipText(this.Info, "正在下载字幕文件")); //字幕文件(on)地址 string subfile = Path.Combine(Info.SaveDirectory.ToString(), title + ".xml"); Info.SubFilePath.Add(subfile); //取得字幕文件(on)地址 string subUrl = "http://www.tucao.cc/index.php?m=comment&c=mukio&a=init&type=" + type + "&playerID=" + playerId + "~" + selectedvideo.ToString() + "&r=0.09502756828442216"; //下载字幕文件 try { Network.DownloadFile(new DownloadParameter() { Url = subUrl, FilePath = subfile, Proxy = Info.Proxy }); } catch { Info.PartialFinished = true; Info.PartialFinishedDetail += "\r\n弹幕文件文件下载失败"; } } // end 下载弹幕xml } catch (Exception ex) { throw ex; } //end try //下载成功完成 return(true); }