Exemple #1
0
 private bool GetDownloadUrls()
 {
     if (quality < VideoQuality.Q4K)
     {
         //下载链接api为 https://api.bilibili.com/x/player/playurl?avid=44743619&cid=78328965&qn=32 cid为上面获取到的 avid为输入的av号 qn为视频质量
         //https://www.biliplus.com/BPplayurl.php?otype=json&cid=29892777&module=bangumi&qn=16
         WebClient MyWebClient = new WebClient();
         MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
         string callback = "";
         try
         {
             if (!Settings.outland)
             {
                 MyWebClient.Headers.Add("Cookie", User.cookie);
                 callback = Encoding.UTF8.GetString(MyWebClient.DownloadData(string.Format("https://api.bilibili.com/x/player/playurl?avid={0}&cid={1}&qn={2}", aid, cid, quality.ToString()))); //如果获取网站页面采用的是UTF-8,则使用这句
             }
             else
             {
                 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;                                                                                                                          //加上这一句
                 callback = Encoding.UTF8.GetString(MyWebClient.DownloadData(string.Format("https://www.biliplus.com/BPplayurl.php?otype=json&module=bangumi&avid={0}&cid={1}&qn={2}", aid, cid, quality.ToString()))); //如果获取网站页面采用的是UTF-8,则使用这句
             }
         }
         catch (WebException e)
         {
             Dialog.Show("无法下载," + e.Message);
             return(false);
         }
         MyWebClient.Dispose();
         if (!Settings.outland)
         {
             JSONCallback.Player.Player player = JsonConvert.DeserializeObject <JSONCallback.Player.Player>(callback);
             if (player.code == -404)
             {
                 Dialog.Show(string.Format("无法下载 {0}({1}), 该视频需要大会员登录下载,请先登录", player.code, player.message), "获取错误");
                 return(false);
             }
             else if (player.code != 0)
             {
                 Dialog.Show(string.Format("无法下载 {0}({1})", player.code, player.message), "获取错误");
                 return(false);
             }
             if (!player.data.accept_quality.Contains(quality))
             {
                 Console.WriteLine(string.Format("没有指定的画质 {0} ,最高画质为 {1}, 自动下载最高画质{1}", VideoQuality.Name(quality), VideoQuality.Name(player.data.accept_quality[0])));
                 quality = player.data.accept_quality[0];
                 return(GetDownloadUrls());//我太懒了,直接递归吧
             }
             foreach (JSONCallback.Player.DurlItem Item in player.data.durl)
             {
                 DownloadUrl du = new DownloadUrl();
                 du.url  = Item.url;
                 du.size = Item.size;
                 urls.Add(du);
             }
             return(true);
         }
         else
         {
             if (callback == "")
             {
                 Dialog.Show(string.Format("使用BiliPlus API出错!"), "获取错误");
                 return(false);
             }
             JSONCallback.BiliPlus.Player player = JsonConvert.DeserializeObject <JSONCallback.BiliPlus.Player>(callback);
             if (!player.accept_quality.Contains(quality))
             {
                 Console.WriteLine(string.Format("没有指定的画质 {0} ,最高画质为 {1}, 自动下载最高画质{1}", VideoQuality.Name(quality), VideoQuality.Name(player.accept_quality[0])));
                 quality = player.accept_quality[0];
                 return(GetDownloadUrls());//我太懒了,直接递归吧
             }
             foreach (JSONCallback.BiliPlus.DurlItem Item in player.durl)
             {
                 DownloadUrl du = new DownloadUrl();
                 du.url  = Item.url;
                 du.size = Item.size;
                 urls.Add(du);
             }
             return(true);
         }
     }
     else
     {
         string callback = Other.GetHtml("https://www.bilibili.com/video/av" + aid, true);
         string json     = Other.TextGetCenter("window.__playinfo__=", "</script>", callback);
         Console.WriteLine(json);
         try
         {
             JSONCallback.FourKPlayer.FourKPlayer player = JsonConvert.DeserializeObject <JSONCallback.FourKPlayer.FourKPlayer>(json);
             if (player.code == -404)
             {
                 Dialog.Show(string.Format("无法下载 {0}({1}), 该视频需要大会员登录下载,请先登录", player.code, player.message), "获取错误");
                 return(false);
             }
             else if (player.code != 0)
             {
                 Dialog.Show(string.Format("无法下载 {0}({1})", player.code, player.message), "获取错误");
                 return(false);
             }
             if (!player.data.accept_quality.Contains(quality))
             {
                 Console.WriteLine(string.Format("没有指定的画质 {0} ,最高画质为 {1}, 自动下载最高画质{1}", VideoQuality.Name(quality), VideoQuality.Name(player.data.accept_quality[0])));
                 quality = player.data.accept_quality[0];
                 return(GetDownloadUrls());//我太懒了,直接递归吧
             }
             foreach (JSONCallback.FourKPlayer.VideoItem Item in player.data.dash.video)
             {
                 if (Item.id != quality)
                 {
                     continue;
                 }
                 DownloadUrl du = new DownloadUrl();
                 du.type = Item.mimeType.Replace("video/", "");
                 du.url  = Item.baseUrl;
                 du.size = -1;//暂不支持检测大小
                 urls.Add(du);
                 du      = new DownloadUrl();
                 du.type = "mp3";
                 du.url  = player.data.dash.audio[1].baseUrl;
                 du.size = -1;//暂不支持检测大小
                 urls.Add(du);
                 return(true);
             }
             return(false);
         }
         catch (Exception e)
         {
             Console.WriteLine("4K获取出错,正在尝试降级后重试.");
             quality = VideoQuality.Q1080P60;
             return(GetDownloadUrls());
         }
     }
 }
