-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathApi.cs
134 lines (129 loc) · 5.36 KB
/
Api.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
122
123
124
125
126
127
128
129
130
131
132
133
134
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NicoSitePlugin
{
class ApiGetCommunityLivesException : Exception
{
public ApiGetCommunityLivesException()
{
}
}
class NotLoggedInException : Exception { }
static class Api
{
public static async Task<CommunityLiveInfo[]> GetCommunityLives(IDataSource server, CookieContainer cc, string communityId)
{
//以下のAPIだとON_AIRだけ取れる。
//https://com.nicovideo.jp/api/v1/communities/{communityIdWithoutCo}/lives/onair.json
//でも配信していないと
//{"meta":{"status":404,"error-code":"RESOURCE_NOT_FOUND","error-message":"このコミュニティは生放送中ではありません。"}}
//が返ってくる
var communityIdWithoutCo = communityId.Substring(2);
var url = $"https://com.nicovideo.jp/api/v1/communities/{communityIdWithoutCo}/lives.json";
var res = await server.GetAsync(url, cc);
dynamic d = JsonConvert.DeserializeObject(res);
if (d.meta.status != 200)
{
throw new ApiGetCommunityLivesException();
}
var lives = new List<CommunityLiveInfo>();
foreach (var liveDyn in d.data.lives)
{
var live = new CommunityLiveInfo
{
Description = (string)liveDyn.description,
LiveId = (string)liveDyn.id,
Status = (string)liveDyn.status,
Title = (string)liveDyn.title,
UserId = (long)liveDyn.user_id,
WatchUrl = (string)liveDyn.watch_url,
};
lives.Add(live);
}
return lives.ToArray();
}
public class CommunityLiveInfo
{
public string LiveId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Status { get; set; }
public long UserId { get; set; }
public string WatchUrl { get; set; }
}
/// <summary>
///
/// </summary>
/// <param name="server"></param>
/// <param name="cc"></param>
/// <returns></returns>
/// <exception cref="NotLoggedInException">未ログインの場合</exception>
/// <exception cref="SpecChangedException"></exception>
public static async Task<MyInfo> GetMyInfo(IDataSource server, CookieContainer cc)
{
var url = "https://www.nicovideo.jp/my";
var html = await server.GetAsync(url, cc);
var match = Regex.Match(html, "data-common-header=\"(.+)\"></div>");
if (!match.Success)
{
var matchHtmlTitle = Regex.Match(html, "<title>([^<]*)</title>");
var title = matchHtmlTitle.Groups[1].Value;
if (matchHtmlTitle.Success && title.Contains("ログイン"))
{
throw new NotLoggedInException();
}
else
{
throw new SpecChangedException(html);
}
}
var json = match.Groups[1].Value.Replace(""", "\"");
dynamic d = JsonConvert.DeserializeObject(json);
var isLogin = (bool)d.initConfig.user.isLogin;
var userId = (long)d.initConfig.user.id;
var isPremium = (bool)d.initConfig.user.isPremium;
var nickname = (string)d.initConfig.user.nickname;
var iconUrl = (string)d.initConfig.user.iconUrl;
return new MyInfo
{
IsLogin = isLogin,
Nickname = nickname,
IconUrl = iconUrl,
IsPremium = isPremium,
UserId = userId.ToString(),
};
}
/// <summary>
///
/// </summary>
/// <param name="dataSource"></param>
/// <param name="channelId">channelId/screenName</param>
/// <returns></returns>
internal static async Task<string> GetCurrentChannelLiveId(IDataSource dataSource, string channelId)
{
var url = "http://ch.nicovideo.jp/" + channelId;
var res = await dataSource.GetAsync(url);
var match = Regex.Match(res, "(data-live_status=\"onair\".+?</span>)", RegexOptions.Singleline);
if (!match.Success) return null;
var nowLiveSection = match.Groups[1].Value;
var liveId = Tools.ExtractLiveId(nowLiveSection);
return liveId;
}
/// <summary>
/// 指定されたコミュニティの現在の配信のIDを取得する
/// </summary>
/// <param name="dataSource"></param>
/// <param name="communityId">co\d+</param>
/// <returns>配信中であればその配信のID、そうでなければnull</returns>
internal static async Task<string> GetCurrentCommunityLiveId(IDataSource dataSource, string communityId, CookieContainer cc)
{
var lives = await GetCommunityLives(dataSource, cc, communityId);
return lives.FirstOrDefault(a => a.Status == "ON_AIR")?.LiveId;
}
}
}