-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBiliLiveRoom.cs
121 lines (113 loc) · 5.17 KB
/
BiliLiveRoom.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using BiliApi.Auth;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
namespace BiliApi
{
//https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=2064239
/// <summary>
/// Bilibili直播间对象
/// </summary>
public class BiliLiveRoom
{
public int roomid;
public int shortid;
public string title;
public string cover;
public string[] tags;
public string keyframe;
public int lid;
public short status;
public static short STATUS_LIVE = 1;
public static short STATUS_OFFLINE = 0;
public static short STATUS_VEDIOPLAY = 2;
public LiveManagement manage;
public BiliSession sess;
public BiliLiveRoom(int roomid,BiliSession sess)
{
this.sess = sess;
string data = sess._get_with_cookies("https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=" + roomid);
JObject json = (JObject)JsonConvert.DeserializeObject(data);
roomid = json["data"]["room_info"].Value<int>("room_id");
shortid = json["data"]["room_info"].Value<int>("short_id");
title = json["data"]["room_info"].Value<string>("title");
cover = json["data"]["room_info"].Value<string>("cover");
tags = json["data"]["room_info"].Value<string>("tags").Split(',');
keyframe = json["data"]["room_info"].Value<string>("keyframe");
status = json["data"]["room_info"].Value<short>("live_status");
lid = json["data"]["room_info"].Value<int>("live_start_time");
this.roomid = roomid;
manage = new LiveManagement(this);
//keyframe
}
public BiliLiveRoom(int roomid, IAuthBase auth)
{
this.sess = new BiliSession(auth.GetLoginCookies());
string data = sess._get_with_cookies("https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=" + roomid);
JObject json = (JObject)JsonConvert.DeserializeObject(data);
roomid = json["data"]["room_info"].Value<int>("room_id");
shortid = json["data"]["room_info"].Value<int>("short_id");
title = json["data"]["room_info"].Value<string>("title");
cover = json["data"]["room_info"].Value<string>("cover");
tags = json["data"]["room_info"].Value<string>("tags").Split(',');
keyframe = json["data"]["room_info"].Value<string>("keyframe");
status = json["data"]["room_info"].Value<short>("live_status");
lid = json["data"]["room_info"].Value<int>("live_start_time");
this.roomid = roomid;
manage = new LiveManagement(this);
//keyframe
}
public BiliLiveRoom(JObject json)
{
roomid = json["data"]["room_info"].Value<int>("room_id");
shortid = json["data"]["room_info"].Value<int>("short_id");
title = json["data"]["room_info"].Value<string>("title");
cover = json["data"]["room_info"].Value<string>("cover");
tags = json["data"]["room_info"].Value<string>("tags").Split(',');
keyframe = json["data"]["room_info"].Value<string>("keyframe");
status = json["data"]["room_info"].Value<short>("live_status");
lid = json["data"]["room_info"].Value<int>("live_start_time");
//keyframe
}
public static short getLiveStatus(int roomid)
{
string data = BiliSession._get("https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=" + roomid);
JObject json = (JObject)JsonConvert.DeserializeObject(data);
return json["data"]["room_info"].Value<short>("live_status");
}
private DateTime lastsend_dmk;
public bool sendDanmaku(string message, int fsize = 25, int color = 16777215, int bubble = 0)
{
if (lastsend_dmk != null)
{
while ((DateTime.Now - lastsend_dmk).TotalSeconds < 3)
{
Thread.Sleep(0);
}
}
lastsend_dmk = DateTime.Now;
Dictionary<string, string> kvs = new Dictionary<string, string>();
CookieCollection ck = sess.CookieContext;
JObject job = new JObject();
kvs.Add("color", color.ToString());
kvs.Add("fontsize", fsize.ToString());
kvs.Add("mode", "1");
kvs.Add("msg", message);
kvs.Add("rnd", TimestampHandler.GetTimeStamp(DateTime.Now).ToString());
kvs.Add("roomid", roomid.ToString());
kvs.Add("bubble", bubble.ToString());
kvs.Add("csrf_token", ck["bili_jct"].Value);
kvs.Add("csrf", ck["bili_jct"].Value);
string response = sess._post_with_cookies("https://api.live.bilibili.com/msg/send", kvs);
if (response == "")
{
return false;
}
JObject raw_json = (JObject)JsonConvert.DeserializeObject(response);
return raw_json.Value<int>("code") == 0;
}
}
}