Skip to content
Permalink
Browse files

增加bvid查询

resolves #13
  • Loading branch information
MonoLogueChi committed Mar 23, 2020
1 parent aaf7e3b commit 9abca85302d91f0ff72a80abd5232256f78c2e2f
@@ -22,6 +22,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=artplayer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Bili/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=bilibili/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=bvid/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Danmu/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Danmus/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dplayer/@EntryIndexedValue">True</s:Boolean>
@@ -17,22 +17,30 @@ public class BiliBiliController : BiliBiliBaseController
[HttpGet]
[HttpGet("danmu")]
[HttpGet("danmu.{format}")]
public async Task<dynamic> Get(int cid, int aid, int p, string format)
public async Task<dynamic> Get(int cid, int aid, string bvid, int p, string format)
{
string[] date = Request.Query["date"];
if (date.Length == 0 && !(!string.IsNullOrEmpty(format) && format.Equals("json")))
{
HttpContext.Response.ContentType = "application/xml; charset=utf-8";
if (cid == 0 && aid != 0)
if (cid == 0)
{
p = p == 0 ? 1 : p;
cid = await Bilibili.GetCidAsync(aid, p);
if (aid != 0)
{
p = p == 0 ? 1 : p;
cid = await Bilibili.GetCidAsync(aid, p);
}
else if (!string.IsNullOrEmpty(bvid))
{
p = p == 0 ? 1 : p;
cid = await Bilibili.GetCidAsync(bvid, p);
}
}

return await Bilibili.GetDanmuRawByCidTaskAsync(cid);
}

var danmu = await Bilibili.GetDanmuAsync(cid, aid, p, date);
var danmu = await Bilibili.GetDanmuAsync(cid, aid, bvid, p, date);

if (!string.IsNullOrEmpty(format) && format.Equals("json"))
return new WebResult<IEnumerable<ArtPlayerDanmuData>>(danmu
@@ -16,22 +16,30 @@ public class BiliBiliController : BiliBiliBaseController
[HttpGet]
[HttpGet("danmu")]
[HttpGet("danmu.{format}")]
public async Task<dynamic> Get(int cid, int aid, int p, string format)
public async Task<dynamic> Get(int cid, int aid, string bvid, int p, string format)
{
string[] date = Request.Query["date"];
if (date.Length == 0 && !(!string.IsNullOrEmpty(format) && format.Equals("json")))
{
if (cid == 0 && aid != 0)
if (cid == 0)
{
p = p == 0 ? 1 : p;
cid = await Bilibili.GetCidAsync(aid, p);
if (aid != 0)
{
p = p == 0 ? 1 : p;
cid = await Bilibili.GetCidAsync(aid, p);
}
else if (!string.IsNullOrEmpty(bvid))
{
p = p == 0 ? 1 : p;
cid = await Bilibili.GetCidAsync(bvid, p);
}
}

HttpContext.Response.ContentType = "application/xml; charset=utf-8";
return await Bilibili.GetDanmuRawByCidTaskAsync(cid);
}

var danmu = await Bilibili.GetDanmuAsync(cid, aid, p, date);
var danmu = await Bilibili.GetDanmuAsync(cid, aid, bvid, p, date);

if (!string.IsNullOrEmpty(format) && format.Equals("json"))
return new WebResult<IEnumerable<BaseDanmuData>>(danmu.ToDanmuDataBases());
@@ -13,11 +13,11 @@ public class BiliBiliController : BiliBiliBaseController
public BiliBiliController(BiliBiliHelp bilibili) : base(bilibili) { }

[HttpGet("bilibili")]
public async Task<DplayerWebResult> Get(int cid, int aid, int p)
public async Task<DplayerWebResult> Get(int cid, int aid, string bvid, int p)
{
string[] date = Request.Query["date"];
HttpContext.Request.Headers["Accept"] = "application/json";
var result = await Bilibili.GetDanmuAsync(cid, aid, p, date);
var result = await Bilibili.GetDanmuAsync(cid, aid, bvid, p, date);
return new DplayerWebResult(result.ToDanmuDataBases().ToArray());
}
}
@@ -4,22 +4,31 @@ namespace Danmu.Model.Danmu.BiliBili
{
public class BiliBiliPage
{
/// <summary>
/// 分P
/// </summary>
[JsonPropertyName("page")]
public int Page { get; set; }
[JsonPropertyName("code")]
public int Code { get; set; }

/// <summary>
/// 标题
/// </summary>
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("data")]
public DataObj[] Data { get; set; }

/// <summary>
/// cid
/// </summary>
[JsonPropertyName("cid")]
public int Cid { get; set; }
public class DataObj
{
/// <summary>
/// 分P
/// </summary>
[JsonPropertyName("page")]
public int Page { get; set; }

/// <summary>
/// 标题
/// </summary>
[JsonPropertyName("part")]
public string Part { get; set; }

/// <summary>
/// cid
/// </summary>
[JsonPropertyName("cid")]
public int Cid { get; set; }
}
}
}
@@ -17,8 +17,8 @@ private async Task<byte[]> GetBiliBiliPageRawAsync(string url)
var key = Md5.GetMd5(url);
return await _cache.GetOrCreateCache(key, TimeSpan.FromMinutes(_setting.CidCacheTime), async () =>
{
var gzipClient = _httpClientFactory.CreateClient(Gzip);
var response = await gzipClient.GetAsync(url);
var httpClient = _httpClientFactory.CreateClient(Deflate);
var response = await httpClient.GetAsync(url);
if (response.IsSuccessStatusCode) return await response.Content.ReadAsByteArrayAsync();
return new byte[0];
});
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Danmu.Model.Danmu.BiliBili;
@@ -19,21 +20,50 @@ public async Task<int> GetCidAsync(int aid, int p)
return GetCid(pages, p);
}

