Ejemplo n.º 1
0
        public static async Task <IBiliDownload> RecreateAsync(DownloadXmlModel xml, string sessdata)
        {
            var video = await BiliVideoHelper.GetSingleVideoAsync(xml.Bv, xml.Cid, xml.Quality, sessdata);

            var partList = new List <IBiliDownloadPart>();

            var t1 = JsonConvert.DeserializeObject <DownloadTaskRestoreModel>(xml.PartList[0].RestoreModelJson);
            var t2 = JsonConvert.DeserializeObject <DownloadTaskRestoreModel>(xml.PartList[1].RestoreModelJson);

            t1.Url = video.VideoUrl;
            t2.Url = video.AudioUrl;

            var part1 = new BiliDashDownloadPart()//使用构造函数来创建实例,灵活
            {
                Task      = DownloadTask.Restore(t1),
                CacheFile = await StorageFile.GetFileFromPathAsync(t1.Path)
            };
            var part2 = new BiliDashDownloadPart()
            {
                Task      = DownloadTask.Restore(t2),
                CacheFile = await StorageFile.GetFileFromPathAsync(t2.Path)
            };

            partList.Add(part1);
            partList.Add(part2);

            var download = new BiliDashDownload()
            {
                DownloadName = xml.DownloadName,
                Bv           = xml.Bv,
                Cid          = xml.Cid,
                Quality      = xml.Quality,
                VideoUrl     = video.VideoUrl,
                AudioUrl     = video.AudioUrl,
                CacheFolder  = await StorageFolder.GetFolderFromPathAsync(xml.CacheFolderPath),
                PartList     = partList,
                Title        = xml.Title
            };

            download.ChineseStatus = "已暂停";
            ulong currentProgress = 0;
            ulong fullProgress    = 0;

            foreach (var part in partList)
            {
                currentProgress += part.Task.DownloadedBytes;
                fullProgress    += part.Task.TotalBytes;
            }
            download.CurrentProgress = currentProgress;
            download.FullProgress    = fullProgress;
            download.currentSpeed    = 0;
            download.IsPaused        = true;

            return(download);
        }
Ejemplo n.º 2
0
        public static async Task <IBiliDownload> CreateAsync(string bv, long cid, int quality, string sESSDATA)
        {
            var video = await BiliVideoHelper.GetSingleVideoAsync(bv, cid, quality, sESSDATA);

            var tokenSource  = new CancellationTokenSource();
            var downloadName = video.Name.Replace("\\", "").Replace("/", "").Replace(":", "").Replace("*", "")
                               .Replace("?", "").Replace("\"", "").Replace("<", "").Replace(">", "").Replace("|", "");
            var title = video.Title.Replace("\\", "").Replace("/", "").Replace(":", "").Replace("*", "")
                        .Replace("?", "").Replace("\"", "").Replace("<", "").Replace(">", "").Replace("|", "");
            //防止出现不允许的文件名

            var ua      = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36 Edg/84.0.522.63";
            var referer = "http://www.bilibili.com";

            if (Directory.Exists(ApplicationData.Current.LocalCacheFolder.Path + "/" + downloadName + "Cache"))
            {
                await(await ApplicationData.Current.LocalCacheFolder.GetFolderAsync(downloadName + "Cache")).DeleteAsync();
            }
            var cacheFolder = await ApplicationData.Current.LocalCacheFolder.CreateFolderAsync(downloadName + "Cache");

            var partList = new List <IBiliDownloadPart>
            {
                await BiliDashDownloadPart.CreateAsync(video.VideoUrl, ua, referer, $"{downloadName}Video", cacheFolder),
                await BiliDashDownloadPart.CreateAsync(video.AudioUrl, ua, referer, $"{downloadName}Audio", cacheFolder)
            };

            var download = new BiliDashDownload()
            {
                DownloadName = downloadName,
                Bv           = bv,
                Cid          = cid,
                Quality      = quality,
                VideoUrl     = video.VideoUrl,
                AudioUrl     = video.AudioUrl,
                CacheFolder  = cacheFolder,
                PartList     = partList,
                Title        = title
            };

            return(download);
        }