-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathvideos.php
204 lines (177 loc) · 8.42 KB
/
videos.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
// StackOverflow contentDetails source: https://stackoverflow.com/a/70908689/7123660
// StackOverflow status source: https://stackoverflow.com/a/70894799/7123660
// StackOverflow music source: https://stackoverflow.com/a/71012426/7123660
$videosTests = [['contentDetails&id=g5xNzUA5Qf8', 'items/0/contentDetails/duration', '213'],
['status&id=J8ZVxDK11Jo', 'items/0/status/embeddable', false],
['status&id=g5xNzUA5Qf8', 'items/0/status/embeddable', true], // could allow subarray for JSON check in response likewise in a single request can check several features
['music&id=Xge20AqKSRE', 'items/0/music/available', false],
['music&id=ntG3GQdY_Ok', 'items/0/music/available', true]];
include_once 'common.php';
$realOptions = ['id', 'status', 'contentDetails', 'music', 'short', 'impressions', 'containsMusic', 'isPaidPromotion', 'isPremium', 'isMemberOnly', 'mostReplayed', 'qualities']; // could load index.php from that
// really necessary ?
foreach ($realOptions as $realOption) {
$options[$realOption] = false;
}
if (isset($_GET['part']) && (isset($_GET['id']) || isset($_GET['clipId']))) {
$part = $_GET['part'];
$parts = explode(',', $part, count($realOptions));
foreach ($parts as $part) {
if (!in_array($part, $realOptions)) {
die('invalid part ' . $part);
} else {
$options[$part] = true;
}
}
$isClip = isset($_GET['clipId']);
$field = $isClip ? 'clipId' : 'id';
$ids = $_GET[$field];
$realIds = str_contains($ids, ',') ? explode(',', $ids, 50) : [$ids];
if (count($realIds) == 0) {
die('invalid id');
}
foreach ($realIds as $realId) {
if ((!$isClip && !isVideoId($realId)) && !isClipId($realId)) {
die('invalid ' . $field);
}
}
if ($options['impressions'] && (!isset($_GET['SAPISIDHASH']) || !isSAPISIDHASH($_GET['SAPISIDHASH']))) {
die('invalid SAPISIDHASH');
}
echo getAPI($realIds);
}
function getJSONFunc($rawData, $music = false)
{
$headers = [
"Content-Type: application/json"
];
if ($music) {
array_push($headers, 'Referer: https://music.youtube.com');
}
$opts = [
"http" => [
"method" => "POST",
"header" => $headers,
"content" => $rawData,
]
];
return getJSON('https://' . ($music ? 'music' : 'www') . '.youtube.com/youtubei/v1/player?key=' . UI_KEY, $opts);
}
function getItem($id)
{
global $options;
$result = '';
if ($options['status'] || $options['contentDetails']) {
$rawData = '{"videoId":"' . $id . '","context":{"client":{"clientName":"WEB_EMBEDDED_PLAYER","clientVersion":"' . CLIENT_VERSION . '"}}}';
$result = getJSONFunc($rawData);
}
$item = [
'kind' => 'youtube#video',
'etag' => 'NotImplemented',
'id' => $id
];
if ($options['status']) {
$status = [
'embeddable' => $result['playabilityStatus']['status'] === 'OK'
];
$item['status'] = $status;
}
if ($options['contentDetails']) {
$contentDetails = [
'duration' => intval($result['videoDetails']['lengthSeconds'])
];
$item['contentDetails'] = $contentDetails;
}
if ($options['music']) {
// music request doesn't provide embeddable info - could not make a request if only music and contentDetails
$rawData = '{"videoId":"' . $id . '","context":{"client":{"clientName":"WEB_REMIX","clientVersion":"' . CLIENT_VERSION . '"}}}';
$resultMusic = getJSONFunc($rawData, true);
$music = [
'available' => $resultMusic['playabilityStatus']['status'] === "OK"
];
$item['music'] = $music;
}
if ($options['short']) {
$short = [
'available' => !isRedirection('https://www.youtube.com/shorts/' . $id)
];
$item['short'] = $short;
}
if ($options['impressions']) {
$headers = [
"x-origin: https://studio.youtube.com",
"authorization: SAPISIDHASH " . $_GET['SAPISIDHASH'],
"Content-Type:",
"cookie: HSID=A4BqSu4moNA0Be1N9; SSID=AA0tycmNyGWo-Z_5v; APISID=a; SAPISID=zRbK-_14V7wIAieP/Ab_wY1sjLVrKQUM2c; SID=HwhYm6rJKOn_3R9oOrTNDJjpHIiq9Uos0F5fv4LPdMRSqyVHA1EDZwbLXo0kuUYAIN_MUQ."
];
$rawData = '{"screenConfig":{"entity":{"videoId":"' . $id . '"}},"desktopState":{"tabId":"ANALYTICS_TAB_ID_REACH"}}';
$opts = [
"http" => [
"method" => "POST",
"header" => $headers,
"content" => $rawData,
]
];
$json = getJSON('https://studio.youtube.com/youtubei/v1/analytics_data/get_screen?key=' . UI_KEY, $opts);
$impressions = $json['cards'][0]['keyMetricCardData']['keyMetricTabs'][0]['primaryContent']['total'];
$item['impressions'] = $impressions;
}
if ($options['containsMusic']) {
$json = getJSONFromHTML('https://www.youtube.com/watch?v=' . $id);
$containsMusic = doesPathExist($json, 'engagementPanels/1/engagementPanelSectionListRenderer/content/structuredDescriptionContentRenderer/items/1/videoDescriptionMusicSectionRenderer');
$item['containsMusic'] = $containsMusic;
}
if ($options['id'] && isset($_GET['clipId'])) {
$json = getJSONFromHTML('https://www.youtube.com/clip/' . $id);
$videoId = $json['currentVideoEndpoint']['watchEndpoint']['videoId'];
$item['videoId'] = $videoId;
}
if ($options['isPaidPromotion']) {
$json = getJSONFromHTML('https://www.youtube.com/watch?v=' . $id, [], 'ytInitialPlayerResponse');
$isPaidPromotion = array_key_exists('paidContentOverlay', $json);
$item['isPaidPromotion'] = $isPaidPromotion;
}
if ($options['isPremium']) {
$json = getJSONFromHTML('https://www.youtube.com/watch?v=' . $id);
$isPremium = array_key_exists('offerModule', $json['contents']['twoColumnWatchNextResults']['secondaryResults']['secondaryResults']);
$item['isPremium'] = $isPremium;
}
if ($options['isMemberOnly']) {
$json = getJSONFromHTML('https://www.youtube.com/watch?v=' . $id, $opts);
$isMemberOnly = array_key_exists('badges', $json['contents']['twoColumnWatchNextResults']['results']['results']['contents'][0]['videoPrimaryInfoRenderer']);
$item['isMemberOnly'] = $isMemberOnly;
}
if ($options['mostReplayed']) {
$json = getJSONFromHTML('https://www.youtube.com/watch?v=' . $id);
$mostReplayed = $json['playerOverlays']['playerOverlayRenderer']['decoratedPlayerBarRenderer']['decoratedPlayerBarRenderer']['playerBar']['multiMarkersPlayerBarRenderer']['markersMap'][0]['value']['heatmap']['heatmapRenderer'];
// What is `Dp` in `maxHeightDp` and `minHeightDp` ? If not relevant could add ['heatMarkers'] to the JSON path above.
$item['mostReplayed'] = $mostReplayed;
}
if ($options['qualities']) {
$json = getJSONFromHTML('https://www.youtube.com/watch?v=' . $id, [], 'ytInitialPlayerResponse');
$qualities = [];
foreach ($json['streamingData']['adaptiveFormats'] as $quality) {
if (array_key_exists('qualityLabel', $quality)) {
$quality = $quality['qualityLabel'];
if (!in_array($quality, $qualities)) {
array_push($qualities, $quality);
}
}
}
$item['qualities'] = $qualities;
}
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);
}