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, long 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 合集和列表 /// /// 查询用户的合集和列表 /// /// /// 第几页 /// 每页的数量;最大值为20 /// public static SpaceSeasonsSeries GetSeasonsSeries(long mid, int pageNum, int pageSize) { // https://api.bilibili.com/x/polymer/space/seasons_series_list?mid=49246269&page_num=1&page_size=18 string url = $"https://api.bilibili.com/x/polymer/space/seasons_series_list?mid={mid}&page_num={pageNum}&page_size={pageSize}"; string referer = "https://www.bilibili.com"; string response = WebClient.RequestWeb(url, referer); try { SpaceSeasonsSeriesOrigin origin = JsonConvert.DeserializeObject(response); if (origin == null || origin.Data == null || origin.Data.ItemsLists == null) { return null; } return origin.Data.ItemsLists; } catch (Exception e) { Utils.Debugging.Console.PrintLine("GetSeasonsSeries()发生异常: {0}", e); LogManager.Error("UserSpace", e); return null; } } /// /// 查询用户的合集的视频详情 /// /// /// /// /// public static SpaceSeasonsDetail GetSeasonsDetail(long mid, long seasonId, int pageNum, int pageSize) { // https://api.bilibili.com/x/polymer/space/seasons_archives_list?mid=23947287&season_id=665&sort_reverse=false&page_num=1&page_size=30 string url = $"https://api.bilibili.com/x/polymer/space/seasons_archives_list?mid={mid}&season_id={seasonId}&page_num={pageNum}&page_size={pageSize}&sort_reverse=false"; string referer = "https://www.bilibili.com"; string response = WebClient.RequestWeb(url, referer); try { SpaceSeasonsDetailOrigin origin = JsonConvert.DeserializeObject(response); if (origin == null || origin.Data == null) { return null; } return origin.Data; } catch (Exception e) { Utils.Debugging.Console.PrintLine("GetSeasonsDetail()发生异常: {0}", e); LogManager.Error("UserSpace", e); return null; } } /// /// 查询用户的列表元数据 /// /// /// public static SpaceSeriesMetaData GetSeriesMeta(long seriesId) { // https://api.bilibili.com/x/series/series?series_id=1253087 string url = $"https://api.bilibili.com/x/series/series?series_id={seriesId}"; string referer = "https://www.bilibili.com"; string response = WebClient.RequestWeb(url, referer); try { SpaceSeriesMetaOrigin origin = JsonConvert.DeserializeObject(response); if (origin == null || origin.Data == null) { return null; } return origin.Data; } catch (Exception e) { Utils.Debugging.Console.PrintLine("GetSeriesMeta()发生异常: {0}", e); LogManager.Error("UserSpace", e); return null; } } /// /// 查询用户的列表的视频详情 /// /// /// /// /// /// public static SpaceSeriesDetail GetSeriesDetail(long mid, long seriesId, int pn, int ps) { // https://api.bilibili.com/x/series/archives?mid=27899754&series_id=1253087&only_normal=true&sort=desc&pn=1&ps=30 string url = $"https://api.bilibili.com/x/series/archives?mid={mid}&series_id={seriesId}&only_normal=true&sort=desc&pn={pn}&ps={ps}"; string referer = "https://www.bilibili.com"; string response = WebClient.RequestWeb(url, referer); try { SpaceSeriesDetailOrigin origin = JsonConvert.DeserializeObject(response); if (origin == null || origin.Data == null) { return null; } return origin.Data; } catch (Exception e) { Utils.Debugging.Console.PrintLine("GetSeriesDetail()发生异常: {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 } }