Exemplo n.º 1
0
        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            args.Cancel = true;
            bool hide = true;

            if (listView.SelectedItems == null || listView.SelectedItems.Count == 0)
            {
                Utils.ShowMessageToast("请选择要下载的剧集");
                return;
            }
            IsPrimaryButtonEnabled = false;
            foreach (DownloadEpisodeItem item in listView.SelectedItems)
            {
                if (item.State != 0 && item.State != 99)
                {
                    continue;
                }
                try
                {
                    item.State = 1;

                    var downloadInfo = new DownloadInfo()
                    {
                        CoverUrl     = downloadItem.Cover,
                        DanmakuUrl   = "https://api.bilibili.com/x/v1/dm/list.so?oid=" + item.CID,
                        EpisodeID    = item.EpisodeID,
                        CID          = item.CID,
                        AVID         = downloadItem.ID,
                        SeasonID     = downloadItem.SeasonID,
                        SeasonType   = downloadItem.SeasonType,
                        Title        = downloadItem.Title,
                        EpisodeTitle = item.Title,
                        Type         = downloadItem.Type,
                        Index        = item.Index
                    };
                    //读取视频信息
                    //读取视频字幕
                    var info = await playerVM.GetPlayInfo(item.AVID, item.CID);

                    if (info.subtitle != null && info.subtitle.subtitles != null && info.subtitle.subtitles.Count > 0)
                    {
                        downloadInfo.Subtitles = new List <DownloadSubtitleInfo>();
                        foreach (var subtitleItem in info.subtitle.subtitles)
                        {
                            downloadInfo.Subtitles.Add(new DownloadSubtitleInfo()
                            {
                                Name = subtitleItem.lan_doc,
                                Url  = subtitleItem.subtitle_url
                            });
                        }
                    }
                    //读取视频地址
                    var playUrl = await playerVM.GetPlayUrls(new Controls.PlayInfo()
                    {
                        avid        = item.AVID,
                        cid         = item.CID,
                        ep_id       = item.EpisodeID,
                        play_mode   = downloadItem.Type == Helpers.DownloadType.Season ? Controls.VideoPlayType.Season : Controls.VideoPlayType.Video,
                        season_id   = downloadItem.SeasonID,
                        season_type = downloadItem.SeasonType
                    }, qn : (cbQuality.SelectedItem as QualityWithPlayUrlInfo).quality);

                    if (!playUrl.success)
                    {
                        item.State        = 99;
                        item.ErrorMessage = playUrl.message;
                        continue;
                    }
                    downloadInfo.QualityID   = playUrl.data.current.quality;
                    downloadInfo.QualityName = playUrl.data.current.quality_description;
                    downloadInfo.Urls        = new List <DownloadUrlInfo>();
                    if (playUrl.data.current.playUrlInfo.mode == VideoPlayMode.Dash)
                    {
                        downloadInfo.Urls.Add(new DownloadUrlInfo()
                        {
                            FileName   = "video.m4s",
                            HttpHeader = playUrl.data.current.HttpHeader,
                            Url        = playUrl.data.current.playUrlInfo.dash_video_url.baseUrl
                        });
                        downloadInfo.Urls.Add(new DownloadUrlInfo()
                        {
                            FileName   = "audio.m4s",
                            HttpHeader = playUrl.data.current.HttpHeader,
                            Url        = playUrl.data.current.playUrlInfo.dash_audio_url.baseUrl
                        });
                    }
                    if (playUrl.data.current.playUrlInfo.mode == VideoPlayMode.MultiFlv)
                    {
                        int i = 0;
                        foreach (var videoItem in playUrl.data.current.playUrlInfo.multi_flv_url)
                        {
                            downloadInfo.Urls.Add(new DownloadUrlInfo()
                            {
                                FileName   = $"{i}.blv",
                                HttpHeader = playUrl.data.current.HttpHeader,
                                Url        = videoItem.url
                            });
                        }
                    }
                    if (playUrl.data.current.playUrlInfo.mode == VideoPlayMode.SingleFlv || playUrl.data.current.playUrlInfo.mode == VideoPlayMode.SingleMp4)
                    {
                        downloadInfo.Urls.Add(new DownloadUrlInfo()
                        {
                            FileName   = "0.blv",
                            HttpHeader = playUrl.data.current.HttpHeader,
                            Url        = playUrl.data.current.playUrlInfo.url
                        });
                    }
                    //添加下载
                    await DownloadHelper.AddDownload(downloadInfo);

                    item.State = 2;
                }
                catch (Exception ex)
                {
                    hide              = false;
                    item.State        = 99;
                    item.ErrorMessage = ex.Message;
                }
            }
            DownloadVM.Instance.LoadDownloading();
            IsPrimaryButtonEnabled = true;
            if (hide)
            {
                Utils.ShowMessageToast("已添加至下载列表");
                this.Hide();
            }
            else
            {
                Utils.ShowMessageToast("有视频下载失败");
            }
        }