public static async Task CreateDownload(DownloadTaskModel m, List <DownloadUrlInfo> downloadUrls) { //var urls = await PlayurlHelper.GetVideoUrl_Download(m); if (downloadUrls == null || downloadUrls.Count == 0) { await new MessageDialog(m.epTitle + " 无法读取到下载地址").ShowAsync(); return; //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 < downloadUrls.Count; i++) { CreateDown(m, i, downloadUrls[i], folder); } } catch (Exception ex) { await new MessageDialog(ex.Message).ShowAsync(); } }
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) { } }
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.FirstOrDefault(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) { } }
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.Get_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.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) { } }