Beispiel #1
0
        public static async Task <bool> CheckSESSDATAStatusAsync(string sessdata)
        {
            if (string.IsNullOrWhiteSpace(sessdata))
            {
                return(false);
            }
            var client = new HttpClient();

            client.DefaultRequestHeaders.Add("cookie", $"SESSDATA={sessdata}");
            try
            {
                var resp = await client.GetStringAsync("https://api.bilibili.com/x/space/myinfo");

                var json = JsonConvert.DeserializeObject <dynamic>(resp);
                if (json.code == 0)
                {
                    Settings.Uid = json.data.mid;
                    _logger.Info("SESSDATA检测结果:有效");
                    return(true);
                }
                else
                {
                    _logger.Info($"SESSDATA已失效,值为{sessdata}");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                var dialog = new ExceptionDialog(ex);
                await dialog.ShowAsync();

                return(false);
            }
        }
        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            var sESSDATA = ApplicationData.Current.LocalSettings.Values["biliUserSESSDATA"] as string;

            var list = new List <BiliVideoInfo>();

            foreach (var item in vm.VideoList)
            {
                if (item.ToDownload == false)
                {
                    continue;
                }
                list.Add(item.Info);
            }
            if (list.Count < 1)
            {
                PrimaryButtonText = "请选择至少一个视频";
                await Task.Delay(2000);

                if (PrimaryButtonText == "请选择至少一个视频")
                {
                    PrimaryButtonText = "下载所选项";
                }
                return;
            }
            if (this.qualityComboBox.SelectedValue == null)
            {
                PrimaryButtonText = "请选择清晰度";
                await Task.Delay(2000);

                if (PrimaryButtonText == "请选择清晰度")
                {
                    PrimaryButtonText = "下载所选项";
                }
                return;
            }
            this.needToClose = true;
            var quality = (int)this.qualityComboBox.SelectedValue;

            try
            {
                await DownloadHelper.CreateDownloadsAsync(list, quality, sESSDATA, XamlRoot);
            }
            catch (DirectoryNotFoundException ex)
            {
                var dialog = new ExceptionDialog(ex.Message, XamlRoot);
                _ = await dialog.ShowAsync();
            }
            catch (ParsingVideoException ex)
            {
                var dialog = new ErrorDialog(ex.Message, XamlRoot);
                var result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Primary)
                {
                    MainPage.NavView.SelectedItem = MainPage.NavViewItems[2];
                    MainPage.ContentFrame.Navigate(typeof(UserPage));
                }
            }
        }
Beispiel #3
0
        private async Task RecreateDownloadAsync()//有未完成的任务,重新创建
        {
            var xmlList = DownloadXmlHelper.GetCurrentDownloads();

            if (xmlList.Count < 1)
            {
                return;
            }

            foreach (var xml in xmlList)
            {
                try
                {
                    var download = await BiliDashDownload
                                   .RecreateAsync(xml, ApplicationData.Current.LocalSettings.Values["biliUserSESSDATA"] as string);

                    this.activeDownloadList.Add(download);
                }
                catch (Exception ex)
                {
                    var dialog = new ExceptionDialog(ex.Message, XamlRoot);
                    await dialog.ShowAsync();
                }
            }
        }
        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            if (qualitySelectListBox.SelectedItem == null)
            {
                PrimaryButtonText = "请选择清晰度";
                await Task.Delay(2000);

                PrimaryButtonText = "下载";
                return;
            }
            this.needToClose = true;
            this.Hide();
            try
            {
                await DownloadHelper.CreateDownloadAsync
                    (vm.Bv, vm.Cid, this.QualitySelectionProperty, ApplicationData.Current.LocalSettings.Values["biliUserSESSDATA"] as string, XamlRoot);
            }
            catch (ParsingVideoException ex)
            {
                var dialog = new ErrorDialog(ex.Message, XamlRoot);
                var result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Primary)
                {
                    MainPage.ContentFrame.Navigate(typeof(UserPage));
                    MainPage.NavView.SelectedItem = MainPage.NavViewItems[2];
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                var dialog = new ExceptionDialog(ex.Message, XamlRoot);
                _ = await dialog.ShowAsync();
            }
        }
