This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
watch.php
executable file
·206 lines (190 loc) · 9.08 KB
/
watch.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
205
206
<?php
// watch.php for watching videos
// init twig services
require __DIR__ . '/vendor/autoload.php';
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
$loader = new FilesystemLoader(__DIR__ . '/templates');
$twig = new Environment($loader);
include("includes/youtubei/details.php");
include("includes/youtubei/player.php");
include("includes/youtubei/comments.php");
include_once("includes/config.inc.php");
// debug flag
if (isset(($_GET['debug']))) {
if ($_GET['debug'] == $debugCode) {
$debug = true;
}
} else {
$debug = false;
}
if (!isset($_GET['v'])) {
echo $twig->render(
"novideo.html.twig",
[
"id" => "#",
]
);
} else {
$id = $_GET['v'];
// request player :hsuk:
$response_object = requestDetails($id);
$mainResponseObject = json_decode($response_object);
$playerObject = json_decode(requestPlayer($id));
// check if video exists
if (!isset($mainResponseObject->contents->twoColumnWatchNextResults->results->results->contents[0]->videoPrimaryInfoRenderer->title->runs[0]->text)) {
echo $twig->render(
"novideo.html.twig",
[
"id" => $id,
]
);
} else {
$videoDetails = array(
"videoTitle" => $mainResponseObject->contents->twoColumnWatchNextResults->results->results->contents[0]->videoPrimaryInfoRenderer->title->runs[0]->text,
"videoDescription" => '<span class="redtext"><i>No description</i></span>', // due for modification later
"videoLengthInSeconds" => $playerObject->videoDetails->lengthSeconds,
"videoViews" => $mainResponseObject->contents->twoColumnWatchNextResults->results->results->contents[0]->videoPrimaryInfoRenderer->viewCount->videoViewCountRenderer->viewCount->simpleText,
"videoAuthor" => $mainResponseObject->contents->twoColumnWatchNextResults->results->results->contents[1]->videoSecondaryInfoRenderer->owner->videoOwnerRenderer->title->runs[0]->text,
"videoUploadDate" => $mainResponseObject->contents->twoColumnWatchNextResults->results->results->contents[0]->videoPrimaryInfoRenderer->dateText->simpleText,
"videoRuntime" => $playerObject->microformat->playerMicroformatRenderer->lengthSeconds,
"videoThumbnail" => $playerObject->contents->twoColumnWatchNextResults->results->results->contents[0]->videoPrimaryInfoRenderer->title->runs[0]->text,
"authorChannelId" => $mainResponseObject->contents->twoColumnWatchNextResults->results->results->contents[1]->videoSecondaryInfoRenderer->owner->videoOwnerRenderer->title->runs[0]->navigationEndpoint->browseEndpoint->browseId,
"videoConvertedRuntime" => gmdate("i:s", $playerObject->microformat->playerMicroformatRenderer->lengthSeconds),
);
// replace description text if description exists
if (isset($mainResponseObject->contents->twoColumnWatchNextResults->results->results->contents[1]->videoSecondaryInfoRenderer->attributedDescription->content)) {
$videoDetails['videoDescription'] = $mainResponseObject->contents->twoColumnWatchNextResults->results->results->contents[1]->videoSecondaryInfoRenderer->attributedDescription->content;
}
// get video tags(annoying)
if (isset($playerObject->videoDetails->keywords)) {
$tagarr = $playerObject->videoDetails->keywords;
$tagcount = sizeof($tagarr);
if ($tagcount >= 1) {
$tags = $tagarr;
} else {
$tags = array(0 => "None");
}
} else {
$tagcount = 0;
$tags = array(0 => "None");
}
// video source file
if (requestVideoSrc($id)) {
$videoLink = requestVideoSrc($id);
$videoHtml = sprintf('<video controls class="video-player googlevideo-player" style="width: 427px; height: margin:center;" src="%s"></video>', $videoLink);
if ($debug) {
echo $videoLink;
}
} else {
$videoHtml = sprintf('<span class="noVideoError">Video unavailable for playback. <a href="https://youtube.com/watch?v=%s">Watch on YouTube</a></span>', $id);
}
// fetch comments into an array...
$initialResponseContext = json_decode(fetchInitialNext($id));
if (isset($initialResponseContext->contents->twoColumnWatchNextResults->results->results->contents[3])) {
$hasComments = true;
$continuation = $initialResponseContext
->contents
->twoColumnWatchNextResults
->results
->results
->contents[3]
->itemSectionRenderer
->contents[0]
->continuationItemRenderer
->continuationEndpoint
->continuationCommand
->token;
// fetch the comments
$mainResponseContext = json_decode(fetchComment($id, $continuation));
$commentCount = $mainResponseObject->contents->twoColumnWatchNextResults->results->results->contents[2]->itemSectionRenderer->contents[0]->commentsEntryPointHeaderRenderer->commentCount->simpleText;
$commentsArray = $mainResponseContext->onResponseReceivedEndpoints[1]->reloadContinuationItemsCommand->continuationItems;
// start writing to twiG
} else {
$hasComments = false;
}
// get related videos...
$relatedResponse = json_decode(fetchInitialNext($id));
if (isset($relatedResponse->contents->twoColumnWatchNextResults->secondaryResults->secondaryResults->results)) {
$hasRelated = true;
$relatedArray = $relatedResponse
->contents
->twoColumnWatchNextResults
->secondaryResults
->secondaryResults
->results;
} else {
$hasRelated = false;
}
// add check for native player GET param
if (isset($_GET['useNative'])) {
if ($_GET['useNative'] == true) {
setcookie("useNative", true, time() + (86400 * 30), "/");
$useNativePlayer = true;
} elseif ($_GET['useNative'] == false) {
setcookie("useNative", false, time() + (86400 * 30), "/");
$useNativePlayer = false;
}
} // else fallback to cookie
// check for native player cookie
if (isset($_COOKIE['useNative'])) {
$nativeCookie = $_COOKIE['useNative'];
if ($nativeCookie) {
$useNativePlayer = true;
} else {
$useNativePlayer = false;
}
} else {
$useNativePlayer = true;
}
// 2012 easter egg
if (isset($_GET['2012']) && $_GET['2012'] == "1") {
echo $twig->render(
"watch2012.html.twig",
[
"videoId" => $id,
"videoHtml" => $videoHtml,
"videoSrc" => $videoLink,
"videoTags" => $tags,
"videoDescription" => $videoDetails['videoDescription'],
"videoTitle" => $videoDetails['videoTitle'],
"videoViews" => $videoDetails['videoViews'],
"videoAuthor" => $videoDetails['videoAuthor'],
"videoUploadDate" => $videoDetails['videoUploadDate'],
"videoRuntime" => $videoDetails['videoRuntime'],
"videoThumbnail" => $videoDetails['videoThumbnail'],
"authorChannelId" => $videoDetails['authorChannelId'],
"videoConvertedRuntime" => $videoDetails['videoConvertedRuntime'],
]
);
} else {
$dataArray = [
"videoId" => $id,
"videoHtml" => $videoHtml,
"videoSrc" => $videoLink,
"videoTags" => $tags,
"videoDescription" => $videoDetails['videoDescription'],
"videoTitle" => $videoDetails['videoTitle'],
"videoViews" => $videoDetails['videoViews'],
"videoAuthor" => $videoDetails['videoAuthor'],
"videoUploadDate" => $videoDetails['videoUploadDate'],
"videoRuntime" => $videoDetails['videoRuntime'],
"videoThumbnail" => $videoDetails['videoThumbnail'],
"authorChannelId" => $videoDetails['authorChannelId'],
"videoConvertedRuntime" => $videoDetails['videoConvertedRuntime'],
"hasComments" => $hasComments,
"hasRelated" => $hasRelated,
"useNativePlayer" => $useNativePlayer,
];
if ($hasComments) {
$dataArray['videoComments'] = $commentsArray;
$dataArray['commentCount'] = $commentCount;
}
if ($hasRelated) {
$dataArray['videoRelated'] = $relatedArray;
$dataArray['videoRelatedCount'] = sizeof($relatedArray) - 1;
}
echo $twig->render("watch.html.twig", $dataArray);
}
}
}