-
Notifications
You must be signed in to change notification settings - Fork 52
/
community.php
125 lines (104 loc) · 3.8 KB
/
community.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
<?php
header('Content-Type: application/json; charset=UTF-8');
include_once 'common.php';
includeOnceProtos(['Browse', 'SubBrowse']);
$realOptions = [
'snippet',
];
foreach ($realOptions as $realOption) {
$options[$realOption] = false;
}
if (isset($_GET['part'], $_GET['id'], $_GET['channelId'])) {
$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;
}
}
$postId = $_GET['id'];
if (!isPostId($postId)) {
dieWithJsonMessage('Invalid postId');
}
$channelId = $_GET['channelId'];
if (!isChannelId($channelId)) {
dieWithJsonMessage('Invalid channelId');
}
$order = isset($_GET['order']) ? $_GET['order'] : 'relevance';
if (!in_array($order, ['relevance', 'time'])) {
dieWithJsonMessage('Invalid order');
}
echo getAPI($postId, $channelId, $order);
} else if(!test()) {
dieWithJsonMessage('Required parameters not provided');
}
function implodeArray($anArray, $separator)
{
return array_map(fn($k, $v) => "${k}${separator}${v}", array_keys($anArray), array_values($anArray));
}
function getAPI($postId, $channelId, $order)
{
$currentTime = time();
$SAPISID = 'CENSORED';
$__Secure_3PSID = 'CENSORED';
$ORIGIN = 'https://www.youtube.com';
$SAPISIDHASH = "${currentTime}_" . sha1("$currentTime $SAPISID $ORIGIN");
$subBrowse = new \SubBrowse();
$subBrowse->setPostId($postId);
$browse = new \Browse();
$browse->setEndpoint('community');
$browse->setSubBrowse($subBrowse);
$params = base64_encode($browse->serializeToString());
$rawData = [
'context' => [
'client' => [
'clientName' => 'WEB',
'clientVersion' => MUSIC_VERSION
]
],
'browseId' => $channelId,
'params' => $params,
];
$opts = [
'http' => [
'method' => 'POST',
'header' => implodeArray([
'Content-Type' => 'application/json',
'Origin' => $ORIGIN,
'Authorization' => "SAPISIDHASH $SAPISIDHASH",
'Cookie' => implode('; ', implodeArray([
'__Secure-3PSID' => $__Secure_3PSID,
'__Secure-3PAPISID' => $SAPISID,
], '=')),
], ': '),
'content' => json_encode($rawData),
]
];
$result = getJSON('https://www.youtube.com/youtubei/v1/browse', $opts);
$contents = getTabByName($result, 'Community')['tabRenderer']['content']['sectionListRenderer']['contents'];
$content = $contents[0]['itemSectionRenderer']['contents'][0];
$post = getCommunityPostFromContent($content);
$continuationToken = urldecode($contents[1]['itemSectionRenderer']['contents'][0]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token']);
if ($order === 'time') {
$result = getContinuationJson($continuationToken);
$continuationToken = urldecode($result['onResponseReceivedEndpoints'][0]['reloadContinuationItemsCommand']['continuationItems'][0]['commentsHeaderRenderer']['sortMenu']['sortFilterSubMenuRenderer']['subMenuItems'][1]['serviceEndpoint']['continuationCommand']['token']);
}
$comments = [
'nextPageToken' => $continuationToken
];
$post['comments'] = $comments;
$answerItem = [
'kind' => 'youtube#community',
'etag' => 'NotImplemented',
'id' => $postId,
'snippet' => $post
];
$answer = [
'kind' => 'youtube#communityListResponse',
'etag' => 'NotImplemented'
];
$answer['items'] = [$answerItem];
return json_encode($answer, JSON_PRETTY_PRINT);
}