This commit is contained in:
Genteure 2018-03-15 21:55:01 +08:00
parent b805a05542
commit 3e18db09ca
4 changed files with 110 additions and 2 deletions

View File

@ -7,6 +7,13 @@ namespace BililiveRecorder.Core
{
internal static class BililiveAPI
{
/// <summary>
/// 下载json并解析
/// </summary>
/// <param name="url">下载路径</param>
/// <returns>数据</returns>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="WebException"/>
public static JSONObject HttpGetJson(string url)
{
var c = new WebClient();
@ -18,6 +25,13 @@ namespace BililiveRecorder.Core
return j;
}
/// <summary>
/// 获取直播间播放地址
/// </summary>
/// <param name="roomid">原房间号</param>
/// <returns>FLV播放地址</returns>
/// <exception cref="WebException"/>
/// <exception cref="Exception"/>
public static string GetPlayUrl(int roomid)
{
string url = $@"https://api.live.bilibili.com/room/v1/Room/playUrl?cid={roomid}&quality=0&platform=web";
@ -25,6 +39,13 @@ namespace BililiveRecorder.Core
return data?["data"]?["durl"]?[0]?["url"]?.str ?? throw new Exception("没有直播播放地址");
}
/// <summary>
/// 获取直播间信息
/// </summary>
/// <param name="roomid">房间号(允许短号)</param>
/// <returns>直播间信息</returns>
/// <exception cref="WebException"/>
/// <exception cref="Exception"/>
public static RoomInfo GetRoomInfo(int roomid)
{
string url = $@"http://api.live.bilibili.com/AppRoom/index?room_id={roomid}&platform=android";

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BililiveRecorder.Core
{
public enum RecordStatus
{
Recording,
NotStreaming,
}
}

View File

@ -12,7 +12,10 @@ namespace BililiveRecorder.Core
{
public class RecordedRoom : INotifyPropertyChanged
{
public int RoomID;
public int roomID { get; private set; }
public RoomInfo roomInfo { get; private set; }
public RecordStatus Status;
public StreamMonitor streamMonitor;
public FlvStreamProcessor Processor; // FlvProcessor
@ -23,6 +26,8 @@ namespace BililiveRecorder.Core
{
Processor.BlockProcessed += Processor_BlockProcessed;
streamMonitor.StreamStatusChanged += StreamMonitor_StreamStatusChanged;
UpdateRoomInfo();
}
private void StreamMonitor_StreamStatusChanged(object sender, StreamStatusChangedArgs e)
@ -34,11 +39,76 @@ namespace BililiveRecorder.Core
}
}
private void _StartRecord()
public void StartRecord()
{
throw new NotImplementedException();
}
private void _StartRecord()
{
// throw new NotImplementedException();
if (webRequest != null)
{
//TODO: cleanup
webRequest = null;
}
if (Processor != null)
{
//TODO: cleanup
Processor = null;
}
string flv_path = BililiveAPI.GetPlayUrl(roomInfo.RealRoomid);
webRequest = WebRequest.CreateHttp(flv_path);
_SetupFlvRequest(webRequest);
HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;
if (response.StatusCode != HttpStatusCode.OK)
{
//TODO: Log
response.Close();
webRequest = null;
return;
}
}
private static void _SetupFlvRequest(HttpWebRequest r)
{
r.Accept = "*/*";
r.AllowAutoRedirect = true;
r.Connection = "keep-alive";
r.Referer = "https://live.bilibili.com";
r.Headers["Origin"] = "https://live.bilibili.com";
r.UserAgent = "Mozilla/5.0 BililiveRecorder/0.0.0.0 (+https://github.com/Bililive/BililiveRecorder;bliverec@genteure.com)";
}
void StartWebRequest()
{
webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), webRequest);
}
void FinishWebRequest(IAsyncResult result)
{
HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
}
public bool UpdateRoomInfo()
{
try
{
roomInfo = BililiveAPI.GetRoomInfo(roomID);
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return false;
}
}
// Called by API or GUI
public void Clip()
{

View File

@ -13,5 +13,10 @@ namespace BililiveRecorder.Core
{
throw new NotImplementedException();
}
public void CheckAfterSeconeds(uint seconds)
{
throw new NotImplementedException();
}
}
}