public static string GetHtml(string url, bool withcookie = false) { string htmlCode; HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); webRequest.Timeout = 6000; webRequest.Method = "GET"; webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0"; webRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); webRequest.Referer = "https://www.bilibili.com/"; if (withcookie) { webRequest.Headers.Add("Cookie", User.cookie + ";CURRENT_QUALITY=120"); } HttpWebResponse webResponse = null; try { webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse(); } catch (Exception e) { Dialog.Show(e.Message, "获取内容失败"); return(""); } //获取目标网站的编码格式 string contentype = webResponse.Headers["Content-Type"]; Regex regex = new Regex("charset\\s*=\\s*[\\W]?\\s*([\\w-]+)", RegexOptions.IgnoreCase); if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压 { using (System.IO.Stream streamReceive = webResponse.GetResponseStream()) { using (System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress)) { //匹配编码格式 if (regex.IsMatch(contentype)) { Encoding ending = Encoding.GetEncoding(regex.Match(contentype).Groups[1].Value.Trim()); using (StreamReader sr = new System.IO.StreamReader(zipStream, ending)) { htmlCode = sr.ReadToEnd(); } } else { using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.UTF8)) { htmlCode = sr.ReadToEnd(); } } } } } else if (webResponse.ContentEncoding.ToLower() == "deflate") { using (System.IO.Stream streamReceive = webResponse.GetResponseStream()) { using (System.IO.Compression.DeflateStream zipStream = new System.IO.Compression.DeflateStream(streamReceive, System.IO.Compression.CompressionMode.Decompress)) { //匹配编码格式 if (regex.IsMatch(contentype)) { Encoding ending = Encoding.GetEncoding(regex.Match(contentype).Groups[1].Value.Trim()); using (StreamReader sr = new System.IO.StreamReader(zipStream, ending)) { htmlCode = sr.ReadToEnd(); } } else { using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.UTF8)) { htmlCode = sr.ReadToEnd(); } } } } } else { using (System.IO.Stream streamReceive = webResponse.GetResponseStream()) { using (System.IO.StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.Default)) { htmlCode = sr.ReadToEnd(); } } } return(htmlCode); }
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 { switch (Settings.useapi) { case 0: 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,则使用这句 break; case 1: 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,则使用这句 break; case 2: //force_host=0&&npcybs=0 MyWebClient.Headers.Add("Cookie", User.cookie); string api = string.Format("/x/tv/ugc/playurl?avid={0}&cid={1}&qn={2}&type=&otype=json&device=android&platform=android&mobi_app=android_tv_yst&build=102801&fnver=0&fnval=80", aid, cid, quality.ToString()); callback = Encoding.UTF8.GetString(MyWebClient.DownloadData("https://api.bilibili.com" + api)); //如果获取网站页面采用的是UTF-8,则使用这句 break; } } catch (WebException e) { Dialog.Show("无法下载," + e.Message); return(false); } MyWebClient.Dispose(); switch (Settings.useapi) { case 0: 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); break; case 1: if (callback == "") { Dialog.Show(string.Format("使用BiliPlus API出错!"), "获取错误"); return(false); } JSONCallback.BiliPlus.Player playerb = JsonConvert.DeserializeObject <JSONCallback.BiliPlus.Player>(callback); if (!playerb.accept_quality.Contains(quality)) { Console.WriteLine(string.Format("没有指定的画质 {0} ,最高画质为 {1}, 自动下载最高画质{1}", VideoQuality.Name(quality), VideoQuality.Name(playerb.accept_quality[0]))); quality = playerb.accept_quality[0]; return(GetDownloadUrls()); //我太懒了,直接递归吧 } foreach (JSONCallback.BiliPlus.DurlItem Item in playerb.durl) { DownloadUrl du = new DownloadUrl(); du.url = Item.url; du.size = Item.size; urls.Add(du); } return(true); break; case 2: if (callback.Contains("-400")) { return(false); } JSONCallback.FourKPlayer.Data playertv = JsonConvert.DeserializeObject <JSONCallback.FourKPlayer.Data>(callback); if (!playertv.accept_quality.Contains(quality)) { Console.WriteLine(string.Format("没有指定的画质 {0} ,最高画质为 {1}, 自动下载最高画质{1}", VideoQuality.Name(quality), VideoQuality.Name(playertv.accept_quality[0]))); quality = playertv.accept_quality[0]; return(GetDownloadUrls()); //我太懒了,直接递归吧 } foreach (JSONCallback.FourKPlayer.VideoItem Item in playertv.dash.video) { if (Item.id != quality) { continue; } DownloadUrl du = new DownloadUrl(); du.type = "mp4"; du.url = Item.base_url; du.size = -1; //暂不支持检测大小 urls.Add(du); du = new DownloadUrl(); du.type = "mp3"; du.url = playertv.dash.audio[1].base_url; du.size = -1; //暂不支持检测大小 urls.Add(du); return(true); } return(false); break; default: return(false); } } else { string callback = Other.GetHtml("https://www.bilibili.com/video/av" + aid, true); string json = Other.TextGetCenter("window.__playinfo__=", "</script>", callback); 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) { Console.WriteLine("4K获取出错,正在尝试降级后重试."); quality = VideoQuality.Q1080P60; return(GetDownloadUrls()); } } }
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("您无法继续选择,请直接开始下载"); } }
public Video(string vlink) { //第一步,bilibili网址转换 //注意垃圾spm! //例如 https://www.bilibili.com/bangumi/play/ss28615/?spm=3.0212 try { Uri uri = new Uri(vlink); if (uri.Host != "space.bilibili.com") { vlink = uri.AbsolutePath; vlink = vlink.TrimEnd('/'); vlink = vlink.Substring(vlink.LastIndexOf("/") + 1, vlink.Length - vlink.LastIndexOf("/") - 1); } else { vlink = uri.AbsolutePath; vlink = vlink.TrimEnd('/'); vlink = vlink.Substring(vlink.LastIndexOf("/") + 1, vlink.Length - vlink.LastIndexOf("/") - 1); int uid = 0; if (int.TryParse(vlink, out uid)) { ProcessUid(vlink); return; } else { Dialog.Show("用户ID错误,请检查链接格式!"); return; } } } catch (UriFormatException e) { if (vlink.Contains("/")) { Dialog.Show("链接格式错误!请复制完整的链接!"); return; } } //第二步判断格式 if (vlink.Contains("BV")) { Type = VideoType.AV; vlink = "av" + Video.ProcessBV(vlink); } vlink = vlink.ToLower(); if (vlink.Contains("av")) { Type = VideoType.AV; ProcessAV(vlink.Replace("av", "")); } else if (vlink.Contains("ep")) { Type = VideoType.SS; ProcessEP(vlink.Replace("ep", "")); } else if (vlink.Contains("ss")) { Type = VideoType.SS; ProcessSS(vlink.Replace("ss", "")); } else if (vlink.Contains("md")) { ProcessMD(vlink.Replace("md", "")); } else if (vlink.Contains("ml")) { ProcessML(vlink.Replace("ml", "")); } else if (vlink.Contains("uid")) { ProcessUid(vlink.Replace("uid", "")); } else { Dialog.Show("不是可以获取的格式,请检查格式是否正确"); } }
public Video(string vlink) { //第一步,bilibili网址转换 //注意垃圾spm! //例如 https://www.bilibili.com/bangumi/play/ss28615/?spm=3.0212 if (vlink.IndexOf("?") != -1) {//去除后置参数 vlink = vlink.Substring(0, vlink.IndexOf("?")); } if (vlink.Contains("space.bilibili.com")) { //https://space.bilibili.com/341151171 string uid = vlink.Substring(vlink.LastIndexOf("/") + 1, vlink.Length - 1 - vlink.LastIndexOf("/")); ProcessUid(uid); return; } vlink = Other.TextGetCenter("/", "/?", vlink); if (vlink.EndsWith("/")) { vlink = vlink.Substring(0, vlink.Length - 1); } vlink = Other.TextGetCenter("/", "?", vlink); //第二步判断格式 if (vlink.Contains("BV")) { Type = VideoType.AV; vlink = "av" + Video.ProcessBV(vlink); } vlink = vlink.ToLower(); if (vlink.Contains("av")) { Type = VideoType.AV; ProcessAV(vlink.Replace("av", "")); } else if (vlink.Contains("ep")) { Type = VideoType.SS; ProcessEP(vlink.Replace("ep", "")); } else if (vlink.Contains("ss")) { Type = VideoType.SS; ProcessSS(vlink.Replace("ss", "")); } else if (vlink.Contains("md")) { ProcessMD(vlink.Replace("md", "")); } else if (vlink.Contains("ml")) { ProcessML(vlink.Replace("ml", "")); } else if (vlink.Contains("uid")) { ProcessUid(vlink.Replace("uid", "")); } else { Dialog.Show("不是可以获取的格式,请检查格式是否正确"); } }