BililiveRecorder/BililiveRecorder.Core/BililiveAPI.cs

212 lines
8.5 KiB
C#
Raw Normal View History

2018-12-10 22:25:15 +08:00
using Newtonsoft.Json.Linq;
using NLog;
2018-12-10 22:25:15 +08:00
using System;
2018-03-13 23:04:08 +08:00
using System.Collections.Generic;
2018-10-30 23:41:38 +08:00
using System.Linq;
2018-03-13 23:04:08 +08:00
using System.Net;
2019-08-22 01:26:18 +08:00
using System.Net.Http;
2019-10-31 22:02:19 +08:00
using System.Threading;
2019-08-22 01:26:18 +08:00
using System.Threading.Tasks;
2018-03-13 23:04:08 +08:00
namespace BililiveRecorder.Core
{
internal static class BililiveAPI
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2019-08-22 01:26:18 +08:00
private static readonly Random random = new Random();
2019-10-31 22:02:19 +08:00
private static readonly SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
private static HttpClient httpclient;
internal static Config.ConfigV1 Config = null; // TODO: 以后有空把整个 class 改成非 static 的然后用 DI 获取 config
2019-08-22 01:26:18 +08:00
static BililiveAPI()
{
2019-11-13 19:06:57 +08:00
httpclient = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };
2019-08-22 01:26:18 +08:00
httpclient.DefaultRequestHeaders.Add("Accept", "application/json, text/javascript, */*; q=0.01");
httpclient.DefaultRequestHeaders.Add("Referer", "https://live.bilibili.com/");
httpclient.DefaultRequestHeaders.Add("User-Agent", Utils.UserAgent);
}
2019-10-31 22:02:19 +08:00
public static async Task ApplyCookieSettings(string cookie_string)
{
await semaphoreSlim.WaitAsync();
try
{
if (!string.IsNullOrWhiteSpace(cookie_string))
{
try
{
CookieContainer cc = new CookieContainer();
cc.PerDomainCapacity = 300;
foreach (var t in cookie_string.Trim(' ', ';').Split(';').Select(x => x.Trim().Split(new[] { '=' }, 2)))
{
try
{
string v = string.Empty;
if (t.Length == 2)
{
v = System.Web.HttpUtility.UrlDecode(t[1]).Trim();
}
cc.Add(new Cookie(t[0].Trim(), v, "/", ".bilibili.com"));
}
catch (Exception) { }
}
var pclient = new HttpClient(handler: new HttpClientHandler
{
CookieContainer = cc
}, disposeHandler: true)
{
Timeout = TimeSpan.FromSeconds(5)
};
pclient.DefaultRequestHeaders.Add("Accept", "application/json, text/javascript, */*; q=0.01");
pclient.DefaultRequestHeaders.Add("Referer", "https://live.bilibili.com/");
pclient.DefaultRequestHeaders.Add("User-Agent", Utils.UserAgent);
httpclient = pclient;
return;
}
catch (Exception ex)
{
logger.Error(ex, "设置 Cookie 时发生错误");
}
}
var cleanclient = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
cleanclient.DefaultRequestHeaders.Add("Accept", "application/json, text/javascript, */*; q=0.01");
cleanclient.DefaultRequestHeaders.Add("Referer", "https://live.bilibili.com/");
cleanclient.DefaultRequestHeaders.Add("User-Agent", Utils.UserAgent);
httpclient = cleanclient;
}
finally
{
semaphoreSlim.Release();
}
}
2018-03-15 21:55:01 +08:00
/// <summary>
/// 下载json并解析
/// </summary>
/// <param name="url">下载路径</param>
/// <returns>数据</returns>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="WebException"/>
2019-08-22 01:26:18 +08:00
public static async Task<JObject> HttpGetJsonAsync(string url)
2018-03-13 23:04:08 +08:00
{
2019-10-31 22:02:19 +08:00
await semaphoreSlim.WaitAsync();
try
{
var s = await httpclient.GetStringAsync(url);
var j = JObject.Parse(s);
return j;
}
2019-11-13 19:06:57 +08:00
catch (TaskCanceledException)
{
return null;
}
2019-10-31 22:02:19 +08:00
finally
{
semaphoreSlim.Release();
}
2018-03-13 23:04:08 +08:00
}
2018-03-15 21:55:01 +08:00
/// <summary>
/// 获取直播间播放地址
/// </summary>
/// <param name="roomid">原房间号</param>
/// <returns>FLV播放地址</returns>
/// <exception cref="WebException"/>
/// <exception cref="Exception"/>
2019-08-22 01:26:18 +08:00
public static async Task<string> GetPlayUrlAsync(int roomid)
2018-03-13 23:04:08 +08:00
{
2019-08-14 21:24:48 +08:00
string url = $@"https://api.live.bilibili.com/room/v1/Room/playUrl?cid={roomid}&quality=4&platform=web";
if (Config.AvoidTxy)
2018-10-30 23:41:38 +08:00
{
// 尽量避开腾讯云
int attempt_left = 3;
while (true)
2018-10-30 23:41:38 +08:00
{
attempt_left--;
if ((await HttpGetJsonAsync(url))?["data"]?["durl"] is JArray all_jtoken && all_jtoken.Count > 0)
{
var all = all_jtoken.Select(x => x["url"].ToObject<string>()).ToArray();
var withoutTxy = all.Where(x => !x.Contains("txy.")).ToArray();
if (withoutTxy.Length > 0)
{
return withoutTxy[random.Next(0, withoutTxy.Length - 1)];
}
else if (attempt_left <= 0)
{
return all[random.Next(0, all.Length - 1)];
}
}
else
{
throw new Exception("没有直播播放地址");
}
2018-10-30 23:41:38 +08:00
}
}
else
{
// 随机选择一个 url
if ((await HttpGetJsonAsync(url))?["data"]?["durl"] is JArray array)
2018-10-30 23:41:38 +08:00
{
List<string> urls = new List<string>();
for (int i = 0; i < array.Count; i++)
{
urls.Add(array[i]?["url"]?.ToObject<string>());
}
var distinct = urls.Distinct().ToArray();
if (distinct.Length > 0)
{
return distinct[random.Next(0, distinct.Count() - 1)];
}
2018-10-30 23:41:38 +08:00
}
throw new Exception("没有直播播放地址");
2018-10-30 23:41:38 +08:00
}
2018-03-13 23:04:08 +08:00
}
2018-03-15 21:55:01 +08:00
/// <summary>
/// 获取直播间信息
/// </summary>
/// <param name="roomid">房间号(允许短号)</param>
/// <returns>直播间信息</returns>
/// <exception cref="WebException"/>
/// <exception cref="Exception"/>
2019-08-22 01:26:18 +08:00
public static async Task<RoomInfo> GetRoomInfoAsync(int roomid)
2018-03-13 23:04:08 +08:00
{
try
{
2019-08-22 01:26:18 +08:00
var room = await HttpGetJsonAsync($@"https://api.live.bilibili.com/room/v1/Room/get_info?id={roomid}");
if (room["code"].ToObject<int>() != 0)
{
2019-08-22 01:26:18 +08:00
logger.Warn("不能获取 {roomid} 的信息1: {errormsg}", roomid, room["message"]?.ToObject<string>());
return null;
}
var user = await HttpGetJsonAsync($@"https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid={roomid}");
if (user["code"].ToObject<int>() != 0)
{
2019-11-13 19:48:36 +08:00
logger.Warn("不能获取 {roomid} 的信息2: {errormsg}", roomid, user["message"]?.ToObject<string>());
return null;
}
var i = new RoomInfo()
{
2019-08-22 01:26:18 +08:00
ShortRoomId = room?["data"]?["short_id"]?.ToObject<int>() ?? throw new Exception("未获取到直播间信息"),
RoomId = room?["data"]?["room_id"]?.ToObject<int>() ?? throw new Exception("未获取到直播间信息"),
IsStreaming = 1 == (room?["data"]?["live_status"]?.ToObject<int>() ?? throw new Exception("未获取到直播间信息")),
UserName = user?["data"]?["info"]?["uname"]?.ToObject<string>() ?? throw new Exception("未获取到直播间信息"),
};
return i;
}
catch (Exception ex)
2018-03-13 23:04:08 +08:00
{
logger.Warn(ex, "获取直播间 {roomid} 的信息时出错", roomid);
throw;
}
2018-03-13 23:04:08 +08:00
}
}
}