完成B站收藏夹api

croire 3 years ago
parent 5a58a2e284
commit 0e9f12d3c6

@ -3,9 +3,6 @@ using DownKyi.Core.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DownKyi.Core.BiliApi.Favorites
{
@ -36,5 +33,110 @@ namespace DownKyi.Core.BiliApi.Favorites
}
}
/// <summary>
/// 查询用户创建的视频收藏夹
/// </summary>
/// <param name="mid">目标用户UID</param>
/// <param name="pn">页码</param>
/// <param name="ps">每页项数</param>
/// <returns></returns>
public static List<FavoritesMetaInfo> GetCreatedFavorites(long mid, int pn, int ps)
{
string url = $"https://api.bilibili.com/x/v3/fav/folder/created/list?up_mid={mid}&pn={pn}&ps={ps}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);
try
{
var favorites = JsonConvert.DeserializeObject<FavoritesListOrigin>(response);
if (favorites == null || favorites.Data == null || favorites.Data.List == null)
{ return null; }
return favorites.Data.List;
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("GetCreatedFavorites()发生异常: {0}", e);
LogManager.Error("FavoritesInfo", e);
return null;
}
}
/// <summary>
/// 查询所有的用户创建的视频收藏夹
/// </summary>
/// <param name="mid">目标用户UID</param>
/// <returns></returns>
public static List<FavoritesMetaInfo> GetAllCreatedFavorites(long mid)
{
List<FavoritesMetaInfo> result = new List<FavoritesMetaInfo>();
int i = 0;
while (true)
{
i++;
int ps = 50;
var data = GetCreatedFavorites(mid, i, ps);
if (data == null || data.Count == 0)
{ break; }
result.AddRange(data);
}
return result;
}
/// <summary>
/// 查询用户收藏的视频收藏夹
/// </summary>
/// <param name="mid">目标用户UID</param>
/// <param name="pn">页码</param>
/// <param name="ps">每页项数</param>
/// <returns></returns>
public static List<FavoritesMetaInfo> GetCollectedFavorites(long mid, int pn, int ps)
{
string url = $"https://api.bilibili.com/x/v3/fav/folder/collected/list?up_mid={mid}&pn={pn}&ps={ps}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);
try
{
var favorites = JsonConvert.DeserializeObject<FavoritesListOrigin>(response);
if (favorites == null || favorites.Data == null || favorites.Data.List == null)
{ return null; }
return favorites.Data.List;
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("GetCollectedFavorites()发生异常: {0}", e);
LogManager.Error("FavoritesInfo", e);
return null;
}
}
/// <summary>
/// 查询所有的用户收藏的视频收藏夹
/// </summary>
/// <param name="mid">目标用户UID</param>
/// <returns></returns>
public static List<FavoritesMetaInfo> GetAllCollectedFavorites(long mid)
{
List<FavoritesMetaInfo> result = new List<FavoritesMetaInfo>();
int i = 0;
while (true)
{
i++;
int ps = 50;
var data = GetCollectedFavorites(mid, i, ps);
if (data == null || data.Count == 0)
{ break; }
result.AddRange(data);
}
return result;
}
}
}

