-
Notifications
You must be signed in to change notification settings - Fork 8
/
nico-live.cpp
447 lines (392 loc) · 12.1 KB
/
nico-live.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include "nico-live.hpp"
#include <chrono>
#include <ctime>
#include <curl/curl.h>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "nicookie.h"
#include "nico-live-api.hpp"
#include "nico-live-watcher.hpp"
#include "nicolive-log.h"
#include "nicolive.h"
namespace
{
// umsvs std::unordered_map<std::string, std::vector<std::string>
// umss std::unordered_map<std::string, std::string>
inline const std::string &get_umsvs_str(
const std::unordered_map<std::string, std::vector<std::string>> &data,
const std::unordered_map<std::string, std::string> &map,
const std::string &name, int pos = 0)
{
return data.at(map.at(name)).at(pos);
}
inline int get_umsvs_int(
const std::unordered_map<std::string, std::vector<std::string>> &data,
const std::unordered_map<std::string, std::string> &map,
const std::string &name, int pos = 0)
{
return std::stoi(get_umsvs_str(data, map, name, pos));
}
inline bool get_umsvs_bool(
const std::unordered_map<std::string, std::vector<std::string>> &data,
const std::unordered_map<std::string, std::string> &map,
const std::string &name, int pos = 0)
{
return !!get_umsvs_int(data, map, name, pos);
}
inline std::chrono::system_clock::time_point get_umsvs_time(
const std::unordered_map<std::string, std::vector<std::string>> &data,
const std::unordered_map<std::string, std::string> &map,
const std::string &name, int pos = 0)
{
return std::chrono::system_clock::from_time_t(
get_umsvs_int(data, map, name, pos));
}
/* const char *name */
inline const std::string &get_umsvs_str(
const std::unordered_map<std::string, std::vector<std::string>> &data,
const std::unordered_map<std::string, std::string> &map, const char *name,
int pos = 0)
{
return get_umsvs_str(data, map, std::string(name), pos);
}
inline int get_umsvs_int(
const std::unordered_map<std::string, std::vector<std::string>> &data,
const std::unordered_map<std::string, std::string> &map, const char *name,
int pos = 0)
{
return get_umsvs_int(data, map, std::string(name), pos);
}
inline bool get_umsvs_bool(
const std::unordered_map<std::string, std::vector<std::string>> &data,
const std::unordered_map<std::string, std::string> &map, const char *name,
int pos = 0)
{
return get_umsvs_bool(data, map, std::string(name), pos);
}
inline std::chrono::system_clock::time_point get_umsvs_time(
const std::unordered_map<std::string, std::vector<std::string>> &data,
const std::unordered_map<std::string, std::string> &map, const char *name,
int pos = 0)
{
return get_umsvs_time(data, map, std::string(name), pos);
}
}
NicoLive::NicoLive()
{
watcher = std::unique_ptr<NicoLiveWatcher>(new NicoLiveWatcher(this));
webApi = std::unique_ptr<NicoLiveApi>(new NicoLiveApi());
}
NicoLive::~NicoLive() {}
void NicoLive::setSession(const std::string &session)
{
this->session = session;
this->flags.session_valid = false;
this->flags.load_viqo = false;
this->webApi->setCookie("user_session", this->session);
}
void NicoLive::setSession(const char *session)
{
this->setSession(std::string(session));
}
void NicoLive::setAccount(const std::string &mail, const std::string &password)
{
this->mail = mail;
this->password = password;
this->flags.session_valid = false;
this->flags.load_viqo = false;
}
void NicoLive::setAccount(const char *mail, const char *password)
{
this->setAccount(std::string(mail), std::string(password));
}
void NicoLive::setEnabledAdjustBitrate(bool enabled)
{
this->flags.adjust_bitrate = enabled;
}
const std::string &NicoLive::getMail() const { return this->mail; }
const std::string &NicoLive::getPassword() const { return this->password; }
const std::string &NicoLive::getSession() const { return this->session; }
const std::string &NicoLive::getLiveId() const { return this->live_info.id; }
const std::string &NicoLive::getLiveUrl() const { return this->live_url; }
const std::string &NicoLive::getLiveKey() const { return this->live_key; }
long long NicoLive::getLiveBitrate() const { return this->live_info.bitrate; }
const std::string &NicoLive::getOnairLiveId() const
{
return this->onair_live_id;
}
bool NicoLive::enabledLive() const
{
auto now = std::chrono::system_clock::now();
return this->live_info.start_time <= now &&
this->live_info.end_time >= now;
}
NicoLive::LiveState NicoLive::getLiveState() const
{
auto now = std::chrono::system_clock::now();
if (now < getLiveStartTime()) {
return NicoLive::LiveState::BEFORE_START;
} else if (now <= getLiveEndTime()) {
return NicoLive::LiveState::ENABLE_LIVE;
} else {
return NicoLive::LiveState::AFTER_END;
}
}
std::chrono::system_clock::time_point NicoLive::getLiveStartTime() const
{
return this->live_info.start_time;
}
std::chrono::system_clock::time_point NicoLive::getLiveEndTime() const
{
return this->live_info.end_time;
}
std::chrono::system_clock::duration NicoLive::getRemainingStartTime() const
{
return this->live_info.start_time - this->live_info.server_time;
}
std::chrono::system_clock::duration NicoLive::getRemainingEndTime() const
{
return this->live_info.end_time - this->live_info.server_time;
}
std::chrono::system_clock::duration NicoLive::getRemainingLive() const
{
if (isOnair())
return this->live_info.end_time -
std::chrono::system_clock::now();
else
return std::chrono::milliseconds(0);
}
bool NicoLive::enabledStopBeforeEndTime() const
{
// TODO: Implement!
return true;
}
bool NicoLive::enabledStartBeforeStartTime() const
{
// TODO: Implement!
return true;
}
bool NicoLive::enabledAdjustBitrate() const
{
return this->flags.adjust_bitrate;
}
bool NicoLive::enabledSession() const { return this->flags.session_valid; }
bool NicoLive::isOnair() const { return this->flags.onair; }
void NicoLive::startStreaming()
{
this->onair_live_id = getLiveId();
this->flags.onair = true;
}
void NicoLive::stopStreaming()
{
this->onair_live_id = std::string();
this->flags.onair = false;
}
void NicoLive::startWatching() { this->watcher->start(); }
void NicoLive::stopWatching() { this->watcher->stop(); }
void NicoLive::nextSilentOnce() { this->flags.silent_once = true; }
bool NicoLive::silentOnce()
{
bool once = this->flags.silent_once;
this->flags.silent_once = false;
return once;
}
bool NicoLive::checkSession()
{
return (sitePubStat() || (siteLoginNLE() && sitePubStat()));
}
bool NicoLive::checkLive() { return siteLiveProf(); }
bool NicoLive::siteLogin()
{
if (this->mail.empty() || this->password.empty()) {
nicolive_log_warn("no mail or password");
return false;
}
bool result =
this->webApi->loginSiteNicolive(this->mail, this->password);
if (result) {
this->session = this->webApi->getCookie("user_session").c_str();
}
return result;
}
bool NicoLive::siteLoginNLE()
{
if (this->mail.empty() || this->password.empty()) {
nicolive_log_warn("no mail or password");
return false;
}
std::string ticket =
this->webApi->loginNicoliveEncoder(this->mail, this->password);
nicolive_log_debug("ticket: %20s", ticket.c_str());
if (!ticket.empty()) {
this->ticket = ticket;
return true;
} else {
return false;
}
}
bool NicoLive::sitePubStat()
{
nicolive_log_debug("session: %20s", this->session.c_str());
nicolive_log_debug("ticket: %20s", this->ticket.c_str());
bool useTicket = false;
if (this->session.empty()) {
if (this->siteLoginNLE()) {
useTicket = true;
} else {
nicolive_log_debug(
"this->session and this->ticket"
" are both empty.");
this->flags.onair = false;
clearLiveInfo();
return false;
}
}
const std::string statusXpath = "/getpublishstatus/@status";
const std::string errorCodeXpath =
"/getpublishstatus/error/code/text()";
const std::unordered_map<std::string, std::string> xpathMap = {
{"server_time", "/getpublishstatus/@time"},
{"id", "/getpublishstatus//stream/id/text()"},
{"exclude", "/getpublishstatus//stream/exclude/text()"},
{"base_time", "/getpublishstatus//stream/base_time/text()"},
{"open_time", "/getpublishstatus//stream/open_time/text()"},
{"start_time", "/getpublishstatus//stream/start_time/text()"},
{"end_time", "/getpublishstatus//stream/end_time/text()"},
{"url", "/getpublishstatus//rtmp/url/text()"},
{"stream", "/getpublishstatus//rtmp/stream/text()"},
{"ticket", "/getpublishstatus//rtmp/ticket/text()"},
{"bitrate", "/getpublishstatus//rtmp/bitrate/text()"},
};
std::unordered_map<std::string, std::vector<std::string>> data;
data[statusXpath] = std::vector<std::string>();
data[errorCodeXpath] = std::vector<std::string>();
for (auto &xpathPair : xpathMap) {
data[xpathPair.second] = std::vector<std::string>();
}
bool result = false;
if (useTicket) {
result =
this->webApi->getPublishStatusTicket(this->ticket, &data);
} else {
result = this->webApi->getPublishStatus(&data);
}
if (!result) {
nicolive_log_error("failed get publish status web page");
return false;
}
if (data[statusXpath].empty()) {
nicolive_log_error("faield get publish status");
return false;
}
bool success = false;
std::string status = data[statusXpath][0];
if (status == "ok") {
try {
this->live_info.server_time =
get_umsvs_time(data, xpathMap, "server_time");
this->live_info.id =
get_umsvs_str(data, xpathMap, "id");
this->live_info.exclude =
get_umsvs_bool(data, xpathMap, "exclude");
this->live_info.base_time =
get_umsvs_time(data, xpathMap, "base_time");
this->live_info.open_time =
get_umsvs_time(data, xpathMap, "open_time");
this->live_info.start_time =
get_umsvs_time(data, xpathMap, "start_time");
this->live_info.end_time =
get_umsvs_time(data, xpathMap, "end_time");
this->live_info.url =
get_umsvs_str(data, xpathMap, "url");
this->live_info.stream =
get_umsvs_str(data, xpathMap, "stream");
if (data.find(xpathMap.at("ticket")) != data.end() &&
data[xpathMap.at("ticket")].size() != 0) {
this->live_info.ticket =
data[xpathMap.at("ticket")].at(0);
} else {
this->live_info.ticket = std::string();
}
this->live_info.bitrate =
get_umsvs_int(data, xpathMap, "bitrate");
nicolive_log_info(
"live waku: %s", this->live_info.id.c_str());
success = true;
} catch (const std::out_of_range &err) {
nicolive_log_error("parse faild: %s", err.what());
clearLiveInfo();
success = false;
} catch (const std::invalid_argument &err) {
nicolive_log_error("parse faild: %s", err.what());
clearLiveInfo();
success = false;
}
} else if (status == "fail") {
clearLiveInfo();
std::string errorCode = "null";
if (!data[errorCodeXpath].empty()) {
errorCode = data[errorCodeXpath][0];
}
if (errorCode == "notfound") {
nicolive_log_info("no live waku");
success = true;
} else if (errorCode == "unknown") {
nicolive_log_warn("login session failed");
} else {
nicolive_log_error(
"unknow error code: %s", errorCode.c_str());
}
} else {
clearLiveInfo();
nicolive_log_error("unknow status: %s", status.c_str());
}
if (success) {
this->flags.session_valid = true;
} else {
this->flags.session_valid = false;
}
return success;
}
bool NicoLive::siteLiveProf()
{
if (this->live_info.id.empty()) {
nicolive_log_debug("this->live_info.id is empty.");
this->live_url = std::string();
this->live_key = std::string();
return false;
} else if (this->live_info.ticket.empty()) {
nicolive_log_info("found live url and key without ticket");
this->live_url = std::string();
this->live_url += this->live_info.url;
this->live_key = this->live_info.stream;
return true;
} else {
nicolive_log_info("found live url and key with ticket");
this->live_url = std::string();
this->live_url += this->live_info.url;
this->live_url += "?";
this->live_url += this->live_info.ticket;
this->live_key = this->live_info.stream;
return true;
}
}
bool NicoLive::loadViqoSettings()
{
const char *session = nicookie_get_session(NICOOKIE_APP_VIQO);
if (session == nullptr) {
const char *errMessage = nicookie_strerror(nicookie_errno);
nicolive_log_error("%s", errMessage);
return false;
}
this->setSession(session);
return true;
this->flags.load_viqo = true;
return true;
}
void NicoLive::clearLiveInfo()
{
this->live_info = decltype(this->live_info)();
}