Beispiel #5
0
        /// <summary>
        /// 创建下载后自动navigate
        /// </summary>
        /// <param name="bv"></param>
        /// <param name="cid"></param>
        /// <param name="quality"></param>
        /// <param name="sESSDATA"></param>
        /// <returns></returns>
        public static async Task CreateDownloadAsync(string bv, long cid, int quality, string sESSDATA, XamlRoot xamlRoot) //创建下载
        {
            if (string.IsNullOrWhiteSpace(Settings.DownloadPath))                                                          //检查下载目录是否为空
            {
                var dialog = new ErrorDialog("未设置下载储存文件夹,请前往设置以更改", xamlRoot)
                {
                    PrimaryButtonText = "前往设置"
                };
                var result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Secondary)
                {
                    return;
                }
                else
                {
                    MainPage.ContentFrame.Navigate(typeof(SettingPage));
                    MainPage.NavView.SelectedItem = MainPage.NavView.SettingsItem;
                    return;
                }
            }
            try
            {
                await StorageFolder.GetFolderFromPathAsync(Settings.DownloadPath);
            }
            catch (FileNotFoundException)
            {
                throw new DirectoryNotFoundException("找不到指定下载目录:" + Settings.DownloadPath);
            }

            if (Settings.AutoDownloadDanmaku)
            {
                var video = await BiliVideoHelper.GetSingleVideoAsync(bv, cid, quality, sESSDATA);

                _ = BiliDanmakuHelper.DownloadDanmakuAsync(video.Title, video.Name, cid);
            }

            try
            {
                var download = await BiliDashDownload.CreateAsync(bv, cid, quality, sESSDATA);

                DownloadPage.Current.activeDownloadList.Add(download);
                var task = download.StartAsync();
                MainPage.NavView.SelectedItem = MainPage.NavViewItems[0];
                MainPage.ContentFrame.Navigate(typeof(DownloadPage));
                DownloadPage.Current.pivot.SelectedIndex = 0;
                await task;
            }
            catch (Exception ex)
            {
                var dialog = new ExceptionDialog(ex.Message, xamlRoot);
                await dialog.ShowAsync();
            }
        }
Beispiel #6
0
 private async void PauseOrResumeBtn_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         var download = (sender as Button).DataContext as IBiliDownload;
         download.PauseOrResume();
     }
     catch (Exception ex)
     {
         var dialog = new ExceptionDialog(ex);
         await dialog.ShowAsync();
     }
 }
Beispiel #7
0
 private async void CancelBtn_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         var download = (sender as Button).DataContext as IBiliDownload;
         await VideoDownloadManager.CancelDashDownloadAsync(download);
     }
     catch (Exception ex)
     {
         var dialog = new ExceptionDialog(ex);
         await dialog.ShowAsync();
     }
 }
        private async void ShowExceptionMessage(Exception ex)
        {
            var content = $"Exception message: {ex.Message}\n" +
                          "----------------------------------------------------\n" +
                          $"Stacktrace: {ex.StackTrace}";

            var dialog = new ExceptionDialog
            {
                Title            = "Exception",
                ExceptionContent = content
            };

            await dialog.ShowAsync();
        }
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var sESSDATA = ApplicationData.Current.LocalSettings.Values["biliUserSESSDATA"] as string;
            var model    = (sender as Button)?.DataContext as VideoInfo;

            this.needToClose = true;
            this.Hide();

            try
            {
                var info = await BiliVideoHelper.GetSingleVideoInfoAsync(model.Info.Bv, model.Info.Cid, 64, sESSDATA);

                var dialog = await SingleVideoDialog.CreateAsync(info, XamlRoot);

                var result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Secondary)
                {
                    this.needToClose = false; await this.ShowAsync();
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                var dialog = new ExceptionDialog(ex.Message, XamlRoot);
                await dialog.ShowAsync();
            }
            catch (ParsingVideoException ex)
            {
                var dialog = new ErrorDialog(ex.Message, XamlRoot);
                var result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Primary)
                {
                    MainPage.NavView.SelectedItem = MainPage.NavViewItems[2];
                    MainPage.ContentFrame.Navigate(typeof(UserPage));
                }
            }
            catch (System.Exception ex)
            {
                var dialog = new ErrorDialog(ex.Message, XamlRoot);
                dialog.PrimaryButtonText = "";
                await dialog.ShowAsync();
            }
        }
        private async void ShowTraktExceptionMessage(TraktException ex)
        {
            var content = $"Exception message: {ex.Message}\n" +
                          $"Status code: {ex.StatusCode}\n" +
                          $"Request URL: {ex.RequestUrl}\n" +
                          $"Request message: {ex.RequestBody}\n" +
                          $"Request response: {ex.Response}\n" +
                          $"Server Reason Phrase: {ex.ServerReasonPhrase}\n" +
                          "----------------------------------------------------\n" +
                          $"Stacktrace: {ex.StackTrace}";

            var dialog = new ExceptionDialog
            {
                Title            = "Trakt Exception",
                ExceptionContent = content
            };

            await dialog.ShowAsync();
        }
        private async void DownloadCoverBtn_Click(object sender, RoutedEventArgs e)
        {
            if (_client == null)
            {
                _client = new();
            }
            try
            {
                using var stream = await _client.GetStreamAsync(_master.CoverUrl);

                var folder = await StorageFolder.GetFolderFromPathAsync(Settings.DownloadPath);

                var file = await folder.CreateFileAsync(_master.Title + "-封面.png", CreationCollisionOption.ReplaceExisting);

                using var fs = await file.OpenStreamForWriteAsync();

                await stream.CopyToAsync(fs);

                stream.Dispose();
                fs.Dispose();

                Tip2.IsOpen = true;
                _logger.Info($"下载了{_master.Bv}的封面");
            }
            catch (WebException ex)
            {
                _logger.Info(ex, ex.Message);
                var dialog = new ExceptionDialog(ex);
                await dialog.ShowAsync();
            }
            catch (Exception ex)
            {
                _logger.Error(ex, ex.Message);
                var dialog = new ExceptionDialog(ex);
                await dialog.ShowAsync();
            }
        }