/// <summary>
/// 获取Cid
/// </summary>
/// <param name="bvid"></param>
/// <param name="p"></param>
/// <returns></returns>
public async Task<int> GetCidAsync(string bvid, int p)
{
var pages = await GetBiliBiliPageAsync(bvid);
return GetCid(pages, p);
}

/// <summary>
/// 获取视频Cid和分P信息
/// </summary>
/// <param name="aid">视频的aid</param>
/// <returns>Page数据</returns>
private async Task<BiliBiliPage[]> GetBiliBiliPageAsync(int aid)
private async Task<BiliBiliPage> GetBiliBiliPageAsync(int aid)
{
var raw = await GetBiliBiliPageRawAsync($"https://api.bilibili.com/x/player/pagelist?aid={aid}");
if (raw.Length != 0)
{
var pages = JsonSerializer.DeserializeAsync<BiliBiliPage>(new MemoryStream(raw));
return await pages;
}

return new BiliBiliPage();
}

/// <summary>
/// 获取视频Cid和分P信息
/// </summary>
/// <param name="bvid"></param>
/// <returns></returns>
private async Task<BiliBiliPage> GetBiliBiliPageAsync(string bvid)
{
var raw = await GetBiliBiliPageRawAsync($"https://www.bilibili.com/widget/getPageList?aid={aid}");
var raw = await GetBiliBiliPageRawAsync($"https://api.bilibili.com/x/player/pagelist?bvid={bvid}");
if (raw.Length != 0)
{
var pages = JsonSerializer.DeserializeAsync<BiliBiliPage[]>(new MemoryStream(raw));
var pages = JsonSerializer.DeserializeAsync<BiliBiliPage>(new MemoryStream(raw));
return await pages;
}

return new BiliBiliPage[0];
return new BiliBiliPage();
}

/// <summary>
@@ -42,12 +72,9 @@ private async Task<BiliBiliPage[]> GetBiliBiliPageAsync(int aid)
/// <param name="pages">Page数据</param>
/// <param name="p">分p</param>
/// <returns>cid</returns>
private int GetCid(BiliBiliPage[] pages, int p)
private int GetCid(BiliBiliPage pages, int p)
{
var cid = 0;
foreach (var page in pages)
if (page.Page == p)
cid = page.Cid;
var cid = pages.Code == 0 ? pages.Data.Where(e => e.Page == p).Select(s => s.Cid).FirstOrDefault() : 0;
return cid;
}
}
@@ -11,12 +11,13 @@ namespace Danmu.Utils.BiliBili
{
public partial class BiliBiliHelp
{
private readonly HttpClientCacheDao _cache;
private readonly bool _canGetHistory;
private readonly IHttpClientFactory _httpClientFactory;
private readonly HttpClientCacheDao _cache;
private readonly BiliBiliSetting _setting;

public BiliBiliHelp(AppConfiguration appConfiguration, IHttpClientFactory httpClientFactory, HttpClientCacheDao biliBiliCache)
public BiliBiliHelp(AppConfiguration appConfiguration, IHttpClientFactory httpClientFactory,
HttpClientCacheDao biliBiliCache)
{
_httpClientFactory = httpClientFactory;
_setting = appConfiguration.GetAppSetting().BiliBiliSetting;
@@ -43,7 +44,7 @@ public async Task<Stream> GetDanmuRawByCidTaskAsync(int cid)
/// <returns></returns>
public async Task<DanmuDataBiliBili> GetDanmuAsync(int cid, string[] date)
{
if (!_canGetHistory) return await GetDanmuAsync(cid, 0, 1, new string[0]);
if (!_canGetHistory) return await GetDanmuAsync(cid, 0, null, 1, new string[0]);
var a = Task.Run(() => date.Select(async s =>
{
var b = await GetDanmuRawAsync(
@@ -62,15 +63,24 @@ public async Task<DanmuDataBiliBili> GetDanmuAsync(int cid, string[] date)
/// </summary>
/// <param name="cid"></param>
/// <param name="aid"></param>
/// <param name="bvid"></param>
/// <param name="p"></param>
/// <param name="date"></param>
/// <returns></returns>
public async Task<DanmuDataBiliBili> GetDanmuAsync(int cid, int aid, int p, string[] date)
public async Task<DanmuDataBiliBili> GetDanmuAsync(int cid, int aid, string bvid, int p, string[] date)
{
if (cid == 0 && aid != 0)
if (cid == 0)
{
p = p == 0 ? 1 : p;
cid = await GetCidAsync(aid, p);
if (aid != 0)
{
p = p == 0 ? 1 : p;
cid = await GetCidAsync(aid, p);
}
else if (!string.IsNullOrEmpty(bvid))
{
p = p == 0 ? 1 : p;
cid = await GetCidAsync(bvid, p);
}
}

return cid == 0
@@ -14,12 +14,13 @@ variables:
version: "1.0.0"

steps:
- checkout: self
fetchDepth: 1

- task: UseNode@1
inputs:
version: "12.x"



- task: CmdLine@2
inputs:
script: |
@@ -47,7 +48,7 @@ steps:
CLI_VERSION=`git describe --tags`
sed -i "s/<PublishReadyToRun>false/<PublishReadyToRun>true/g" $(Build.SourcesDirectory)/Danmu/Danmu.csproj
sed -i "s/<Version>1.0.0/<Version>`echo ${CLI_VERSION}`/g" $(Build.SourcesDirectory)/CommandLine/Danmu.CommandLine.csproj
- task: DotNetCoreCLI@2
inputs:
command: publish

0 comments on commit 9abca85

Please sign in to comment.
You can’t perform that action at this time.