-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathliveChats.php
135 lines (114 loc) · 4.66 KB
/
liveChats.php
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
<?php
header('Content-Type: application/json; charset=UTF-8');
$liveTests = [];
include_once 'common.php';
$realOptions = [
'snippet',
'participants',
];
foreach ($realOptions as $realOption) {
$options[$realOption] = false;
}
if (isset($_GET['part'], $_GET['id']) && ($_GET['part'] != 'snippet' || isset($_GET['time']))) {
$part = $_GET['part'];
$parts = explode(',', $part, count($realOptions));
foreach ($parts as $part) {
if (!in_array($part, $realOptions)) {
dieWithJsonMessage("Invalid part $part");
} else {
$options[$part] = true;
}
}
if ($part == 'snippet' && !isPositiveInteger($_GET['time'])) {
dieWithJsonMessage('Invalid time');
}
$ids = $_GET['id'];
$realIds = explode(',', $ids);
verifyMultipleIds($realIds);
foreach ($realIds as $realId) {
if ((!isVideoId($realId))) {
dieWithJsonMessage('Invalid id');
}
}
echo getAPI($realIds);
} else if(!test()) {
dieWithJsonMessage('Required parameters not provided');
}
function getItem($id)
{
global $options;
$result = getJSONFromHTML("https://www.youtube.com/watch?v=$id");
$continuation = $result['contents']['twoColumnWatchNextResults']['conversationBar']['liveChatRenderer']['continuations'][0]['reloadContinuationData']['continuation'];
$rawData = [
'context' => [
'client' => [
'clientName' => 'WEB',
'clientVersion' => MUSIC_VERSION
]
],
'continuation' => strval($continuation),
'currentPlayerState' => [
'playerOffsetMs' => $_GET['time']
]
];
$opts = [
'http' => [
'header' => ['Content-Type: application/json'],
'method' => 'POST',
'content' => json_encode($rawData),
]
];
$result = getJSON('https://www.youtube.com/youtubei/v1/live_chat/get_live_chat_replay?key=' . UI_KEY, $opts);
$item = [
'kind' => 'youtube#video',
'etag' => 'NotImplemented',
'id' => $id
];
if ($options['snippet']) {
$snippet = [];
$actions = $result['continuationContents']['liveChatContinuation']['actions'];
foreach ($actions as $action) {
$replayChatItemAction = $action['replayChatItemAction'];
$liveChatTextMessageRenderer = $replayChatItemAction['actions'][0]['addChatItemAction']['item']['liveChatTextMessageRenderer'];
if ($liveChatTextMessageRenderer != null) {
$message = [
'id' => urldecode($liveChatTextMessageRenderer['id']),
'message' => $liveChatTextMessageRenderer['message']['runs'],
'authorName' => $liveChatTextMessageRenderer['authorName']['simpleText'],
'authorThumbnails' => $liveChatTextMessageRenderer['authorPhoto']['thumbnails'],
'timestampAbsoluteUsec' => intval($liveChatTextMessageRenderer['timestampUsec']),
'authorChannelId' => $liveChatTextMessageRenderer['authorExternalChannelId'],
'timestamp' => getIntFromDuration($liveChatTextMessageRenderer['timestampText']['simpleText']),
'videoOffsetTimeMsec' => intval($replayChatItemAction['videoOffsetTimeMsec'])
];
array_push($snippet, $message);
}
}
$item['snippet'] = $snippet;
}
if ($options['participants']) {
$participants = [];
$opts = [
'http' => [
'header' => ['User-Agent: ' . USER_AGENT],
]
];
$result = getJSONFromHTML("https://www.youtube.com/live_chat?continuation=$continuation", $opts, 'window["ytInitialData"]', '');
$participants = array_slice($result['continuationContents']['liveChatContinuation']['actions'], 1);
$item['participants'] = $participants;
}
return $item;
}
function getAPI($ids)
{
$items = [];
foreach ($ids as $id) {
array_push($items, getItem($id));
}
$answer = [
'kind' => 'youtube#videoListResponse',
'etag' => 'NotImplemented',
'items' => $items
];
return json_encode($answer, JSON_PRETTY_PRINT);
}