Exemple #2
0
        public void LoadEdge()
        {
            try
            {
                buttons.Clear();
                SelectionPanel.Controls.Clear();
                WebClient MyWebClient = new WebClient
                {
                    Credentials = CredentialCache.DefaultCredentials//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
                };
                MyWebClient.Headers.Add("Cookie", cookie);
                string callback = Encoding.UTF8.GetString(MyWebClient.DownloadData(string.Format("https://api.bilibili.com/x/stein/edgeinfo_v2?aid={0}&edge_id={1}&graph_version={2}", aid, edgeid, graphversion)));
                edgeInfo = JsonConvert.DeserializeObject <JSONCallback.EdgeInfo.EdgeInfo>(callback);
                MyWebClient.Dispose();
                if (!Settings.lowcache)
                {
                    try
                    {
                        if (cid == "")
                        {
                            cid = edgeInfo.data.story_list[0].cid;
                        }

                        new WebClient().DownloadFile(string.Format("http://i0.hdslb.com/bfs/steins-gate/{0}_screenshot.jpg", cid), Environment.CurrentDirectory + string.Format("/temp/av{0}-edge{1}.jpg", aid, edgeid));
                    }
                    catch (Exception)
                    {
                    }
                    if (File.Exists(Environment.CurrentDirectory + string.Format("/temp/av{0}-edge{1}.jpg", aid, edgeid)))
                    {
                        SelectionPanel.BackgroundImage = Image.FromFile(Environment.CurrentDirectory + string.Format("/temp/av{0}-edge{1}.jpg", aid, edgeid));
                    }
                }
                if (edgeInfo.data.edges.questions == null)
                {
                    Dialog.Show("您无法继续选择,请直接开始下载");
                    return;
                }
                for (int i = 0; i < edgeInfo.data.edges.questions[0].choices.Count; i++)
                {
                    JSONCallback.EdgeInfo.ChoicesItem choice = edgeInfo.data.edges.questions[0].choices[i];
                    Button button;
                    button.edgeid = choice.id;
                    button.cid    = choice.cid;
                    button.option = choice.option;
                    if (choice.x != 0 && choice.y != 0)
                    {
                        button.x = Convert.ToInt32(Math.Floor(choice.x / 1.5));
                        button.y = Convert.ToInt32(Math.Floor(choice.y / 1.5));
                    }
                    else
                    {
                        button.x = 0;
                        button.y = (edgeInfo.data.edges.questions[0].choices.Count * 40) - (i * 38);
                    }
                    button.id = random.Next();
                    buttons.Add(button);

                    InteractiveSelectButton btn = new InteractiveSelectButton();
                    SelectionPanel.Controls.Add(btn);
                    btn.Text     = button.option;
                    btn.uniqueid = button.id;
                    btn.Location = new Point(button.x, button.y);
                    btn.Click   += SelectAnswer;
                }
            }
            catch (Exception)
            {
                Dialog.Show("您无法继续选择,请直接开始下载");
            }
        }