using DownKyi.Core.BiliApi.Users.Models; using DownKyi.Core.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; namespace DownKyi.Core.BiliApi.Users { /// /// 用户空间信息 /// public static class UserSpace { /// /// 查询空间设置 /// /// /// public static SpaceSettings GetSpaceSettings(long mid) { string url = $"https://space.bilibili.com/ajax/settings/getSettings?mid={mid}"; string referer = "https://www.bilibili.com"; string response = WebClient.RequestWeb(url, referer); try { SpaceSettingsOrigin settings = JsonConvert.DeserializeObject(response); if (settings == null || settings.Data == null || !settings.Status) { return null; } return settings.Data; } catch (Exception e) { Utils.Debugging.Console.PrintLine("GetSpaceSettings()发生异常: {0}", e); LogManager.Error("UserSpace", e); return null; } } #region 投稿 /// /// 获取用户投稿视频的所有分区 /// /// 用户id /// public static List GetPublicationType(long mid) { int pn = 1; int ps = 1; SpacePublicationList publication = GetPublication(mid, pn, ps); return GetPublicationType(publication); } /// /// 获取用户投稿视频的所有分区 /// /// 用户id /// public static List GetPublicationType(SpacePublicationList publication) { if (publication == null || publication.Tlist == null) { return null; } List result = new List(); JObject typeList = JObject.Parse(publication.Tlist.ToString("N")); foreach (KeyValuePair item in typeList) { SpacePublicationListTypeVideoZone value = JsonConvert.DeserializeObject(item.Value.ToString()); result.Add(value); } return result; } /// /// 查询用户所有的投稿视频明细 /// /// 用户id /// 排序 /// 视频分区 /// 搜索关键词 /// public static List GetAllPublication(long mid, int tid = 0, PublicationOrder order = PublicationOrder.PUBDATE, string keyword = "") { List result = new List(); int i = 0; while (true) { i++; int ps = 100; SpacePublicationList data = GetPublication(mid, i, ps, tid, order, keyword); if (data == null || data.Vlist == null || data.Vlist.Count == 0) { break; } result.AddRange(data.Vlist); } return result; } /// /// 查询用户投稿视频明细 /// /// 用户id /// 页码 /// 每页的视频数 /// 排序 /// 视频分区 /// 搜索关键词 /// public static SpacePublicationList GetPublication(long mid, int pn, int ps, int tid = 0, PublicationOrder order = PublicationOrder.PUBDATE, string keyword = "") { string url = $"https://api.bilibili.com/x/space/arc/search?mid={mid}&pn={pn}&ps={ps}&order={order.ToString("G").ToLower()}&tid={tid}&keyword={keyword}"; string referer = "https://www.bilibili.com"; string response = WebClient.RequestWeb(url, referer); try { // 忽略play的值为“--”时的类型错误 JsonSerializerSettings settings = new JsonSerializerSettings { Error = (sender, args) => { if (Equals(args.ErrorContext.Member, "play") && args.ErrorContext.OriginalObject.GetType() == typeof(SpacePublicationListVideo)) { args.ErrorContext.Handled = true; } } }; SpacePublicationOrigin spacePublication = JsonConvert.DeserializeObject(response, settings); if (spacePublication == null || spacePublication.Data == null) { return null; } return spacePublication.Data.List; } catch (Exception e) { Utils.Debugging.Console.PrintLine("GetPublication()发生异常: {0}", e); LogManager.Error("UserSpace", e); return null; } } #endregion #region 频道 /// /// 查询用户频道列表 /// /// 用户id /// public static List GetChannelList(long mid) { string url = $"https://api.bilibili.com/x/space/channel/list?mid={mid}"; string referer = "https://www.bilibili.com"; string response = WebClient.RequestWeb(url, referer); try { SpaceChannelOrigin spaceChannel = JsonConvert.DeserializeObject(response); if (spaceChannel == null || spaceChannel.Data == null) { return null; } return spaceChannel.Data.List; } catch (Exception e) { Utils.Debugging.Console.PrintLine("GetChannelList()发生异常: {0}", e); LogManager.Error("UserSpace", e); return null; } } /// /// 查询用户频道中的所有视频 /// /// /// /// public static List GetAllChannelVideoList(long mid, long cid) { List result = new List(); int i = 0; while (true) { i++; int ps = 100; List data = GetChannelVideoList(mid, cid, i, ps); if (data == null || data.Count == 0) { break; } result.AddRange(data); } return result; } /// /// 查询用户频道中的视频 /// /// /// /// /// /// public static List GetChannelVideoList(long mid, long cid, int pn, int ps) { string url = $"https://api.bilibili.com/x/space/channel/video?mid={mid}&cid={cid}&pn={pn}&ps={ps}"; string referer = "https://www.bilibili.com"; string response = WebClient.RequestWeb(url, referer); try { SpaceChannelVideoOrigin spaceChannelVideo = JsonConvert.DeserializeObject(response); if (spaceChannelVideo == null || spaceChannelVideo.Data == null || spaceChannelVideo.Data.List == null) { return null; } return spaceChannelVideo.Data.List.Archives; } catch (Exception e) { Utils.Debugging.Console.PrintLine("GetChannelVideoList()发生异常: {0}", e); LogManager.Error("UserSpace", e); return null; } } #endregion #region 课程 /// /// 查询用户发布的课程列表 /// /// 目标用户UID /// 页码 /// 每页项数 /// public static List GetCheese(long mid, int pn, int ps) { string url = $"https://api.bilibili.com/pugv/app/web/season/page?mid={mid}&pn={pn}&ps={ps}"; string referer = "https://www.bilibili.com"; string response = WebClient.RequestWeb(url, referer); try { SpaceCheeseOrigin cheese = JsonConvert.DeserializeObject(response); if (cheese == null || cheese.Data == null || cheese.Data.Items == null) { return null; } return cheese.Data.Items; } catch (Exception e) { Utils.Debugging.Console.PrintLine("GetCheese()发生异常: {0}", e); LogManager.Error("UserSpace", e); return null; } } /// /// 查询用户发布的所有课程列表 /// /// 目标用户UID /// public static List GetAllCheese(long mid) { List result = new List(); int i = 0; while (true) { i++; int ps = 50; List data = GetCheese(mid, i, ps); if (data == null || data.Count == 0) { break; } result.AddRange(data); } return result; } #endregion #region 订阅 /// /// 查询用户追番(追剧)明细 /// /// 目标用户UID /// 查询类型 /// 页码 /// 每页项数 /// public static BangumiFollowData GetBangumiFollow(long mid, BangumiType type, int pn, int ps) { string url = $"https://api.bilibili.com/x/space/bangumi/follow/list?vmid={mid}&type={type:D}&pn={pn}&ps={ps}"; string referer = "https://www.bilibili.com"; string response = WebClient.RequestWeb(url, referer); try { BangumiFollowOrigin bangumiFollow = JsonConvert.DeserializeObject(response); if (bangumiFollow == null || bangumiFollow.Data == null) { return null; } return bangumiFollow.Data; } catch (Exception e) { Utils.Debugging.Console.PrintLine("GetBangumiFollow()发生异常: {0}", e); LogManager.Error("UserSpace", e); return null; } } /// /// 查询用户所有的追番(追剧)明细 /// /// 目标用户UID /// 查询类型 /// public static List GetAllBangumiFollow(long mid, BangumiType type) { List result = new List(); int i = 0; while (true) { i++; int ps = 30; BangumiFollowData data = GetBangumiFollow(mid, type, i, ps); if (data == null || data.List == null || data.List.Count == 0) { break; } result.AddRange(data.List); } return result; } #endregion } }