private static async Task SetPartInfo(DownloadTaskModel m, StorageFolder folder) { try { StorageFile file = null; if (await ExistsFile(folder.Path + @"/info.json")) { return; } else { file = await folder.CreateFileAsync("info.json", CreationCollisionOption.OpenIfExists); } DownloadPartnInfoModel downloadVideonInfoModel = new DownloadPartnInfoModel() { index = m.epIndex, title = m.epTitle, cid = m.cid, path = folder.Path, epid = m.epid }; var infoJson = JsonConvert.SerializeObject(downloadVideonInfoModel); await FileIO.WriteTextAsync(file, infoJson); } catch (Exception) { throw; } }
public static async void CreateDownload(DownloadTaskModel m) { var urls = await PlayurlHelper.GetVideoUrl_Download(m); if (urls == null || urls.Count == 0) { await new MessageDialog(m.epTitle + " 无法读取到下载地址").ShowAsync(); //throw new Exception(m.epTitle + " 无法读取到下载地址"); } try { var folder = await GetCIDFolder(m); await Task.Run(async() => { await SetVideoInfo(m, folder); await SetPartInfo(m, folder); await DownloadDanmu(m.cid, folder); await DownThumb(m.thumb, await folder.GetParentAsync()); }); for (int i = 0; i < urls.Count; i++) { CreateDown(m, i, urls[i], folder); } } catch (Exception ex) { await new MessageDialog(ex.Message).ShowAsync(); } }
private static async void CreateDown(DownloadTaskModel m, int index, string url, StorageFolder folder) { BackgroundDownloader downloader = new BackgroundDownloader(); downloader.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"); if (!url.Contains("360.cn")) { downloader.SetRequestHeader("Referer", "https://www.bilibili.com/blackboard/html5player.html?crossDomain=true"); } //设置下载模式 if (SettingHelper.Get_DownMode() == 0) { group.TransferBehavior = BackgroundTransferBehavior.Serialized; } else { group.TransferBehavior = BackgroundTransferBehavior.Parallel; } downloader.TransferGroup = group; //创建视频文件 var filetype = ".flv"; if (url.Contains(".mp4")) { filetype = ".mp4"; } StorageFile file = await folder.CreateFileAsync(index.ToString("000") + filetype, CreationCollisionOption.OpenIfExists); DownloadOperation downloadOp = downloader.CreateDownload(new Uri(url), file); //设置下载策略 if (SettingHelper.Get_Use4GDown()) { downloadOp.CostPolicy = BackgroundTransferCostPolicy.Always; } else { downloadOp.CostPolicy = BackgroundTransferCostPolicy.UnrestrictedOnly; } SqlHelper.InsertDownload(new DownloadGuidClass() { guid = downloadOp.Guid.ToString(), cid = m.cid, index = index, aid = (m.downloadMode == DownloadMode.Anime) ? m.sid : m.avid, eptitle = m.epTitle, title = m.title, mode = (m.downloadMode == DownloadMode.Anime) ? "anime" : "video" }); try { await downloadOp.StartAsync(); } catch (Exception) { } }
private static async Task <StorageFolder> GetCIDFolder(DownloadTaskModel m) { string setting = SettingHelper.Get_DownPath(); StorageFolder DownFolder = null; try { if (SettingHelper.Get_CustomDownPath() && setting != "系统视频库") { string mruFirstToken = StorageApplicationPermissions.MostRecentlyUsedList.Entries.First(x => x.Metadata == setting).Token; var settingFolder = await StorageApplicationPermissions.MostRecentlyUsedList.GetFolderAsync(mruFirstToken); DownFolder = await settingFolder.CreateFolderAsync("BiliBiliDownload", CreationCollisionOption.OpenIfExists); } else { DownFolder = await KnownFolders.VideosLibrary.CreateFolderAsync("BiliBiliDownload", CreationCollisionOption.OpenIfExists); } } catch (Exception) { DownFolder = await KnownFolders.VideosLibrary.CreateFolderAsync("BiliBiliDownload", CreationCollisionOption.OpenIfExists); } StorageFolder avidFolder = null; if (m.downloadMode == DownloadMode.Video) { avidFolder = await DownFolder.CreateFolderAsync(m.avid, CreationCollisionOption.OpenIfExists); } else { avidFolder = await DownFolder.CreateFolderAsync("s_" + m.sid, CreationCollisionOption.OpenIfExists); } StorageFolder cidFolder = await avidFolder.CreateFolderAsync(m.cid, CreationCollisionOption.OpenIfExists); return(cidFolder); }
private static async Task SetVideoInfo(DownloadTaskModel m, StorageFolder folder) { try { var f = await folder.GetParentAsync(); StorageFile file = null; if (await ExistsFile(f.Path + @"/info.json")) { return; } else { file = await f.CreateFileAsync("info.json", CreationCollisionOption.OpenIfExists); } DownloadVideonInfoModel downloadVideonInfoModel = new DownloadVideonInfoModel() { thumb = m.thumb, title = m.title }; if (m.downloadMode == DownloadMode.Video) { downloadVideonInfoModel.id = m.avid; downloadVideonInfoModel.type = "video"; } else { downloadVideonInfoModel.id = m.sid; downloadVideonInfoModel.type = "anime"; } var infoJson = JsonConvert.SerializeObject(downloadVideonInfoModel); await FileIO.WriteTextAsync(file, infoJson); } catch (Exception) { throw; } }
private static async void CreateDown(DownloadTaskModel m, int index, DownloadUrlInfo url, StorageFolder folder) { BackgroundDownloader downloader = new BackgroundDownloader(); foreach (var item in url.Headers) { downloader.SetRequestHeader(item.Key, item.Value); } //downloader.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0"); //if (!url.Contains("360.cn")) //{ // downloader.SetRequestHeader("Origin", "https://www.bilibili.com/"); // downloader.SetRequestHeader("Referer", "https://www.bilibili.com/"); //} //设置下载模式 if (SettingHelper.DownMode == 0) { group.TransferBehavior = BackgroundTransferBehavior.Serialized; } else { group.TransferBehavior = BackgroundTransferBehavior.Parallel; } downloader.TransferGroup = group; //创建视频文件 var filetype = ".flv"; if (url.Url.Contains(".mp4")) { filetype = ".mp4"; } StorageFile file = await folder.CreateFileAsync(index.ToString("000") + filetype, CreationCollisionOption.OpenIfExists); DownloadOperation downloadOp = downloader.CreateDownload(new Uri(url.Url), file); //设置下载策略 if (SettingHelper.Use4GDown) { downloadOp.CostPolicy = BackgroundTransferCostPolicy.Always; } else { downloadOp.CostPolicy = BackgroundTransferCostPolicy.UnrestrictedOnly; } using (var context = SqlHelper.CreateContext()) { context.InsertDownload(new DownloadGuidClass() { Guid = downloadOp.Guid.ToString(), Cid = m.cid, Index = index, Aid = (m.downloadMode == DownloadMode.Anime) ? m.sid : m.avid, Eptitle = m.epTitle, Title = m.title, Mode = (m.downloadMode == DownloadMode.Anime) ? "anime" : "video" }); } try { await downloadOp.StartAsync(); } catch (Exception ex) { Debug.WriteLine(ex); } }
/// <summary> /// 读取下载地址 /// </summary> /// <param name="m"></param> /// <returns></returns> public static async Task <List <string> > GetVideoUrl_Download(DownloadTaskModel m) { try { List <string> urls = new List <string>(); var qn = 32; switch (m.quality) { case 1: qn = 32; break; case 2: qn = 64; break; case 3: qn = 80; break; case 4: qn = 112; break; default: break; } //https://interface.bilibili.com/v2/playurl?cid=31452468&appkey=84956560bc028eb7&otype=json&type=&quality=112&qn=112&sign=38b1355368ee65d055c12b57bdb26e3a if (m.downloadMode == DownloadMode.Video) { var videos = await GetVideoUrl(m.avid, m.cid, qn); if (videos != null) { return(videos.urls); } else { return(null); } } else { var ban = await GetBangumiUrl(new PlayerModel() { Aid = m.avid, banId = m.sid, episode_id = m.epid, banInfo = new Models.episodesModel() { episode_id = m.epid }, index = m.epIndex, Mid = m.cid }, qn); if (ban != null) { return(ban.urls); } else { return(null); } } } catch (Exception) { return(null); } }
/// <summary> /// 读取下载地址 /// </summary> /// <param name="m"></param> /// <returns></returns> public static async Task <List <string> > GetVideoUrl_Download(DownloadTaskModel m) { try { List <string> urls = new List <string>(); var qn = 32; switch (m.quality) { case 1: qn = 32; break; case 2: qn = 64; break; case 3: qn = 80; break; case 4: qn = 112; break; default: break; } //https://interface.bilibili.com/v2/playurl?cid=31452468&appkey=84956560bc028eb7&otype=json&type=&quality=112&qn=112&sign=38b1355368ee65d055c12b57bdb26e3a if (m.downloadMode == DownloadMode.Video) { string url = string.Format("https://interface.bilibili.com/v2/playurl?cid={1}&appkey={0}&otype=json&type=&quality={2}&qn={2}&ts={3}", ApiHelper._appKey_VIDEO, m.cid, qn, ApiHelper.GetTimeSpan_2); url += "&sign=" + ApiHelper.GetSign_VIDEO(url); string re = await WebClientClass.GetResults(new Uri(url)); FlvPlyaerUrlModel flvPlyaerUrlModel = JsonConvert.DeserializeObject <FlvPlyaerUrlModel>(re); if (flvPlyaerUrlModel.code == 0) { foreach (var item in flvPlyaerUrlModel.durl) { urls.Add(item.url); } } else { return(null); } } else { string url = string.Format("https://interface.bilibili.com/playurl?cid={0}&player=1&quality={1}&qn={1}&ts={2}", m.cid, qn, ApiHelper.GetTimeSpan); url += "&sign=" + ApiHelper.GetSign_PlayUrl(url); string re = await WebClientClass.GetResults(new Uri(url)); if (re.Contains("<code>")) { string url2 = string.Format( "https://bangumi.bilibili.com/player/web_api/v2/playurl?cid={1}&appkey={0}&otype=json&type=&quality={2}&module=bangumi&season_type=1&qn={2}&ts={3}", ApiHelper._appKey_VIDEO, m.cid, qn, ApiHelper.GetTimeSpan_2); url2 += "&sign=" + ApiHelper.GetSign_VIDEO(url2); re = await WebClientClass.GetResults(new Uri(url2)); FlvPlyaerUrlModel flvPlyaerUrlModel = JsonConvert.DeserializeObject <FlvPlyaerUrlModel>(re); if (flvPlyaerUrlModel.code == 0 && !re.Contains("8986943")) { foreach (var item in flvPlyaerUrlModel.durl) { urls.Add(item.url); } } else { var re3 = await WebClientClass.GetResults(new Uri(string.Format("https://moe.nsapps.cn/api/v1/BiliAnimeUrl?animeid={0}&cid={1}&epid={2}&rnd={3}", m.sid, m.cid, m.epid, ApiHelper.GetTimeSpan))); JObject obj = JObject.Parse(re3); if (Convert.ToInt32(obj["code"].ToString()) == 0) { urls.Add(obj["data"][0]["url"].ToString()); } else { var playurl = await _5DMHelper.GetUrl(m.sid, Convert.ToInt32(m.epIndex) - 1); if (playurl == "") { return(null); } else { urls.Add(playurl); } } } } else { var mc = Regex.Matches(re, @"<length>(.*?)</length>.*?<size>(.*?)</size>.*?<url><!\[CDATA\[(.*?)\]\]></url>", RegexOptions.Singleline); foreach (Match item in mc) { urls.Add(item.Groups[3].Value); } } } return(urls); } catch (Exception) { return(null); } }