BililiveRecorder/BililiveRecorder.Core/BililiveAPI.cs

83 lines
3.4 KiB
C#
Raw Normal View History

2018-12-10 22:25:15 +08:00
using Newtonsoft.Json.Linq;
using System;
2018-03-13 23:04:08 +08:00
using System.Collections.Generic;
2018-03-21 22:41:34 +08:00
using System.Globalization;
2018-10-30 23:41:38 +08:00
using System.Linq;
2018-03-13 23:04:08 +08:00
using System.Net;
2018-03-21 22:41:34 +08:00
using System.Text.RegularExpressions;
2018-03-13 23:04:08 +08:00
namespace BililiveRecorder.Core
{
internal static class BililiveAPI
{
2018-03-15 21:55:01 +08:00
/// <summary>
/// 下载json并解析
/// </summary>
/// <param name="url">下载路径</param>
/// <returns>数据</returns>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="WebException"/>
2018-12-10 22:25:15 +08:00
public static JObject HttpGetJson(string url)
2018-03-13 23:04:08 +08:00
{
var c = new WebClient();
2018-11-09 10:50:46 +08:00
c.Headers.Add(HttpRequestHeader.UserAgent, Utils.UserAgent);
2018-03-13 23:04:08 +08:00
c.Headers.Add(HttpRequestHeader.Accept, "application/json, text/javascript, */*; q=0.01");
c.Headers.Add(HttpRequestHeader.Referer, "https://live.bilibili.com/");
var s = c.DownloadString(url);
2018-12-10 22:25:15 +08:00
var j = JObject.Parse(s);
2018-03-13 23:04:08 +08:00
return j;
}
2018-03-15 21:55:01 +08:00
/// <summary>
/// 获取直播间播放地址
/// </summary>
/// <param name="roomid">原房间号</param>
/// <returns>FLV播放地址</returns>
/// <exception cref="WebException"/>
/// <exception cref="Exception"/>
2018-03-13 23:04:08 +08:00
public static string GetPlayUrl(int roomid)
{
string url = $@"https://api.live.bilibili.com/room/v1/Room/playUrl?cid={roomid}&quality=0&platform=web";
2018-12-10 22:25:15 +08:00
if (HttpGetJson(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++)
{
2018-12-10 22:25:15 +08:00
urls.Add(array[i]?["url"]?.ToObject<string>());
2018-10-30 23:41:38 +08:00
}
var distinct = urls.Distinct().ToArray();
if (distinct.Length > 0)
{
return distinct[random.Next(0, distinct.Count() - 1)];
}
}
throw new Exception("没有直播播放地址");
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"/>
2018-03-13 23:04:08 +08:00
public static RoomInfo GetRoomInfo(int roomid)
{
2018-12-17 21:24:57 +08:00
string url = $@"https://api.live.bilibili.com/AppRoom/index?room_id={roomid}&platform=android";
2018-03-13 23:04:08 +08:00
var data = HttpGetJson(url);
var i = new RoomInfo()
{
2018-12-10 22:25:15 +08:00
DisplayRoomid = data?["data"]?["show_room_id"]?.ToObject<int>() ?? throw new Exception("未获取到直播间信息"),
RealRoomid = (int)(data?["data"]?["room_id"]?.ToObject<int>() ?? throw new Exception("未获取到直播间信息")),
Username = data?["data"]?["uname"]?.ToObject<string>() ?? throw new Exception("未获取到直播间信息"),
isStreaming = "LIVE" == (data?["data"]?["status"]?.ToObject<string>() ?? throw new Exception("未获取到直播间信息")),
2018-03-13 23:04:08 +08:00
};
return i;
}
2018-03-21 22:41:34 +08:00
private static readonly Regex rx = new Regex(@"\\[uU]([0-9A-Fa-f]{4})", RegexOptions.Compiled);
internal static string Decode(this string str) => rx.Replace(str, match => ((char)Int32.Parse(match.Value.Substring(2), NumberStyles.HexNumber)).ToString());
2018-10-30 23:41:38 +08:00
private static readonly Random random = new Random();
2018-03-13 23:04:08 +08:00
}
}