@ -0,0 +1,91 @@
using DownKyi.Core.BiliApi.Favorites.Models;
using DownKyi.Core.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace DownKyi.Core.BiliApi.Favorites
{
public static class FavoritesResource
{
/// <summary>
/// 获取收藏夹内容明细列表
/// </summary>
/// <param name="mediaId">收藏夹ID</param>
/// <param name="pn">页码</param>
/// <param name="ps">每页项数</param>
/// <returns></returns>
public static List<FavoritesMedia> GetFavoritesMedia(long mediaId, int pn, int ps)
{
string url = $"https://api.bilibili.com/x/v3/fav/resource/list?media_id={mediaId}&pn={pn}&ps={ps}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);
try
{
var resource = JsonConvert.DeserializeObject<FavoritesMediaResourceOrigin>(response);
if (resource == null || resource.Data == null || resource.Data.Medias == null)
{ return null; }
return resource.Data.Medias;
}
catch (Exception e)
{
Console.WriteLine("GetFavoritesMedia()发生异常: {0}", e);
LogManager.Error("FavoritesResource", e);
return null;
}
}
/// <summary>
/// 获取收藏夹内容明细列表(全部)
/// </summary>
/// <param name="mediaId">收藏夹ID</param>
/// <returns></returns>
public static List<FavoritesMedia> GetAllFavoritesMedia(long mediaId)
{
List<FavoritesMedia> result = new List<FavoritesMedia>();
int i = 0;
while (true)
{
i++;
int ps = 20;
var data = GetFavoritesMedia(mediaId, i, ps);
if (data == null || data.Count == 0)
{ break; }
result.AddRange(data);
}
return result;
}
/// <summary>
/// 获取收藏夹全部内容id
/// </summary>
/// <param name="mediaId"></param>
/// <returns></returns>
public static List<FavoritesMediaId> GetFavoritesMediaId(long mediaId)
{
string url = $"https://api.bilibili.com/x/v3/fav/resource/ids?media_id={mediaId}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);
try
{
var media = JsonConvert.DeserializeObject<FavoritesMediaIdOrigin>(response);
if (media == null || media.Data == null)
{ return null; }
return media.Data;
}
catch (Exception e)
{
Console.WriteLine("GetFavoritesMediaId()发生异常: {0}", e);
LogManager.Error("FavoritesResource", e);
return null;
}
}
}
}

@ -0,0 +1,17 @@
using DownKyi.Core.BiliApi.Models;
using Newtonsoft.Json;
namespace DownKyi.Core.BiliApi.Favorites.Models
{
public class FavStatus : BaseModel
{
[JsonProperty("collect")]
public long Collect { get; set; }
[JsonProperty("play")]
public long Play { get; set; }
[JsonProperty("thumb_up")]
public long ThumbUp { get; set; }
[JsonProperty("share")]
public long Share { get; set; }
}
}

@ -0,0 +1,19 @@
using DownKyi.Core.BiliApi.Models;
using Newtonsoft.Json;
namespace DownKyi.Core.BiliApi.Favorites.Models
{
public class FavUpper : BaseModel
{
[JsonProperty("mid")]
public long Mid { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("face")]
public string Face { get; set; }
[JsonProperty("followed")]
public bool Followed { get; set; }
// vip_type
// vip_statue
}
}

@ -0,0 +1,30 @@
using DownKyi.Core.BiliApi.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace DownKyi.Core.BiliApi.Favorites.Models
{
// https://api.bilibili.com/x/v3/fav/folder/collected/list
public class FavoritesListOrigin : BaseModel
{
//[JsonProperty("code")]
//public int Code { get; set; }
//[JsonProperty("message")]
//public string Message { get; set; }
//[JsonProperty("ttl")]
//public int Ttl { get; set; }
[JsonProperty("data")]
public FavoritesList Data { get; set; }
}
public class FavoritesList : BaseModel
{
[JsonProperty("count")]
public int Count { get; set; }
[JsonProperty("list")]
public List<FavoritesMetaInfo> List { get; set; }
[JsonProperty("has_more")]
public int HasMore { get; set; }
}
}

@ -0,0 +1,43 @@
using DownKyi.Core.BiliApi.Models;
using Newtonsoft.Json;
namespace DownKyi.Core.BiliApi.Favorites.Models
{
public class FavoritesMedia : BaseModel
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("type")]
public int Type { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("cover")]
public string Cover { get; set; }
[JsonProperty("intro")]
public string Intro { get; set; }
[JsonProperty("page")]
public int Page { get; set; }
[JsonProperty("duration")]
public long Duration { get; set; }
[JsonProperty("upper")]
public FavUpper Upper { get; set; }
// attr
[JsonProperty("cnt_info")]
public MediaStatus CntInfo { get; set; }
[JsonProperty("link")]
public string Link { get; set; }
[JsonProperty("ctime")]
public long Ctime { get; set; }
[JsonProperty("pubtime")]
public long Pubtime { get; set; }
[JsonProperty("fav_time")]
public long FavTime { get; set; }
[JsonProperty("bv_id")]
public string BvId { get; set; }
[JsonProperty("bvid")]
public string Bvid { get; set; }
// season
// ogv
// ugc
}
}

