/// <summary> /// 获取直播分类列表 /// </summary> /// <returns></returns> public static async Task <LiveCategoryDataInfo> GetLiveCategoryInfo() { try { using HttpClient client = new HttpClient(); using (HttpResponseMessage response = await client.SendAsync(HttpMethod.Get, _getLiveCategoryApi, null, GlobalSettings.Bilibili.PCHeaders)) { string result = await response.Content.ReadAsStringAsync(); if (string.IsNullOrEmpty(result)) { throw new Exception("result data is null."); } LiveCategoryDataInfo resultModel = JsonHelper.DeserializeJsonToObject <LiveCategoryDataInfo>(result); if (resultModel.Code == 0) { return(resultModel); } else { throw new Exception($"Get live category failed. error code is {resultModel.Code}({resultModel.Msg})."); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// 获取全部分类 /// </summary> /// <param name="args"></param> /// <returns></returns> static async Task Main(string[] args) { try { GlobalSettings.LoadAll(); LiveCategoryDataInfo info = await LiveApi.GetLiveCategoryInfo(); if (info != null && info.Code == 0) { Console.WriteLine("-------------------------"); Console.WriteLine(" ID 名称 "); foreach (var bigCate in info.Data) { Console.WriteLine("-------------------------"); Console.WriteLine(bigCate.Name); Console.WriteLine("-------------------------"); foreach (var item in bigCate.List) { Console.WriteLine(String.Format("{0,-6} | {1,-20} ", item.id, item.name)); } } if (args != null && args.Length > 1) { string path = null; for (int i = 0; i < args.Length; i++) { if (args[i].ToLower() == "-md" && i < args.Length - 1) { path = args[i + 1]; (bool, string)result = OutputToMarkdownFile(info.Data, path); if (result.Item1) { Console.WriteLine("\n写入到文件成功。"); } else { Console.WriteLine($"\n写入到文件失败。{result.Item2}"); } } } } if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Environment.Exit(0); } else { Console.ReadKey(false); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// 更新直播间分类 /// </summary> /// <param name="user"></param> /// <param name="id"></param> /// <returns></returns> public static async Task <bool> UpdateLiveCategory(User user, string id) { try { LiveCategoryDataInfo liveCategoryData = await GetLiveCategoryInfo(); if (liveCategoryData == null || liveCategoryData.Code != 0 || liveCategoryData.Data == null) { return(false); } bool isOk = false; foreach (var item in liveCategoryData.Data) { foreach (var cate in item.List) { if (cate.id.Equals(id, StringComparison.OrdinalIgnoreCase)) { isOk = true; } } } if (!isOk) { return(false); } string roomId = await GetRoomId(user); var queries = new QueryCollection { { "room_id", roomId }, { "area_id", id }, { "csrf_token", user.Data.Csrf }, { "csrf", user.Data.Csrf } }; using (HttpResponseMessage response = await user.Handler.SendAsync(HttpMethod.Post, _updateLiveRoomCategory, queries, user.AppHeaders, user.Data.Cookie)) { ResultModel <IResultData> resultModel = await response.ConvertResultModel <IResultData>(); if (resultModel.Code == 0) { return(true); } else { throw new Exception($"Update live category failed. error code is {resultModel.Code}({resultModel.Msg})."); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// 获取全部分类 /// </summary> /// <param name="args"></param> /// <returns></returns> static async Task Main(string[] args) { try { GlobalSettings.LoadAll(); LiveCategoryDataInfo info = await LiveApi.GetLiveCategoryInfo(); if (info != null && info.Code == 0) { Console.WriteLine("-------------------------"); Console.WriteLine(" ID 名称 "); foreach (var bigCate in info.Data) { Console.WriteLine("-------------------------"); Console.WriteLine(bigCate.Name); Console.WriteLine("-------------------------"); foreach (var item in bigCate.List) { Console.WriteLine(String.Format("{0,-6} | {1,-20} ", item.id, item.name)); } } //生成GITHUB中表格 //foreach (var bigCate in info.Data) //{ // foreach (var item in bigCate.List) // { // Console.WriteLine($" | {item.id} | {item.name} | {item.parent_name} | "); // } //} if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Environment.Exit(0); } else { Console.ReadKey(false); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }