private async void connectToRoom() { if (actualRoomID > 0) { var connectresult = false; var trytime = 0; while (!connectresult) { if (trytime > 5) { break; } else { trytime++; } await Task.Delay(1000);// 稍等一下 ConsoleAssistance.WriteLine("[" + URLID + @"]正在连接"); connectresult = await main.ConnectAsync(actualRoomID); if (!connectresult && main.Error != null && isDebug)// 如果连接不成功并且出错了 { ConsoleAssistance.WriteLine("[" + URLID + @"][debug] 出错信息:" + main.Error.ToString(), ConsoleColor.Red); } } if (connectresult) { ConsoleAssistance.WriteLine("[" + URLID + @"]弹幕机连接成功!", ConsoleColor.Yellow); } else { ConsoleAssistance.WriteLine("[" + URLID + @"]连接失败", ConsoleColor.Red); //Environment.Exit(1); //stay } } else { ConsoleAssistance.WriteLine("[" + URLID + @"]ID非法", ConsoleColor.Red); //Environment.Exit(1); //stay } }
private int getActualRoomID(string urlID) { var roomWebPageUrl = "https://api.live.bilibili.com/room/v1/Room/get_info?id=" + urlID; var wc = new WebClient(); wc.Headers.Add("Accept: text/html"); wc.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"); wc.Headers.Add("Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,ja;q=0.4"); //发送HTTP请求获取真实房间号 string pageHtml; bool result = true; int id = 0; try { pageHtml = wc.DownloadString(roomWebPageUrl); var jsonResult = JObject.Parse(pageHtml); id = int.Parse(jsonResult["data"]["room_id"].ToString()); } catch (Exception e) { id = 0; result = false; } //WebClient wc = new WebClient(); //wc.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据 //Byte[] pageData = wc.DownloadData(@"https://live.bilibili.com/" + urlID); //从指定网站下载数据 ////string pageHtml = Encoding.Default.GetString(pageData); //如果获取网站页面采用的是GB2312,则使用这句 //string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句 //get actual room id if (!result) { ConsoleAssistance.WriteLine("获取真实Room ID失败,将使用URL ID作为连接参数", ConsoleColor.Red); var ex = int.TryParse(urlID, out id); if (ex == false) { Console.WriteLine("ID非法"); //Environment.Exit(1); //stay } } return(id); }
public LiveRoom(string id, bool isDebug, bool isActualRoomID) { main = new DanmakuLoader(isDebug); main.UrlID = id; this.URLID = id; this.isActualRoomId = isActualRoomID; this.isDebug = isDebug; //start ConsoleAssistance.WriteLine("[" + URLID + @"]初始化完毕!", ConsoleColor.Yellow); //binding event main.Disconnected += func_Disconnected; main.LogMessage += func_LogMessage; main.ReceivedDanmaku += func_ReceivedDanmaku; main.ReceivedRoomCount += func_ReceivedRoomCount; ConsoleAssistance.WriteLine("[" + URLID + @"]成功监测事件!", ConsoleColor.Yellow); //first connect firstConnect(id); }
static void func_NewDanmaku(object sender, NewDanmakuEventArgs e) { //record all dmRecord.Record(e.Msg); //select danmaku if (selectedDanmaku != "") { if (selectedWithRegex) { if (!Regex.IsMatch(e.Msg, selectedDanmaku)) { return; } } else { if (!e.Msg.Contains(selectedDanmaku)) { return; } } } //show lock (lockMsgCache) { if (isCanceled) { msgCache.Add(e.Msg); } else { ConsoleAssistance.WriteLine(e.Msg); } } }
static void Main(string[] args) { //get settings isRecord = !args.Contains("-nr"); isDebug = args.Contains("-debug"); isCanceled = false; //welcome title Console.Title = "bili-live-dm-console"; ConsoleAssistance.WriteLine("Welcome you to use bili-live-dm-console!", ConsoleColor.Yellow); //output debug if (isDebug) { ConsoleAssistance.WriteLine("bili-live-dm-console configuration", ConsoleColor.Yellow); ConsoleAssistance.WriteLine("Version:1.0.0", ConsoleColor.White); ConsoleAssistance.WriteLine("bililive_dm lib version:1.0.1.126", ConsoleColor.White); ConsoleAssistance.WriteLine("Workpath:" + Environment.CurrentDirectory, ConsoleColor.White); ConsoleAssistance.WriteLine("Platform:" + Enum.GetName(typeof(PlatformID), Environment.OSVersion.Platform), ConsoleColor.White); } //initialize ConsoleAssistance.WriteLine(@"=========="); ConsoleAssistance.WriteLine("正在启动...", ConsoleColor.Yellow); msgCache = new List <string>(); lockMsgCache = new object(); liveRoomList = new List <LiveRoom>(); //start dmRecord = new DanmakuRecord(isRecord); //record start dmRecord.Record(""); dmRecord.Record(DateTime.Now.ToString()); ConsoleAssistance.WriteLine(@"弹幕机初始化完毕!", ConsoleColor.Yellow); while (true) { var result = Console.ReadKey(true); if (result.Key == ConsoleKey.Tab) { isCanceled = true; ConsoleAssistance.Write(@"bili-live-dm-console > ", ConsoleColor.Green); var cache = Console.ReadLine(); ProcessCommand(cache); lock (lockMsgCache) { foreach (var item in msgCache) { //select remained danmaku if (selectedDanmaku != "") { if (selectedWithRegex) { if (!Regex.IsMatch(item, selectedDanmaku)) { continue; } } else { if (!item.Contains(selectedDanmaku)) { continue; } } } ConsoleAssistance.WriteLine(item); } msgCache.Clear(); } isCanceled = false; }//else pass } }
static void ProcessCommand(string str) { if (str == "") { return; } var result = CommandSplitter.SplitCommand(str); if (!result.IsFine) { ConsoleAssistance.WriteLine("无效的命令:" + result.CommandBugDescription, ConsoleColor.Magenta); return; } try { switch (result[0]) { case "ls": ConsoleAssistance.WriteLine("当前所有侦测的直播间", ConsoleColor.Magenta); foreach (var item in liveRoomList) { ConsoleAssistance.WriteLine(item.URLID + item.VisualUserCount, ConsoleColor.Magenta); } break; case "select": selectedDanmaku = result[1]; if (result[2] == "-re") { selectedWithRegex = true; } else { selectedWithRegex = false; } ConsoleAssistance.WriteLine("新的筛选规则已经应用", ConsoleColor.Magenta); break; case "c": case "connect": var newLiveRoom = new LiveRoom(result[1], isDebug, !result.Contain("-s")); newLiveRoom.NewMessage += func_NewDanmaku; liveRoomList.Add(newLiveRoom); dmRecord.Record("Add [" + result[1] + "] into list"); ConsoleAssistance.WriteLine("已添加新的直播间", ConsoleColor.Magenta); break; case "dc": case "disconnect": var isRemoved = false; foreach (var item in liveRoomList) { if (item.URLID == result[1]) { item.Disconnect(); liveRoomList.Remove(item); dmRecord.Record("Remove [" + result[1] + "] from list"); ConsoleAssistance.WriteLine("已移除直播间", ConsoleColor.Magenta); isRemoved = true; break; } } if (!isRemoved) { ConsoleAssistance.WriteLine("没有匹配的直播间可以移除", ConsoleColor.Magenta); } break; case "exit": foreach (var item in liveRoomList) { item.Disconnect(); } dmRecord.Close(); ConsoleAssistance.WriteLine("退出成功,感谢使用", ConsoleColor.Magenta); Environment.Exit(0); break; default: ConsoleAssistance.WriteLine("无匹配的命令", ConsoleColor.Magenta); break; } } catch (System.Exception) { ConsoleAssistance.WriteLine("命令不完整", ConsoleColor.Magenta); } }
private void func_LogMessage(object sender, LogMessageArgs e) { ConsoleAssistance.WriteLine("[" + URLID + @"][log]" + e.message, ConsoleColor.Yellow); }
private void func_Disconnected(object sender, DisconnectEvtArgs e) { ConsoleAssistance.WriteLine("[" + URLID + @"]连接被意外断开!正在准备自动重连", ConsoleColor.Yellow); connectToRoom(); }