@ -0,0 +1,32 @@
using DownKyi.Core.BiliApi.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace DownKyi.Core.BiliApi.Favorites.Models
{
// https://api.bilibili.com/x/v3/fav/resource/ids
public class FavoritesMediaIdOrigin : BaseModel
{
//[JsonProperty("code")]
//public int Code { get; set; }
//[JsonProperty("message")]
//public string Message { get; set; }
//[JsonProperty("ttl")]
//public int Ttl { get; set; }
[JsonProperty("data")]
public List<FavoritesMediaId> Data { get; set; }
}
public class FavoritesMediaId : BaseModel
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("type")]
public int Type { get; set; }
[JsonProperty("bv_id")]
public string BvId { get; set; }
[JsonProperty("bvid")]
public string Bvid { get; set; }
}
}

@ -0,0 +1,30 @@
using DownKyi.Core.BiliApi.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace DownKyi.Core.BiliApi.Favorites.Models
{
// https://api.bilibili.com/x/v3/fav/resource/list
public class FavoritesMediaResourceOrigin : BaseModel
{
//[JsonProperty("code")]
//public int Code { get; set; }
//[JsonProperty("message")]
//public string Message { get; set; }
//[JsonProperty("ttl")]
//public int Ttl { get; set; }
[JsonProperty("data")]
public FavoritesMediaResource Data { get; set; }
}
public class FavoritesMediaResource : BaseModel
{
[JsonProperty("info")]
public FavoritesMetaInfo Info { get; set; }
[JsonProperty("medias")]
public List<FavoritesMedia> Medias { get; set; }
[JsonProperty("has_more")]
public int HasMore { get; set; }
}
}

@ -50,30 +50,4 @@ namespace DownKyi.Core.BiliApi.Favorites.Models
public int MediaCount { get; set; }
}
public class FavUpper : BaseModel
{
[JsonProperty("mid")]
public long Mid { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("face")]
public string Face { get; set; }
[JsonProperty("followed")]
public bool Followed { get; set; }
// vip_type
// vip_statue
}
public class FavStatus : BaseModel
{
[JsonProperty("collect")]
public long Collect { get; set; }
[JsonProperty("play")]
public long Play { get; set; }
[JsonProperty("thumb_up")]
public long ThumbUp { get; set; }
[JsonProperty("share")]
public long Share { get; set; }
}
}

@ -0,0 +1,15 @@
using DownKyi.Core.BiliApi.Models;
using Newtonsoft.Json;
namespace DownKyi.Core.BiliApi.Favorites.Models
{
public class MediaStatus : BaseModel
{
[JsonProperty("collect")]
public long Collect { get; set; }
[JsonProperty("play")]
public long Play { get; set; }
[JsonProperty("danmaku")]
public long Danmaku { get; set; }
}
}

@ -141,7 +141,15 @@
<Compile Include="BiliApi\Danmaku\DanmakuProtobuf.cs" />
<Compile Include="BiliApi\Danmaku\Models\BiliDanmaku.cs" />
<Compile Include="BiliApi\Favorites\FavoritesInfo.cs" />
<Compile Include="BiliApi\Favorites\FavoritesResource.cs" />
<Compile Include="BiliApi\Favorites\Models\FavoritesList.cs" />
<Compile Include="BiliApi\Favorites\Models\FavoritesMedia.cs" />
<Compile Include="BiliApi\Favorites\Models\FavoritesMediaId.cs" />
<Compile Include="BiliApi\Favorites\Models\FavoritesMediaResource.cs" />
<Compile Include="BiliApi\Favorites\Models\FavoritesMetaInfo.cs" />
<Compile Include="BiliApi\Favorites\Models\FavStatus.cs" />
<Compile Include="BiliApi\Favorites\Models\FavUpper.cs" />
<Compile Include="BiliApi\Favorites\Models\MediaStatus.cs" />
<Compile Include="BiliApi\Login\LoginHelper.cs" />
<Compile Include="BiliApi\Models\BaseModel.cs" />
<Compile Include="BiliApi\Login\LoginInfo.cs" />

Loading…
Cancel
Save