-
Notifications
You must be signed in to change notification settings - Fork 50
/
commentThreads.php
190 lines (175 loc) · 8.25 KB
/
commentThreads.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
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
<?php
header('Content-Type: application/json; charset=UTF-8');
// Stack Overflow source: https://stackoverflow.com/q/71186488
// use multiple lines, or not as they are not supposed to change
$commentThreadsTests = [
// How to disable people comments?
// Otherwise should have a private set of tests
//['part=snippet&videoId=UC4QobU6STFB0P71PMvOGN5A&order=viewCount', 'items/0/id/videoId', 'jNQXAC9IVRw'],
//['part=snippet,replies&commentId=UgzT9BA9uQhXw05Q2Ip4AaABAg&videoId=mWdFMNQBcjs', 'items/0/id/videoId', 'jNQXAC9IVRw'],
];
// example: https://youtu.be/mrJachWLjHU
// example: https://youtu.be/DyDfgMOUjCI
include_once 'common.php';
$realOptions = [
'snippet',
'replies',
];
foreach ($realOptions as $realOption) {
$options[$realOption] = false;
}
if (isset($_GET['part'])) {
$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;
}
}
$videoId = null;
if (isset($_GET['videoId'])) {
$videoId = $_GET['videoId'];
if (!isVideoId($videoId)) {
dieWithJsonMessage('Invalid videoId');
}
}
$commentId = null;
if (isset($_GET['id'])) {
$commentId = $_GET['id'];
if (!isCommentId($commentId)) {
dieWithJsonMessage('Invalid id');
}
}
$order = isset($_GET['order']) ? $_GET['order'] : 'relevance';
if (!in_array($order, ['relevance', 'time'])) {
dieWithJsonMessage('Invalid order');
}
$continuationToken = '';
if (isset($_GET['pageToken'])) {
$continuationToken = $_GET['pageToken'];
if (!isContinuationToken($continuationToken)) {
dieWithJsonMessage('Invalid pageToken');
}
}
echo getAPI($videoId, $commentId, $order, $continuationToken);
} else if(!test()) {
dieWithJsonMessage('Required parameters not provided');
}
function getAPI($videoId, $commentId, $order, $continuationToken, $simulatedContinuation = false)
{
if($commentId !== null)
{
$result = getJSONFromHTML("https://www.youtube.com/watch?v=$videoId&lc=$commentId");
$continuationToken = $result['contents']['twoColumnWatchNextResults']['results']['results']['contents'][3]['itemSectionRenderer']['contents'][0]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token'];
}
$continuationTokenProvided = $continuationToken != '';
if ($continuationTokenProvided) {
$rawData = [
'context' => [
'client' => [
'clientName' => 'WEB',
'clientVersion' => MUSIC_VERSION
]
],
'continuation' => $continuationToken
];
$opts = [
'http' => [
'method' => 'POST',
'header' => ['Content-Type: application/json'],
'content' => json_encode($rawData),
]
];
$result = getJSON('https://www.youtube.com/youtubei/v1/' . ($videoId !== null ? 'next' : 'browse') . '?key=' . UI_KEY, $opts);
if ($order === 'time' && $simulatedContinuation) {
$continuationToken = $result['onResponseReceivedEndpoints'][0]['reloadContinuationItemsCommand']['continuationItems'][0]['commentsHeaderRenderer']['sortMenu']['sortFilterSubMenuRenderer']['subMenuItems'][1]['serviceEndpoint']['continuationCommand']['token'];
return getAPI($videoId, $commentId, null, $continuationToken);
}
} else {
$result = getJSONFromHTML("https://www.youtube.com/watch?v=$videoId");
$continuationToken = end($result['contents']['twoColumnWatchNextResults']['results']['results']['contents'])['itemSectionRenderer']['contents'][0]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token'];
if($continuationToken != '') {
return getAPI($videoId, $commentId, $order, $continuationToken, true);
}
}
$answerItems = [];
$items = $result['frameworkUpdates']['entityBatchUpdate']['mutations'];
$isTopLevelComment = true;
foreach ($items as $item) {
$payload = $item['payload'];
if (array_key_exists('engagementToolbarStateEntityPayload', $payload)) {
$answerItems[$item['entityKey']]['snippet']['topLevelComment']['snippet']['creatorHeart'] = $payload['engagementToolbarStateEntityPayload']['heartState'] == 'TOOLBAR_HEART_STATE_HEARTED';
}
if (!array_key_exists('commentEntityPayload', $payload)) {
continue;
}
$comment = $payload['commentEntityPayload'];
$properties = $comment['properties'];
$author = $comment['author'];
$toolbar = $comment['toolbar'];
$publishedAt = $properties['publishedTime'];
$publishedAt = str_replace(' (edited)', '', $publishedAt, $count);
$internalSnippet = [
'content' => $properties['content']['content'],
'publishedAt' => $publishedAt,
'wasEdited' => $count > 0,
'authorChannelId' => $author['channelId'],
'authorHandle' => $author['displayName'],
'authorName' => str_replace('❤ by ', '', $toolbar['heartActiveTooltip']),
'authorAvatar' => $comment['avatar']['image']['sources'][0],
'isCreator' => $author['isCreator'],
'isArtist' => $author['isArtist'],
'likeCount' => getIntValue($toolbar['likeCountLiked']),
'totalReplyCount' => intval($toolbar['replyCount']),
'videoCreatorHasReplied' => false,
'isPinned' => false,
];
$commentId = $properties['commentId'];
$answerItem = [
'kind' => 'youtube#comment' . ($isTopLevelComment ? 'Thread' : ''),
'etag' => 'NotImplemented',
'id' => $commentId,
'snippet' => ($isTopLevelComment ? [
'topLevelComment' => [
'kind' => 'youtube#comment',
'etag' => 'NotImplemented',
'id' => $commentId,
'snippet' => $internalSnippet
]
] : $internalSnippet)
];
$answerItems[$properties['toolbarStateKey']] = $answerItem;
}
$continuationItems = $result['onResponseReceivedEndpoints'][1]['reloadContinuationItemsCommand']['continuationItems'];
foreach ($continuationItems as $item) {
$commentThreadRenderer = $item['commentThreadRenderer'];
$toolbarStateKey = $commentThreadRenderer['commentViewModel']['commentViewModel']['toolbarStateKey'];
// How to avoid repeating path?
if (doesPathExist($commentThreadRenderer, 'replies/commentRepliesRenderer/viewRepliesCreatorThumbnail')) {
$answerItems[$toolbarStateKey]['snippet']['topLevelComment']['snippet']['videoCreatorHasReplied'] = true;
}
if (doesPathExist($commentThreadRenderer, 'commentViewModel/commentViewModel/pinnedText')) {
$answerItems[$toolbarStateKey]['snippet']['topLevelComment']['snippet']['isPinned'] = true;
}
if ($toolbarStateKey !== null) {
$answerItems[$toolbarStateKey]['snippet']['topLevelComment']['snippet']['nextPageToken'] = $commentThreadRenderer['replies']['commentRepliesRenderer']['contents'][0]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token'];
}
}
$answerItems = array_values($answerItems);
$nextContinuationToken = $continuationItems[20]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token'];
$answer = [
'kind' => 'youtube#comment' . ($isTopLevelComment ? 'Thread' : '') . 'ListResponse',
'etag' => 'NotImplemented',
'pageInfo' => [
'totalResults' => intval($result['onResponseReceivedEndpoints'][0]['reloadContinuationItemsCommand']['continuationItems'][0]['commentsHeaderRenderer']['countText']['runs'][0]['text']),
'resultsPerPage' => $isTopLevelComment ? 20 : 10
]
];
if ($nextContinuationToken != '') {
$answer['nextPageToken'] = $nextContinuationToken;
}
$answer['items'] = $answerItems;
return json_encode($answer, JSON_PRETTY_PRINT);
}