public async static void CollectExistAvInfo(List <AvInfo> infos, FileInfo info, string ffmpeg) { AvInfo i = new AvInfo(); i.Location = info.DirectoryName; i.Name = info.Name.Replace(info.Extension, ""); i.Extension = info.Extension; i.Size = info.Length; i.IsChinese = (i.Name.EndsWith("-C") || i.Name.EndsWith("-c")); i.CreateTime = i.CreateTime; try { //i.IsH265 = await FileUtility.IsH265(info.FullName, ffmpeg); } catch (Exception ee) { } infos.Add(i); }
/// <summary> /// 下载视频 /// </summary> public override bool Download() { //开始下载 TipText("正在分析视频地址"); //修正井号 Info.Url = Info.Url.ToLower().TrimEnd('#'); //修正旧版URL Info.Url = Info.Url.Replace("bilibili.tv", "bilibili.com"); Info.Url = Info.Url.Replace("bilibili.us", "bilibili.com"); Info.Url = Info.Url.Replace("bilibili.smgbb.cn", "www.bilibili.com"); Info.Url = Info.Url.Replace("bilibili.kankanews.com", "www.bilibili.com"); //修正简写URL if (Regex.Match(Info.Url, @"^av\d{2,6}$").Success) { Info.Url = "http://www.bilibili.com/video/" + Info.Url + "/"; } //修正index_1.html if (!Info.Url.EndsWith(".html")) { if (Info.Url.EndsWith("/")) { Info.Url += "index_1.html"; } else { Info.Url += "/index_1.html"; } } string url = Info.Url; //取得子页面文件名(例如"/video/av12345/index_123.html") //string suburl = Regex.Match(Info.Url, @"bilibili\.kankanews\.com(?<part>/video/av\d+/index_\d+\.html)").Groups["part"].Value; string suburl = Regex.Match(Info.Url, @"www\.bilibili\.com(?<part>/video/av\d+/index_\d+\.html)").Groups["part"].Value; //取得AV号和子编号 //Match mAVNumber = Regex.Match(Info.Url, @"(?<av>av\d+)/index_(?<sub>\d+)\.html"); Match mAVNumber = BilibiliPlugin.RegexBili.Match(Info.Url); if (!mAVNumber.Success) { mAVNumber = BilibiliPlugin.RegexAcg.Match(Info.Url); } Settings["AVNumber"] = mAVNumber.Groups["id"].Value; Settings["AVSubNumber"] = mAVNumber.Groups["page"].Value; Settings["AVSubNumber"] = string.IsNullOrEmpty(Settings["AVSubNumber"]) ? "1" : Settings["AVSubNumber"]; //设置自定义文件名 Settings["CustomFileName"] = BilibiliPlugin.DefaultFileNameFormat; if (Info.BasePlugin.Configuration.ContainsKey("CustomFileName")) { Settings["CustomFileName"] = Info.BasePlugin.Configuration["CustomFileName"]; } //是否通过【自动应答】禁用对话框 bool disableDialog = AutoAnswer.IsInAutoAnswers(Info.AutoAnswer, "bilibili", "auto"); //视频地址数组 string[] videos = null; try { //解析关联项需要同时满足的条件: //1.这个任务不是被其他任务所添加的 //2.用户设置了“解析关联项” if (!Info.IsBeAdded || Info.ParseRelated) { //取得网页源文件 string src = Network.GetHtmlSource(url, Encoding.UTF8, Info.Proxy); string subtitle = ""; //取得子标题 Regex rSubTitle = new Regex(@"<option value='(?<part>.+?\.html)'( selected)?>(?<content>.+?)</option>"); MatchCollection mSubTitles = rSubTitle.Matches(src); //如果存在下拉列表框 if (mSubTitles.Count > 0) { //确定当前视频的子标题 foreach (Match item in mSubTitles) { if (suburl == item.Groups["part"].Value) { subtitle = item.Groups["content"].Value; break; } } //准备(地址-标题)字典 var dict = new Dictionary <string, string>(); foreach (Match item in mSubTitles) { if (suburl != item.Groups["part"].Value) { dict.Add(url.Replace(suburl, item.Groups["part"].Value), item.Groups["content"].Value); } } //用户选择任务 var ba = new Collection <string>(); if (!disableDialog) { ba = ToolForm.CreateMultiSelectForm(dict, Info.AutoAnswer, "bilibili"); } //根据用户选择新建任务 foreach (string u in ba) { NewTask(u); } } } //获取视频信息API var ts = Convert.ToInt64((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds); var apiAddress = string.Format(@"http://api.bilibili.cn/view?appkey={0}&ts={1}&id={2}&page={3}", BilibiliPlugin.AppKey, ts, Settings["AVNumber"], Settings["AVSubNumber"]); //AcDown所使用的AppKey是旧AppKey,权限比新申请的要高,而且不需要加上sign验证 //如果将来要使用sign验证的话可以使用下面的代码来算出sign //但是这段代码目前加上后还是不能正确工作的状态,不知道为什么 //Tools.GetStringHash("appkey=" + BilibiliPlugin.AppKey + // "&id=" + Settings["AVNumber"] + // "&page=" + Settings["AVSubNumber"] + // "&ts=" + ts + // BilibiliPlugin.AppSecret)); var webrequest = (HttpWebRequest)WebRequest.Create(apiAddress); webrequest.Accept = @"application/json"; webrequest.UserAgent = "AcDown/" + Application.ProductVersion + " ([email protected])"; webrequest.Proxy = Info.Proxy; var viewSrc = Network.GetHtmlSource(webrequest, Encoding.UTF8); //登录获取API结果 if (viewSrc.Contains("no perm error")) { viewSrc = LoginApi(url, apiAddress, out m_cookieContainer); } AvInfo avInfo; try { //解析JSON avInfo = JsonConvert.DeserializeObject <AvInfo>(viewSrc); } catch { //由于接口原因,有时候虽然请求了json但是会返回XML格式(呃 var viewDoc = new XmlDocument(); viewDoc.LoadXml(viewSrc); avInfo = new AvInfo(); avInfo.title = viewDoc.SelectSingleNode(@"/info/title").InnerText.Replace("&", "&"); avInfo.partname = viewDoc.SelectSingleNode(@"/info/partname").InnerText.Replace("&", "&"); avInfo.cid = Regex.Match(viewSrc, @"(?<=\<cid\>)\d+(?=\</cid\>)", RegexOptions.IgnoreCase).Value; } //视频标题和子标题 string title = avInfo.title ?? ""; string stitle = avInfo.partname ?? ""; if (String.IsNullOrEmpty(stitle)) { Info.Title = title; stitle = title; } else { Info.Title = title + " - " + stitle; } //过滤非法字符 title = Tools.InvalidCharacterFilter(title, ""); stitle = Tools.InvalidCharacterFilter(stitle, ""); //清空地址 Info.FilePath.Clear(); Info.SubFilePath.Clear(); //CID Settings["chatid"] = avInfo.cid; //下载弹幕 DownloadComment(title, stitle, Settings["chatid"]); //解析器的解析结果 ParseResult pr = null; //如果允许下载视频 if ((Info.DownloadTypes & DownloadType.Video) != 0) { //var playurlSrc = Network.GetHtmlSource(@"http://interface.bilibili.tv/playurl?otype=xml&cid=" + Settings["chatid"] + "&type=flv", Encoding.UTF8); //var playurlDoc = new XmlDocument(); //playurlDoc.LoadXml(playurlSrc); //获得视频列表 var prRequest = new ParseRequest() { Id = Settings["chatid"], Proxy = Info.Proxy, AutoAnswers = Info.AutoAnswer, CookieContainer = m_cookieContainer }; pr = new BilibiliInterfaceParser().Parse(prRequest); videos = pr.ToArray(); //支持导出列表 if (videos != null) { StringBuilder sb = new StringBuilder(videos.Length * 2); foreach (string item in videos) { sb.Append(item); sb.Append("|"); } if (Settings.ContainsKey("ExportUrl")) { Settings["ExportUrl"] = sb.ToString(); } else { Settings.Add("ExportUrl", sb.ToString()); } } //下载视频 //确定视频共有几个段落 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"; } //设置文件名 var renamehelper = new CustomFileNameHelper(); string filename = renamehelper.CombineFileName(Settings["CustomFileName"], title, stitle, Info.PartCount == 1 ? "" : Info.CurrentPart.ToString(), ext.Replace(".", ""), Settings["AVNumber"], Settings["AVSubNumber"]); filename = Path.Combine(Info.SaveDirectory.ToString(), filename); //生成父文件夹 if (!Directory.Exists(Path.GetDirectoryName(filename))) { Directory.CreateDirectory(Path.GetDirectoryName(filename)); } //设置当前DownloadParameter currentParameter = new DownloadParameter() { //文件名 例: c:\123(1).flv FilePath = filename, //文件URL Url = videos[i], //代理服务器 Proxy = Info.Proxy, //提取缓存 ExtractCache = Info.ExtractCache, ExtractCachePattern = "fla*.tmp" }; //添加文件路径到List<>中 Info.FilePath.Add(currentParameter.FilePath); //下载文件 bool success; //提示更换新Part delegates.NewPart(new ParaNewPart(this.Info, i + 1)); //下载视频 try { success = Network.DownloadFile(currentParameter, this.Info); if (!success) //未出现错误即用户手动停止 { return(false); } } catch //下载文件时出现错误 { //如果此任务由一个视频组成,则报错(下载失败) if (Info.PartCount == 1) { throw; } else //否则继续下载,设置“部分失败”状态 { Info.PartialFinished = true; Info.PartialFinishedDetail += "\r\n文件: " + currentParameter.Url + " 下载失败"; } } } //end for } //end 判断是否下载视频 //如果插件设置中没有GenerateAcPlay项,或此项设置为true则生成.acplay快捷方式 if (!Info.BasePlugin.Configuration.ContainsKey("GenerateAcPlay") || Info.BasePlugin.Configuration["GenerateAcPlay"] == "true") { //生成AcPlay文件 string acplay = GenerateAcplayConfig(pr, title, stitle); //支持AcPlay直接播放 Settings["AcPlay"] = acplay; } //生成视频自动合并参数 if (Info.FilePath.Count > 1 && !Info.PartialFinished) { Info.Settings.Remove("VideoCombine"); var arg = new StringBuilder(); foreach (var item in Info.FilePath) { arg.Append(item); arg.Append("|"); } var renamehelper = new CustomFileNameHelper(); string filename = renamehelper.CombineFileName(Settings["CustomFileName"], title, stitle, "", "mp4", Info.Settings["AVNumber"], Info.Settings["AVSubNumber"]); filename = Path.Combine(Info.SaveDirectory.ToString(), filename); arg.Append(filename); Info.Settings["VideoCombine"] = arg.ToString(); } } catch { Settings["user"] = ""; Settings["password"] = ""; throw; } return(true); }