public string Start(string 开始后显示的备注) { DownIofo._wc = new WebClient(); DownIofo._wc.Headers.Add("Accept: */*"); DownIofo._wc.Headers.Add("User-Agent: " + Ver.UA); DownIofo._wc.Headers.Add("Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,ja;q=0.4"); DownIofo._wc.DownloadFileCompleted += 载完成事件; DownIofo._wc.DownloadProgressChanged += 载过程中事件; DownIofo.载状态 = true; if (!Directory.Exists(GetDirectoryName(DownIofo.文件保存路径))) { Directory.CreateDirectory(GetDirectoryName(DownIofo.文件保存路径)); } // ReSharper restore AssignNullToNotNullAttribute DownIofo.备注 = "等待接收直播数据流"; MMPU.判断网络路径是否存在 判断文件是否存在 = new MMPU.判断网络路径是否存在(); while (true) { Thread.Sleep(1000); switch (DownIofo.平台) { case "bilibili": if (!bilibili.根据房间号获取房间信息.是否正在直播(DownIofo.房间_频道号)) { DownIofo.载状态 = false; DownIofo.备注 = "该房间未直播"; return(null); } break; default: DownIofo.载状态 = false; DownIofo.备注 = "不支持的平台"; return(null); } if (判断文件是否存在.判断(DownIofo.载地址, DownIofo.平台)) { DownIofo.载状态 = false; DownIofo.备注 = "该房间未直播"; return(null); // break; } else { Thread.Sleep(1000); if (bilibili.根据房间号获取房间信息.是否正在直播(DownIofo.房间_频道号)) { DownIofo.载地址 = bilibili.根据房间号获取房间信息.载地址(DownIofo.房间_频道号); } } } DownIofo.开始时间 = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); DownIofo._wc.DownloadFileAsync(new Uri(DownIofo.载地址), DownIofo.文件保存路径); DownIofo.备注 = 开始后显示的备注; return(DownIofo.文件保存路径); }
/// <summary> /// 获取BILIBILI直播流下载地址 /// </summary> /// <param name="roomid">房间号</param> /// <param name="R">是否为重试</param> /// <returns></returns> public static string 载地址(string roomid) { roomid = 获取真实房间号(roomid); if (roomid == null) { InfoLog.InfoPrintf("房间号获取错误", InfoLog.InfoClass.Debug); return(null); } var apiUrl = "https://api.live.bilibili.com/room/v1/Room/playUrl?cid=" + roomid + "&otype=json&qn=10000&platform=web"; //访问API获取结果 var wc = new WebClient(); wc.Headers.Add("Accept: */*"); wc.Headers.Add("User-Agent: " + MMPU.UA.Ver.UA()); wc.Headers.Add("Accept-Language: zh-CN,zh;q=0.9,ja;q=0.8"); if (!string.IsNullOrEmpty(MMPU.Cookie)) { wc.Headers.Add("Cookie", MMPU.Cookie); } string resultString; try { resultString = wc.DownloadString(apiUrl); } catch (Exception e) { InfoLog.InfoPrintf("发送解析请求失败:" + e.Message, InfoLog.InfoClass.Debug); return(""); } //解析结果使用最高清晰度 try { MMPU.判断网络路径是否存在 判断文件是否存在 = new MMPU.判断网络路径是否存在(); string BBBC = ""; BBBC = (JObject.Parse(resultString)["data"]["durl"][0]["url"].ToString()); //BBBC = (JObject.Parse(resultString)["data"]["durl"][0]["url"].ToString() + "&platform=web").Replace("&pt=", "&pt=web") + "&pSession=" + Guid.NewGuid(); if (!判断文件是否存在.判断(BBBC, "bilibili", roomid)) { InfoLog.InfoPrintf("请求的开播房间当前推流数据为空,推测还未开播,等待数据流...:", InfoLog.InfoClass.Debug); BBBC = (JObject.Parse(resultString)["data"]["durl"][1]["url"].ToString()); } return(BBBC); } catch (Exception e) { InfoLog.InfoPrintf("视频流地址解析失败:" + e.Message, InfoLog.InfoClass.Debug); return(""); } }
/// <summary> /// 获取BILIBILI直播流下载地址 /// </summary> /// <param name="roomid">房间号</param> /// <param name="R">是否为重试</param> /// <returns></returns> public static string 载地址(string roomid) { roomid = 获取真实房间号(roomid); if (roomid == null) { InfoLog.InfoPrintf("房间号获取错误", InfoLog.InfoClass.Debug); return(null); } string resultString; try { resultString = MMPU.使用WC获取网络内容("https://api.live.bilibili.com/room/v1/Room/playUrl?cid=" + roomid + "&otype=json&qn=10000&platform=web"); } catch (Exception e) { InfoLog.InfoPrintf("发送解析请求失败:" + e.Message, InfoLog.InfoClass.Debug); return(""); } //解析结果使用最高清晰度 try { MMPU.判断网络路径是否存在 判断文件是否存在 = new MMPU.判断网络路径是否存在(); string BBBC = ""; BBBC = (JObject.Parse(resultString)["data"]["durl"][0]["url"].ToString()); //BBBC = (JObject.Parse(resultString)["data"]["durl"][0]["url"].ToString() + "&platform=web").Replace("&pt=", "&pt=web") + "&pSession=" + Guid.NewGuid(); if (!判断文件是否存在.判断(BBBC, "bilibili", roomid)) { InfoLog.InfoPrintf("请求的开播房间当前推流数据为空,推测还未开播,等待数据流...:", InfoLog.InfoClass.Debug); try { if ((JObject.Parse(resultString)["message"].ToString() == "房间已加密")) { InfoLog.InfoPrintf("房间已加密", InfoLog.InfoClass.载必要提示); return(""); } } catch (Exception) { } BBBC = (JObject.Parse(resultString)["data"]["durl"][1]["url"].ToString()); } DataCache.BilibiliApiCount++; return(BBBC); } catch (Exception e) { InfoLog.InfoPrintf("视频流地址解析失败:" + e.Message, InfoLog.InfoClass.系统错误信息); return(""); } }
public string Start(string 开始后显示的备注) { DownIofo.开始时间 = Gettime(); MMPU.DownList.Add(this); int a = 0; DownIofo.WC = new WebClient(); DownIofo.WC.Headers.Add("Accept: */*"); DownIofo.WC.Headers.Add("User-Agent: " + MMPU.UA.Ver.UA()); DownIofo.WC.Headers.Add("Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,ja;q=0.4"); DownIofo.WC.DownloadFileCompleted += 载完成事件; DownIofo.WC.DownloadProgressChanged += 载过程中事件; DownIofo.WC.Headers.Add("Accept-Encoding: gzip, deflate, br"); DownIofo.WC.Headers.Add("Cache-Control: max-age=0"); DownIofo.WC.Headers.Add("Sec-Fetch-Mode: navigate"); DownIofo.WC.Headers.Add("Sec-Fetch-Site: none"); DownIofo.WC.Headers.Add("Sec-Fetch-User: ?1"); DownIofo.WC.Headers.Add("Upgrade-Insecure-Requests: 1"); DownIofo.WC.Headers.Add("Cache-Control: max-age=0"); DownIofo.WC.Headers.Add("Referer: https://www.bilibili.com/"); if (!string.IsNullOrEmpty(MMPU.Cookie)) { DownIofo.WC.Headers.Add("Cookie", MMPU.Cookie); } if (!Directory.Exists(GetDirectoryName(DownIofo.文件保存路径))) { Directory.CreateDirectory(GetDirectoryName(DownIofo.文件保存路径)); } // ReSharper restore AssignNullToNotNullAttribute DownIofo.备注 = "等待接收直播数据流"; DownIofo.载状态 = true; MMPU.判断网络路径是否存在 判断文件是否存在 = new MMPU.判断网络路径是否存在(); while (true) { Thread.Sleep(5000); switch (DownIofo.平台) { case "bilibili": if (!bilibili.根据房间号获取房间信息.是否正在直播(DownIofo.房间_频道号, true)) { InfoLog.InfoPrintf(DownIofo.房间_频道号 + "房间:" + DownIofo.主播名称 + " 房间直播状态为False,取消建立新的下载任务", InfoLog.InfoClass.载必要提示); DownIofo.载状态 = false; DownIofo.备注 = "该房间未直播"; DownIofo.结束时间 = Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); MMPU.DownList.Remove(this); return(null); } break; case "主站视频": break; default: DownIofo.载状态 = false; DownIofo.结束时间 = Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); DownIofo.备注 = "不支持的平台"; MMPU.DownList.Remove(this); return(null); } if (判断文件是否存在.判断(DownIofo.载地址, DownIofo.平台, DownIofo.房间_频道号)) { break; } else { Thread.Sleep(5000); switch (DownIofo.平台) { case "bilibili": { if (bilibili.根据房间号获取房间信息.是否正在直播(DownIofo.房间_频道号, true)) { a++; if (判断文件是否存在.判断(DownIofo.载地址, DownIofo.平台, DownIofo.房间_频道号)) { //DownIofo.下载地址 = bilibili.根据房间号获取房间信息.下载地址(DownIofo.房间_频道号); break; } else { DownIofo.载地址 = bilibili.根据房间号获取房间信息.载地址(DownIofo.房间_频道号); if (判断文件是否存在.判断(DownIofo.载地址, DownIofo.平台, DownIofo.房间_频道号)) { break; } else { DownIofo.备注 = "等待房间推流..."; Thread.Sleep(30000); //return null; } } } else { InfoLog.InfoPrintf(DownIofo.房间_频道号 + "房间:" + DownIofo.主播名称 + " 房间未直播,下载任务取消", InfoLog.InfoClass.载必要提示); DownIofo.载状态 = false; DownIofo.备注 = "该房间未直播"; DownIofo.结束时间 = Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); MMPU.DownList.Remove(this); if (DownIofo.继承.是否为继承对象) { //MMPU.弹窗.Add(3000, "重连任务取消", DownIofo.房间_频道号 + ",该房间未直播"); } else { MMPU.弹窗.Add(3000, "下载任务取消", DownIofo.房间_频道号 + ",该房间未直播"); } return(null); } break; } case "主站视频": { break; } } } } DownIofo.开始时间 = Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); try { DownIofo.WC.DownloadFileTaskAsync(new Uri(DownIofo.载地址), DownIofo.文件保存路径); InfoLog.InfoPrintf(DownIofo.主播名称 + "开始直播,建立下载任务\r\n==============建立下载任务================\r\n主播名:" + DownIofo.主播名称 + "\r\n房间号:" + DownIofo.房间_频道号 + "\r\n标题:" + DownIofo.标题 + "\r\n开播时间:" + MMPU.Unix转换为DateTime(DownIofo.开始时间.ToString()) + "\r\n保存路径:" + DownIofo.文件保存路径 + "\r\n下载任务类型:" + (DownIofo.继承.是否为继承对象 ? "续下任务" : "新建下载任务") + "\r\n===============建立下载任务===============\r\n", InfoLog.InfoClass.载必要提示); if (MMPU.录制弹幕 && !DownIofo.继承.是否为继承对象) { DownIofo.弹幕储存流 = new StreamWriter(DownIofo.文件保存路径.Substring(0, DownIofo.文件保存路径.Length - 4) + (MMPU.弹幕录制种类 == 1 ? ".ass" : ".xml")); DownIofo.礼物储存流 = new StreamWriter(DownIofo.文件保存路径 + ".txt"); DownIofo.阿B直播流对象.Connect(int.Parse(DownIofo.房间_频道号)); DownIofo.阿B直播流对象.MessageReceived += Listener_MessageReceived; DownIofo.弹幕储存流.WriteLine(danmu.返回ASS字幕文件头(DownIofo.标题, DownIofo.房间_频道号, DownIofo.主播名称)); } } catch (WebException) { DownIofo.备注 = "主播未推流,已结束直播"; DownIofo.载状态 = false; return(null); } DownIofo.备注 = 开始后显示的备注; DownIofo.载状态 = true; return(DownIofo.文件保存路径); }
public string Start(string 开始后显示的备注) { int a = 0; DownIofo._wc = new WebClient(); DownIofo._wc.Headers.Add("Accept: */*"); DownIofo._wc.Headers.Add("User-Agent: " + Ver.UA); DownIofo._wc.Headers.Add("Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,ja;q=0.4"); DownIofo._wc.DownloadFileCompleted += 载完成事件; DownIofo._wc.DownloadProgressChanged += 载过程中事件; // rq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"; DownIofo._wc.Headers.Add("Accept-Encoding: gzip, deflate, br"); DownIofo._wc.Headers.Add("Accept-Encoding: gzip, deflate, br"); DownIofo._wc.Headers.Add("Cache-Control: max-age=0"); DownIofo._wc.Headers.Add("Sec-Fetch-Mode: navigate"); DownIofo._wc.Headers.Add("Sec-Fetch-Site: none"); DownIofo._wc.Headers.Add("Sec-Fetch-User: ?1"); DownIofo._wc.Headers.Add("Upgrade-Insecure-Requests: 1"); DownIofo._wc.Headers.Add("Cache-Control: max-age=0"); if (!Directory.Exists(GetDirectoryName(DownIofo.文件保存路径))) { Directory.CreateDirectory(GetDirectoryName(DownIofo.文件保存路径)); } // ReSharper restore AssignNullToNotNullAttribute DownIofo.备注 = "等待接收直播数据流"; MMPU.判断网络路径是否存在 判断文件是否存在 = new MMPU.判断网络路径是否存在(); while (true) { Thread.Sleep(1000); switch (DownIofo.平台) { case "bilibili": if (!bilibili.根据房间号获取房间信息.是否正在直播(DownIofo.房间_频道号)) { DownIofo.载状态 = false; DownIofo.备注 = "该房间未直播"; return(null); } break; case "主站视频": break; default: DownIofo.载状态 = false; DownIofo.备注 = "不支持的平台"; return(null); } if (判断文件是否存在.判断(DownIofo.载地址, DownIofo.平台)) { break; } else { Thread.Sleep(1000); if (bilibili.根据房间号获取房间信息.是否正在直播(DownIofo.房间_频道号)) { a++; if (判断文件是否存在.判断(DownIofo.载地址, DownIofo.平台)) { DownIofo.载地址 = bilibili.根据房间号获取房间信息.载地址(DownIofo.房间_频道号); break; } else { if (a > 3) { DownIofo.载状态 = false; DownIofo.备注 = "该房间未推送直播流"; return(null); } } } else { DownIofo.载状态 = false; DownIofo.备注 = "该房间未直播"; return(null); } } } DownIofo.开始时间 = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); DownIofo._wc.DownloadFileAsync(new Uri(DownIofo.载地址), DownIofo.文件保存路径); DownIofo.备注 = 开始后显示的备注; DownIofo.载状态 = true; return(DownIofo.文件保存路径); }
public static string 载地址(string roomid) { roomid = 获取真实房间号(roomid); if (roomid == null) { Console.WriteLine("房间号获取错误。"); return(null); } var apiUrl = "https://api.live.bilibili.com/room/v1/Room/playUrl?cid=" + roomid + "&otype=json&qn=10000&platform=web"; //访问API获取结果 var wc = new WebClient(); wc.Headers.Add("Accept: */*"); wc.Headers.Add("User-Agent: " + Ver.UA); wc.Headers.Add("Accept-Language: zh-CN,zh;q=0.9,ja;q=0.8"); if (!string.IsNullOrEmpty(MMPU.Cookie)) { wc.Headers.Add("Cookie", MMPU.Cookie); } string resultString; try { resultString = wc.DownloadString(apiUrl); } catch (Exception e) { Console.WriteLine("发送解析请求失败:" + e.Message); return(""); } //解析结果使用最高清晰度 try { //foreach (var item in JObject.Parse(resultString)["data"]["durl"]) //{ // Console.WriteLine(item["url"].ToString()); //} MMPU.判断网络路径是否存在 判断文件是否存在 = new MMPU.判断网络路径是否存在(); string BBBC = ""; int ls1 = 0; BBBC = (JObject.Parse(resultString)["data"]["durl"][0]["url"].ToString()); //BBBC = (JObject.Parse(resultString)["data"]["durl"][0]["url"].ToString() + "&platform=web").Replace("&pt=", "&pt=web") + "&pSession=" + Guid.NewGuid(); Console.WriteLine(BBBC); if (!判断文件是否存在.判断(BBBC, "bilibili")) { Console.WriteLine("使用PT_WEB失败"); BBBC = (JObject.Parse(resultString)["data"]["durl"][0]["url"].ToString()); } Console.WriteLine(BBBC); return(BBBC); } catch (Exception e) { Console.WriteLine("视频流地址解析失败:" + e.Message); return(""); } }
public string Start(string 开始后显示的备注) { int a = 0; DownIofo.WC = new WebClient(); DownIofo.WC.Headers.Add("Accept: */*"); DownIofo.WC.Headers.Add("User-Agent: " + MMPU.UA.Ver.UA()); DownIofo.WC.Headers.Add("Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,ja;q=0.4"); DownIofo.WC.DownloadFileCompleted += 载完成事件; DownIofo.WC.DownloadProgressChanged += 载过程中事件; // rq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"; DownIofo.WC.Headers.Add("Accept-Encoding: gzip, deflate, br"); DownIofo.WC.Headers.Add("Cache-Control: max-age=0"); DownIofo.WC.Headers.Add("Sec-Fetch-Mode: navigate"); DownIofo.WC.Headers.Add("Sec-Fetch-Site: none"); DownIofo.WC.Headers.Add("Sec-Fetch-User: ?1"); DownIofo.WC.Headers.Add("Upgrade-Insecure-Requests: 1"); DownIofo.WC.Headers.Add("Cache-Control: max-age=0"); DownIofo.WC.Headers.Add("Referer: https://www.bilibili.com/"); if (!string.IsNullOrEmpty(MMPU.Cookie)) { DownIofo.WC.Headers.Add("Cookie", MMPU.Cookie); } if (!Directory.Exists(GetDirectoryName(DownIofo.文件保存路径))) { Directory.CreateDirectory(GetDirectoryName(DownIofo.文件保存路径)); } // ReSharper restore AssignNullToNotNullAttribute DownIofo.备注 = "等待接收直播数据流"; DownIofo.载状态 = true; MMPU.判断网络路径是否存在 判断文件是否存在 = new MMPU.判断网络路径是否存在(); while (true) { Thread.Sleep(1000); switch (DownIofo.平台) { case "bilibili": if (!bilibili.根据房间号获取房间信息.是否正在直播(DownIofo.房间_频道号)) { InfoLog.InfoPrintf(DownIofo.房间_频道号 + "房间:" + DownIofo.主播名称 + " 房间直播状态为False,下载任务结束", InfoLog.InfoClass.载必要提示); DownIofo.载状态 = false; DownIofo.备注 = "该房间未直播"; DownIofo.结束时间 = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); return(null); } break; case "主站视频": break; default: DownIofo.载状态 = false; DownIofo.结束时间 = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); DownIofo.备注 = "不支持的平台"; return(null); } if (判断文件是否存在.判断(DownIofo.载地址, DownIofo.平台, DownIofo.房间_频道号)) { break; } else { Thread.Sleep(5000); switch (DownIofo.平台) { case "bilibili": { if (bilibili.根据房间号获取房间信息.是否正在直播(DownIofo.房间_频道号)) { a++; if (判断文件是否存在.判断(DownIofo.载地址, DownIofo.平台, DownIofo.房间_频道号)) { //DownIofo.下载地址 = bilibili.根据房间号获取房间信息.下载地址(DownIofo.房间_频道号); break; } else { DownIofo.载地址 = bilibili.根据房间号获取房间信息.载地址(DownIofo.房间_频道号); if (判断文件是否存在.判断(DownIofo.载地址, DownIofo.平台, DownIofo.房间_频道号)) { break; } else { DownIofo.备注 = "该房间未开播/推流或已加密"; //return null; } } } else { InfoLog.InfoPrintf(DownIofo.房间_频道号 + "房间:" + DownIofo.主播名称 + " 房间未直播,下载任务取消", InfoLog.InfoClass.载必要提示); DownIofo.载状态 = false; DownIofo.结束时间 = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); if (DownIofo.继承.是否为继承对象) { //MMPU.弹窗.Add(3000, "重连任务取消", DownIofo.房间_频道号 + ",该房间未直播"); } else { MMPU.弹窗.Add(3000, "下载任务取消", DownIofo.房间_频道号 + ",该房间未直播"); } DownIofo.备注 = "该房间未直播"; return(null); } break; } case "主站视频": { break; } } } } DownIofo.开始时间 = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); try { DownIofo.WC.DownloadFileTaskAsync(new Uri(DownIofo.载地址), DownIofo.文件保存路径); if (MMPU.录制弹幕 && !DownIofo.继承.是否为继承对象) { DownIofo.弹幕储存流 = new StreamWriter(DownIofo.文件保存路径 + ".ass"); DownIofo.礼物储存流 = new StreamWriter(DownIofo.文件保存路径 + ".txt"); DownIofo.阿B直播流对象.Connect(int.Parse(DownIofo.房间_频道号)); /*施工中(弹幕录制断流重连)*/ DownIofo.阿B直播流对象.MessageReceived += Listener_MessageReceived; DownIofo.弹幕储存流.WriteLine(danmu.返回ASS字幕文件头(DownIofo.标题, DownIofo.房间_频道号, DownIofo.主播名称)); } } catch (WebException) {} DownIofo.备注 = 开始后显示的备注; DownIofo.载状态 = true; return(DownIofo.文件保存路径); }