コード例 #1
0
        private async static Task SaveInfo(DownloadInfo info, StorageFolder folder, StorageFolder episodeFolder)
        {
            try
            {
                DownloadSaveInfo downloadSaveInfo = new DownloadSaveInfo()
                {
                    Cover      = info.CoverUrl,
                    SeasonType = info.SeasonType,
                    Title      = info.Title,
                    Type       = info.Type,
                    ID         = info.Type == DownloadType.Season? info.SeasonID.ToString():info.AVID
                };
                DownloadSaveEpisodeInfo downloadSaveEpisodeInfo = new DownloadSaveEpisodeInfo()
                {
                    CID          = info.CID,
                    DanmakuPath  = "danmaku.xml",
                    EpisodeID    = info.EpisodeID,
                    EpisodeTitle = info.EpisodeTitle,
                    AVID         = info.AVID,
                    SubtitlePath = new List <DownloadSubtitleInfo>(),
                    Index        = info.Index,
                    VideoPath    = new List <string>(),
                    QualityID    = info.QualityID,
                    QualityName  = info.QualityName
                };
                if (info.Subtitles != null)
                {
                    foreach (var item in info.Subtitles)
                    {
                        downloadSaveEpisodeInfo.SubtitlePath.Add(new DownloadSubtitleInfo()
                        {
                            Name = item.Name,
                            Url  = item.Name + ".json"
                        });
                    }
                }
                foreach (var item in info.Urls)
                {
                    downloadSaveEpisodeInfo.VideoPath.Add(item.FileName);
                }
                downloadSaveEpisodeInfo.IsDash = downloadSaveEpisodeInfo.VideoPath.FirstOrDefault(x => x.Contains(".m4s")) != null;
                StorageFile file = await folder.CreateFileAsync("info.json", CreationCollisionOption.ReplaceExisting);

                await FileIO.WriteTextAsync(file, JsonConvert.SerializeObject(downloadSaveInfo));

                StorageFile episodeFile = await episodeFolder.CreateFileAsync("info.json", CreationCollisionOption.ReplaceExisting);

                await FileIO.WriteTextAsync(episodeFile, JsonConvert.SerializeObject(downloadSaveEpisodeInfo));
            }
            catch (Exception ex)
            {
                LogHelper.Log("文件保存失败:" + episodeFolder.Path, LogType.ERROR, ex);
            }
        }
コード例 #2
0
        private async static void DownloadVideo(DownloadInfo downloadInfo, DownloadUrlInfo url, StorageFolder episodeFolder)
        {
            BackgroundDownloader downloader = new BackgroundDownloader();

            if (url.HttpHeader != null)
            {
                foreach (var item in url.HttpHeader)
                {
                    downloader.SetRequestHeader(item.Key, item.Value);
                }
            }
            var parallelDownload = SettingHelper.GetValue <bool>(SettingHelper.Download.PARALLEL_DOWNLOAD, true);
            var allowCostNetwork = SettingHelper.GetValue <bool>(SettingHelper.Download.ALLOW_COST_NETWORK, false);

            //设置下载模式
            if (parallelDownload)
            {
                group.TransferBehavior = BackgroundTransferBehavior.Parallel;
            }
            else
            {
                group.TransferBehavior = BackgroundTransferBehavior.Serialized;
            }
            downloader.TransferGroup = group;


            //创建视频文件

            StorageFile file = await episodeFolder.CreateFileAsync(url.FileName, CreationCollisionOption.OpenIfExists);

            DownloadOperation downloadOp = downloader.CreateDownload(new Uri(url.Url), file);

            //设置下载策略
            if (allowCostNetwork)
            {
                downloadOp.CostPolicy = BackgroundTransferCostPolicy.Always;
            }
            else
            {
                downloadOp.CostPolicy = BackgroundTransferCostPolicy.UnrestrictedOnly;
            }
            var guid = downloadOp.Guid.ToString();

            SettingHelper.SetValue(guid, new DownloadGUIDInfo()
            {
                CID          = downloadInfo.CID,
                Path         = episodeFolder.Path,
                EpisodeTitle = downloadInfo.EpisodeTitle,
                FileName     = url.FileName,
                Title        = downloadInfo.Title,
                GUID         = guid,
                Type         = downloadInfo.Type,
                ID           = downloadInfo.Type == DownloadType.Video? downloadInfo.AVID: downloadInfo.SeasonID.ToString()
            });

            try
            {
                await downloadOp.StartAsync();
            }
            catch (Exception)
            {
            }
        }