Beispiel #12
0
        /// <summary>
        /// 创建下载后自动navigate
        /// </summary>
        /// <param name="videos"></param>
        /// <param name="quality"></param>
        /// <param name="sESSDATA"></param>
        /// <returns></returns>
        public static async Task CreateDownloadsAsync(List <BiliVideoInfo> videos, int quality, string sESSDATA, XamlRoot xamlRoot)
        {
            if (ApplicationData.Current.LocalSettings.Values["downloadPath"] as string == null)//检查下载目录是否为空
            {
                var dialog = new ErrorDialog("未设置下载储存文件夹,请前往设置以更改", xamlRoot)
                {
                    PrimaryButtonText = "前往设置"
                };
                var result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Secondary)
                {
                    return;
                }
                else
                {
                    MainPage.ContentFrame.Navigate(typeof(SettingPage));
                    MainPage.NavView.SelectedItem = MainPage.NavView.SettingsItem;
                    return;
                }
            }

            try
            {
                await StorageFolder.GetFolderFromPathAsync(Settings.DownloadPath);
            }
            catch (FileNotFoundException)
            {
                throw new DirectoryNotFoundException("找不到指定下载目录:" + Settings.DownloadPath);
            }

            var videoList = new List <BiliVideo>();

            foreach (var video in videos)
            {
                videoList.Add(await BiliVideoHelper.GetSingleVideoAsync(video.Bv, video.Cid, quality, sESSDATA));
            }

            if (Settings.AutoDownloadDanmaku)
            {
                _ = BiliDanmakuHelper.DownloadMultiDanmakuAsync(videoList);
            }

            var downloadList = new List <IBiliDownload>();

            foreach (var video in videoList)
            {
                var download = await BiliDashDownload.CreateAsync(video.Bv, video.Cid, quality, sESSDATA);

                downloadList.Add(download);
                DownloadPage.Current.activeDownloadList.Add(download);
            }

            MainPage.NavView.SelectedItem = MainPage.NavViewItems[0];
            MainPage.ContentFrame.Navigate(typeof(DownloadPage));
            DownloadPage.Current.pivot.SelectedIndex = 0;

            var tasks = new List <Task>();

            foreach (var download in downloadList)
            {
                tasks.Add(download.StartAsync());
            }

            try
            {
                await Task.WhenAll(tasks);
            }
            catch (Exception ex)
            {
                var dialog = new ExceptionDialog(ex.Message, xamlRoot);
                await dialog.ShowAsync();
            }
        }
        public async void searchBtn_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(searchTextbox.Text))
            {
                return;
            }

            var sESSDATA = ApplicationData.Current.LocalSettings.Values["biliUserSESSDATA"] as string;

            searchBtn.IsEnabled           = false;
            searchProgressRing.IsActive   = true;
            searchProgressRing.Visibility = Visibility.Visible;

            (string, long, UrlType, int)info = (null, 0, 0, 0);
            try
            {
                info = await AnalyzeVideoUrlAsync(searchTextbox.Text, sESSDATA); //分析输入的url,提取bv或者av,是否指定分p
            }
            catch (NullReferenceException)
            {
                Reset();
                var dialog = new ExceptionDialog("未找到视频", XamlRoot);
                await dialog.ShowAsync();

                return;
            }
            catch (Exception ex)
            {
                Reset();
                var dialog = new ExceptionDialog(ex.Message, XamlRoot);
                await dialog.ShowAsync();

                return;
            }

            if (info.Item3 == UrlType.BangumiEP)//下载ep番剧
            {
                var bangumi = await BiliVideoHelper.GetBangumiInfoAsync(info.Item4, 0, sESSDATA);

                var dialog = await BangumiDialog.CreateAsync(bangumi, XamlRoot);

                Reset();
                var result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Secondary)
                {
                    return;
                }
            }
            else if (info.Item3 == UrlType.BangumiSS)//下载ss番剧
            {
                var bangumi = await BiliVideoHelper.GetBangumiInfoAsync(info.Item4, 1, sESSDATA);

                var dialog = await BangumiDialog.CreateAsync(bangumi, XamlRoot);

                Reset();
                var result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Secondary)
                {
                    return;
                }
            }
            //else if (info.Item3 == UrlType.SingelVideo) //指定了分p的时候,废弃这种用法
            //{
            //    var dialog = await SingleVideoDialog.CreateAsync
            //        (await BiliVideoHelper.GetSingleVideoInfoAsync(info.Item1, info.Item2, 64, sESSDATA));
            //    Reset();
            //    var result = await dialog.ShowAsync();
            //    if (result == ContentDialogResult.Secondary) return;
            //}
            else if (info.Item3 == UrlType.MasteredVideo) //下载普通视频集合
            {
                var master = await BiliVideoHelper.GetVideoMasterInfoAsync(info.Item1, sESSDATA);

                var dialog = await MasteredVideoDialog.CreateAsync(master, XamlRoot);

                Reset();
                var result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Secondary)
                {
                    return;
                }
            }
            else if (info.Item3 == UrlType.MangaMC) //下载漫画
            {
                if (Settings.DownloadPath == null)  //检查下载目录是否为空
                {
                    Reset();
                    var dialog = new ErrorDialog("未设置下载储存文件夹,请前往设置以更改", XamlRoot)
                    {
                        PrimaryButtonText = "前往设置"
                    };
                    var result = await dialog.ShowAsync();

                    if (result == ContentDialogResult.Secondary)
                    {
                        return;
                    }
                    else
                    {
                        MainPage.ContentFrame.Navigate(typeof(SettingPage));
                        MainPage.NavView.SelectedItem = MainPage.NavView.SettingsItem;
                        return;
                    }
                }
                try
                {
                    await StorageFolder.GetFolderFromPathAsync(Settings.DownloadPath);
                }
                catch (FileNotFoundException)
                {
                    Reset();
                    var dialog = new ExceptionDialog("找不到指定下载目录:" + Settings.DownloadPath, XamlRoot);
                    await dialog.ShowAsync();

                    return;
                }
                var master = await BiliMangaHelper.GetBiliMangaMasterAsync(info.Item4, sESSDATA);

                Reset();

                var mangaFrame  = new Frame();
                var mangaWindow = new Window
                {
                    ExtendsContentIntoTitleBar = true,
                    Content = mangaFrame
                };
                mangaWindowDic.Add(master, mangaWindow);
                mangaFrame.Navigate(typeof(MangaDownloadPage), master);
                mangaWindow.Activate();
            }
            else
            {
                Reset();
                var dialog = new ContentDialog()
                {
                    Content = "解析失败,没有找到合适的下载方法"
                };
                await dialog.ShowAsync();
            }
        }
        private async void DanmakuDownloadButton_Click(object sender, RoutedEventArgs e)
        {
            var btn   = sender as Button;
            var model = btn?.DataContext as VideoInfo;

            btn.Content = new ProgressRing()
            {
                IsActive = true
            };

            if (string.IsNullOrWhiteSpace(Settings.DownloadPath))
            {
                this.needToClose = true;
                this.Hide();
                var dialog = new ErrorDialog("未设置下载储存文件夹,请前往设置以更改", XamlRoot)
                {
                    PrimaryButtonText = "前往设置"
                };
                var result = await dialog.ShowAsync();

                if (result == ContentDialogResult.Secondary)
                {
                    return;
                }
                else
                {
                    MainPage.ContentFrame.Navigate(typeof(SettingPage));
                    MainPage.NavView.SelectedItem = MainPage.NavView.SettingsItem;
                    return;
                }
            }

            try
            {
                await BiliDanmakuHelper.DownloadDanmakuAsync(this.vm.VideoTitle, model.Info.Name, model.Info.Cid);

                btn.Content   = "下载完成";
                btn.IsEnabled = false;
            }
            catch (NullReferenceException)
            {
                this.needToClose = true;
                this.Hide();
                var dialog = new ContentDialog()
                {
                    Title   = "错误",
                    Content = new TextBlock()
                    {
                        Text       = "未知错误",
                        FontFamily = new FontFamily("Microsoft Yahei UI"),
                        FontSize   = 20
                    },
                    PrimaryButtonText = "知道了"
                };
                await dialog.ShowAsync();
            }
            catch (DirectoryNotFoundException ex)
            {
                this.needToClose = true;
                this.Hide();
                var dialog = new ExceptionDialog(ex.Message, XamlRoot);
                _ = await dialog.ShowAsync();
            }
            catch (Exception ex)
            {
                this.needToClose = true;
                this.Hide();
                var dialog = new ContentDialog()
                {
                    Title   = "错误",
                    Content = new TextBlock()
                    {
                        Text       = ex.Message,
                        FontFamily = new FontFamily("Microsoft Yahei UI"),
                        FontSize   = 20
                    },
                    PrimaryButtonText = "知道了"
                };
                await dialog.ShowAsync();
            }
        }