using DownKyi.Core.BiliApi.Users.Models;
using DownKyi.Core.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace DownKyi.Core.BiliApi.Users
{
///
/// 用户关系相关
///
public static class UserRelation
{
///
/// 查询用户粉丝明细
///
/// 目标用户UID
/// 页码
/// 每页项数
///
public static RelationFollow GetFollowers(long mid, int pn, int ps)
{
string url = $"https://api.bilibili.com/x/relation/followers?vmid={mid}&pn={pn}&ps={ps}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);
try
{
RelationFollowOrigin relationFollower = JsonConvert.DeserializeObject(response);
if (relationFollower == null || relationFollower.Data == null) { return null; }
return relationFollower.Data;
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("GetFollowers()发生异常: {0}", e);
LogManager.Error("UserRelation", e);
return null;
}
}
///
/// 查询用户所有的粉丝明细
///
/// 目标用户UID
///
public static List GetAllFollowers(long mid)
{
List result = new List();
int i = 0;
while (true)
{
i++;
int ps = 50;
RelationFollow data = GetFollowers(mid, i, ps);
if (data == null || data.List == null || data.List.Count == 0)
{ break; }
result.AddRange(data.List);
}
return result;
}
///
/// 查询用户关注明细
///
/// 目标用户UID
/// 页码
/// 每页项数
/// 排序方式
///
public static RelationFollow GetFollowings(long mid, int pn, int ps, FollowingOrder order = FollowingOrder.DEFAULT)
{
string orderType = "";
if (order == FollowingOrder.ATTENTION)
{
orderType = "attention";
}
string url = $"https://api.bilibili.com/x/relation/followings?vmid={mid}&pn={pn}&ps={ps}&order_type={orderType}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);
try
{
RelationFollowOrigin relationFollower = JsonConvert.DeserializeObject(response);
if (relationFollower == null || relationFollower.Data == null) { return null; }
return relationFollower.Data;
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("GetFollowings()发生异常: {0}", e);
LogManager.Error("UserRelation", e);
return null;
}
}
///
/// 查询用户所有的关注明细
///
/// 目标用户UID
/// 排序方式
///
public static List GetAllFollowings(long mid, FollowingOrder order = FollowingOrder.DEFAULT)
{
List result = new List();
int i = 0;
while (true)
{
i++;
int ps = 50;
RelationFollow data = GetFollowings(mid, i, ps, order);
if (data == null || data.List == null || data.List.Count == 0)
{ break; }
result.AddRange(data.List);
}
return result;
}
///
/// 查询悄悄关注明细
///
/// 页码
/// 每页项数
///
public static List GetWhispers(int pn, int ps)
{
string url = $"https://api.bilibili.com/x/relation/whispers?pn={pn}&ps={ps}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);
try
{
RelationWhisper relationWhisper = JsonConvert.DeserializeObject(response);
if (relationWhisper == null || relationWhisper.Data == null) { return null; }
return relationWhisper.Data;
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("GetWhispers()发生异常: {0}", e);
LogManager.Error("UserRelation", e);
return null;
}
}
///
/// 查询黑名单明细
///
/// 页码
/// 每页项数
///
public static List GetBlacks(int pn, int ps)
{
string url = $"https://api.bilibili.com/x/relation/blacks?pn={pn}&ps={ps}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);
try
{
RelationBlack relationBlack = JsonConvert.DeserializeObject(response);
if (relationBlack == null || relationBlack.Data == null) { return null; }
return relationBlack.Data;
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("GetBlacks()发生异常: {0}", e);
LogManager.Error("UserRelation", e);
return null;
}
}
#region 关注分组相关,只能查询当前登录账户的信息
///
/// 查询关注分组列表
///
///
public static List GetFollowingGroup()
{
string url = $"https://api.bilibili.com/x/relation/tags";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);
try
{
var followingGroup = JsonConvert.DeserializeObject(response);
if (followingGroup == null || followingGroup.Data == null) { return null; }
return followingGroup.Data;
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("GetFollowingGroup()发生异常: {0}", e);
LogManager.Error("UserRelation", e);
return null;
}
}
///
/// 查询关注分组明细
///
/// 分组ID
/// 页数
/// 每页项数
/// 排序方式
///
public static List GetFollowingGroupContent(int tagId, int pn, int ps, FollowingOrder order = FollowingOrder.DEFAULT)
{
string orderType = "";
if (order == FollowingOrder.ATTENTION)
{
orderType = "attention";
}
string url = $"https://api.bilibili.com/x/relation/tag?tagid={tagId}&pn={pn}&ps={ps}&order_type={orderType}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);
try
{
FollowingGroupContent content = JsonConvert.DeserializeObject(response);
if (content == null || content.Data == null) { return null; }
return content.Data;
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("GetFollowingGroupContent()发生异常: {0}", e);
LogManager.Error("UserRelation", e);
return null;
}
}
///
/// 查询所有的关注分组明细
///
/// 分组ID
/// 排序方式
///
public static List GetAllFollowingGroupContent(int tagId, FollowingOrder order = FollowingOrder.DEFAULT)
{
List result = new List();
int i = 0;
while (true)
{
i++;
int ps = 50;
var data = GetFollowingGroupContent(tagId, i, ps, order);
if (data == null || data.Count == 0)
{ break; }
result.AddRange(data);
}
return result;
}
#endregion
}
}