-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathYouTubeController.php
570 lines (473 loc) · 19.8 KB
/
YouTubeController.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use YouTube;
use App\Helpers\Helper;
use Exception;
use Log;
use Cache;
use App\Repositories\YouTubeApiRepository;
class YouTubeController extends Controller
{
/**
* Default output format for latest video API.
*
* @var string
*/
protected $defaultFormat = '{title} - {url}';
/**
* Valid 'variables' for the output format in latest video API.
* Make sure to update the replacemtnts in both `latestVideo()` and `latestPlVideo()` if this array changes.
*
* @var array
*/
protected $formatSearch = ['{id}', '{url}', '{title}'];
/**
* YouTube API repository, for handling logic around caching.
*
* @var YouTubeApiRepository
*/
protected $api;
public function __construct(YouTubeApiRepository $api)
{
$this->api = $api;
}
/**
* Retrieves the latest public YouTube upload from the specified identifier.
*
* @param Request $request
* @return Response
*/
public function latestVideo(Request $request)
{
$type = null;
if ($request->has('user')) {
$type = 'user';
}
if ($request->has('handle')) {
$type = 'handle';
}
if ($request->has('id')) {
$type = 'id';
}
if (empty($type)) {
return Helper::text('You need to specify a "user" (/user/ URLs), an "id" (/channel/ URLs) or the YouTube channel handle (@).');
}
$id = $request->input($type, null);
$format = $request->input('format', $this->defaultFormat);
$exclude = $request->input('exclude', null);
$include = $request->input('include', null);
$noShorts = $request->input('no_shorts', '0');
$shortsUrl = $request->has('shorts_url');
$hasMinDuration = $request->has('min_duration');
$hasMaxDuration = $request->has('max_duration');
$minDuration = $request->input('min_duration', null);
$maxDuration = $request->input('max_duration', null);
/**
* We intentionally add an additional second to durations, to give more leeway for the API to return the correct results.
* As YouTube can for example consider "2:00" as "2m00s595ms" or something similar.
*/
if ($hasMinDuration) {
$minDuration = (int) $minDuration;
$minDuration++;
}
if ($hasMaxDuration) {
$maxDuration = (int) $maxDuration;
$maxDuration++;
}
// Checking for minimum 2 here, as we're adding an additional second to the duration when specified.
if ($hasMinDuration && $minDuration < 2) {
return Helper::text('The "min_duration" parameter has to be a number, minimum 1.');
}
if ($hasMaxDuration && $maxDuration < 2) {
return Helper::text('The "max_duration" parameter has to be a number, minimum 1.');
}
$excludeShorts = $noShorts !== '0' && $noShorts !== 'false';
$noLivestream = $request->input('no_livestream', '0');
$excludeLivestream = $noLivestream !== '0' && $noLivestream !== 'false';
$fetchExtendedDetails = $excludeShorts || $excludeLivestream || $hasMinDuration || $hasMaxDuration;
/**
* URL format used for the `{url}` replacement in the format string.
* Can be overridden by `shortsUrl` parameter before we do the actual replacements of `format`.
*/
$urlFormat = 'https://youtu.be/%s';
if ($exclude !== null) {
$exclude = trim($exclude);
}
if ($include !== null) {
$include = trim($include);
}
if (empty(trim($format))) {
$format = $this->defaultFormat;
}
$skip = intval($request->input('skip', 0));
$max = 50;
if ($skip >= $max) {
$skip = 0;
}
/**
* Occasionally someone will try to pass the channel handle as the `user` parameter.
* So let's help them out here :)
*/
if (strpos($id, '@') === 0) {
$type = 'handle';
}
/**
* Channel IDs and old "user" URLs do not have spaces in them.
*
* Some users have specified their display names (example: "My Channel Name")
* as their channel ID, which will just return an error from the YouTube API.
*
* To prevent unnecessary API requests, we're returning an error early as
* this would be an invalid ID/user value anyways.
*/
if (strpos($id, ' ') !== false) {
return Helper::text(__('youtube.invalid_channel_value', [
'type' => $type,
'id' => $id,
]));
}
try {
$parts = ['id', 'snippet', 'contentDetails'];
switch ($type) {
case 'user':
$channel = YouTube::getChannelByName($id, [], $parts);
break;
case 'handle':
$channel = YouTube::getChannelByHandle($id, [], $parts);
break;
default:
$channel = YouTube::getChannelById($id, [], $parts);
break;
}
if ($channel === false) {
return Helper::text(sprintf('The specified identifier "%s" as type "%s" is invalid.', $id, $type));
}
$uploadsPlaylist = $channel
->contentDetails
->relatedPlaylists
->uploads;
// TODO: Implement some caching
$apiResults = YouTube::getPlaylistItemsByPlaylistId($uploadsPlaylist);
$results = $apiResults['results'];
/**
* Sometimes YouTube's API returns bad data... I guess?
*/
if (!is_array($results)) {
return Helper::text('An error occurred retrieving videos for channel: ' . $request->input($type));
}
$videoIds = array_map(function($video) {
return $video->contentDetails->videoId;
}, $results);
$videoDetails = [];
if ($fetchExtendedDetails) {
$videoDetails = $this->api->getVideoDetails($videoIds);
}
if ($excludeShorts) {
$filteredVideos = $this->api->filterShorts($videoDetails);
$results = array_filter($results, function($video) use ($filteredVideos) {
$videoId = $video->contentDetails->videoId;
return isset($filteredVideos[$videoId]);
});
}
// Note: This flag only excludes a currently active livestream. Stream VODs are considered regular videos by the API.
if ($excludeLivestream) {
$filteredVideos = $this->api->filterLivestreams($videoDetails);
$results = array_filter($results, function($video) use ($filteredVideos) {
$videoId = $video->contentDetails->videoId;
return isset($filteredVideos[$videoId]);
});
}
// Filter by durations
if ($hasMinDuration || $hasMaxDuration) {
$minDuration = $minDuration ?? 0;
$maxDuration = $maxDuration ?? 0;
$filteredVideos = $this->api->filterByDuration($videoDetails, $minDuration, $maxDuration);
$results = array_filter($results, function($video) use ($filteredVideos) {
$videoId = $video->contentDetails->videoId;
return isset($filteredVideos[$videoId]);
});
}
/**
* The YouTube API seems to return basic information about private videos as well,
* even though we can't see any "real" information about them.
*
* This actually causes issues when we attempt to sort the videos,
* as `videoPublishedAt` isn't a field that's available.
*
* Instead we filter the videos, so we only have the public ones left.
*/
$results = array_filter($results,
function($video) use ($exclude) {
$privacyStatus = $video->status->privacyStatus ?? 'private';
$isPublic = $privacyStatus === 'public';
if (empty($exclude)) {
return $isPublic;
}
$title = $video->snippet->title ?? '';
$isExcluded = stripos($title, $exclude) !== false;
return $isPublic && !$isExcluded;
}
);
if (!empty($include)) {
$results = array_filter($results,
function($video) use ($include) {
$title = $video->snippet->title ?? '';
return stripos($title, $include) !== false;
}
);
}
if (empty($results)) {
return Helper::text('This channel has no public videos matching the specified criteria.');
}
/**
* Seems that YouTube sorts the API response for uploaded videos
* by their upload timestamp, instead of their
* "published publicly to YouTube" timestamp.
*
* With scheduled uploads, this can become an issue, so we're re-sorting
* the whole array to take this into account.
*
* The fallback for `date('c', 0)` is used when `videoPublishedAt` isn't an available field.
* All public videos (since we filter out any non-public videos earlier), _should_ have this field.
* So it might be an unnecessary precaution.
*
* Turns out that `snippet` seems to contain a more accurate publishedAt date,
* while `contentDetails` can _sometimes_ have... so we try to use `snippet` first.
*/
usort($results, function($a, $b) {
$publishOne = $a->snippet->publishedAt ?? $a->contentDetails->videoPublishedAt ?? date('c', 0);
$publishTwo = $b->snippet->publishedAt ?? $b->contentDetails->videoPublishedAt ?? date('c', 0);
return strtotime($publishTwo) - strtotime($publishOne);
});
$total = count($results);
// Check if the request skips a valid amount of videos.
if ($total < ($skip + 1)) {
return Helper::text(sprintf('Channel only has %d public videos. Invalid skip count specified: %d.', $total, $skip));
}
$video = $results[$skip];
// Title can sometimes includes HTML entities (such as '&' instead of '&')
$title = htmlspecialchars_decode($video->snippet->title, ENT_QUOTES);
$videoId = $video->contentDetails->videoId;
if ($shortsUrl) {
$isShort = $this->api->isShort($videoId);
if ($isShort) {
$urlFormat = 'https://youtube.com/shorts/%s';
}
}
/**
* See $this->formatSearch for a list of available variables.
*/
$replacements = [
// {id}
$videoId,
// {url}
sprintf($urlFormat, $videoId),
// {title}
$title,
];
return Helper::text(str_replace($this->formatSearch, $replacements, $format));
} catch (Exception $ex) {
$exceptionMessage = sprintf('%s%s%s', $ex->getMessage(), PHP_EOL, $ex->getTraceAsString());
$logResult = sprintf('An error occurred in /youtube/latest_video (channel: "%s", skip: %d): %s', $request->input($type), $skip, $exceptionMessage);
Log::error($logResult);
return Helper::text('An error occurred retrieving videos for channel: ' . $request->input($type));
}
}
/**
* Retrieve the latest video in a YouTube playlist based on the playlist ID.
*
* @param Request $request
* @return Response
*/
public function latestPlVideo(Request $request)
{
$id = $request->input('id', null);
$skip = intval($request->input('skip', 0));
$separator = $request->input('separator', '-');
// Use default format, but make sure it's backwards compatible with the `separator` parameter.
$defaultFormat = str_replace('-', $separator, $this->defaultFormat);
$format = $request->input('format', $defaultFormat);
if (empty($id) || trim($id) === '') {
return Helper::text('A playlist ID has to be specified.');
}
try {
$results = YouTube::getPlaylistItemsByPlaylistId($id)['results'];
$count = count($results);
if ($skip > $count - 1) {
$error = sprintf('Skip count (%d) has to be lower than the amount of available videos in playlist (%d).', $skip, $count);
return Helper::text($error);
}
$video = $results[$skip];
// Title can sometimes includes HTML entities (such as '&' instead of '&')
$title = htmlspecialchars_decode($video->snippet->title, ENT_QUOTES);
$videoId = $video->contentDetails->videoId;
/**
* See $this->formatSearch for a list of available variables.
*/
$replacements = [
// {id}
$videoId,
// {url}
sprintf('https://youtu.be/%s', $videoId),
// {title}
$title,
];
return Helper::text(str_replace($this->formatSearch, $replacements, $format));
} catch (Exception $e) {
return Helper::text('An error occurred retrieving playlist items with the playlist ID: ' . $id);
}
}
/**
* Searches the YouTube API for the specified string, if it's a video ID, it'll just return the video ID.
* If it's a valid search string, and it finds a result, it'll return the video ID of the first result.
* If neither, it will either return nothing (if the word "nightbot" is found in the user agent).
* Or it will return an error message.
*
* @param Request $request
* @param string $videoId
* @param string $search Search string or video ID/URL
* @return Response
*/
public function videoId(Request $request, $videoId = null, $search = null)
{
$search = $search ?: $request->input('search', null);
$showUrl = $request->exists('show_url');
if (empty($search)) {
// Send an empty response so that Nightbot doesn't attempt to 'search' the YouTube API with the returned string.
if ($this->isNightbot($request)) {
return Helper::text('');
}
return Helper::text('No search parameter specified.');
}
$normalizedSearch = strtolower(trim($search));
$cacheKey = sprintf('youtube.video_id.%s', hash('sha256', $normalizedSearch));
// Loaded from `config/youtube-cache.php` - Defaults to 3 hours
$cacheTime = config('config.youtube-cache.search', 10800);
/**
* Check if search string has been cached
*/
if (Cache::has($cacheKey)) {
$videoId = Cache::get($cacheKey);
if ($showUrl) {
return Helper::text(sprintf('https://youtu.be/%s', $videoId));
}
return Helper::text($videoId);
}
// YouTube URL detected
$parse = $this->parseURL($search);
if ($parse !== false) {
$video = YouTube::getVideoInfo($parse);
if (!empty($video)) {
$videoId = $video->id;
Cache::put($cacheKey, $videoId, $cacheTime);
if ($showUrl) {
return Helper::text(sprintf('https://youtu.be/%s', $videoId));
}
return Helper::text($videoId);
}
}
// YouTube URL not detected, search for video
if ($parse === false) {
$parameters = [
'q' => $search,
'type' => 'video',
'part' => 'id',
'maxResults' => 5,
];
try {
$videos = YouTube::searchAdvanced($parameters);
/**
* For some reason, certain keywords (only one that I saw was "headlines") would error out.
*
* This is because the YouTube Data API decides to return a specific channel ID for their
* auto-generated "News" channel, even though type is set to 'video'.
*
* Specifically talking about this channel: https://www.youtube.com/channel/UCYfdidRxbB8Qhf0Nx7ioOYw
*
* This is an ugly workaround that I did to prevent it from giving an error whenever
* someone searches for songs like "Drake - Headlines".
*/
if (!empty($videos)) {
foreach ($videos as $searchResult) {
if (property_exists($searchResult->id, 'videoId')) {
$video = $searchResult->id->videoId;
break;
}
}
}
} catch (Exception $ex) {
Log::error(sprintf('An error occurred in /youtube/videoid (search query: "%s" ): %s', $search, (string) $ex));
if ($this->isNightbot($request)) {
return Helper::text('');
}
return Helper::text('An error occurred when searching: ' . $search);
}
}
if (empty($video)) {
if ($this->isNightbot($request)) {
return Helper::text('');
}
return Helper::text('Invalid video URL, video ID or search string.');
}
Cache::put($cacheKey, $video, $cacheTime);
if ($showUrl) {
$video = sprintf('https://youtu.be/%s', $video);
}
return Helper::text($video);
}
/**
* Parses a URL and attempts to retrieve the video ID.
* This does not validate if the video ID is valid or not, though it is intended to be done in `videoId()`.
*
* @param string $url The URL to parse
* @return string|false The video ID or false if it's unable to find it.
*/
private function parseURL($url)
{
$url = urldecode($url);
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
return false;
}
$parsed = null;
try {
$parsed = parse_url($url);
} catch (Exception $e) {
return false;
}
if (empty($parsed)) {
return false;
}
$host = strtolower($parsed['host']);
if ($host === 'youtu.be') {
// Return video ID after `https://youtu.be/`
return substr($parsed['path'], 1);
}
if ($host === 'youtube.com' || $host === 'www.youtube.com') {
$query = [];
$queryString = $parsed['query'] ?? '';
parse_str($queryString, $query);
if (empty($query['v'])) {
return false;
}
if (!is_string($query['v'])) {
return false;
}
return $query['v'];
}
return false;
}
/**
* Checks if the request is done using Nightbot's "URL fetcher"
*
* @param Request $request
* @return boolean
*/
private function isNightbot(Request $request)
{
if (strpos($request->server('HTTP_USER_AGENT'), 'Nightbot') !== false) {
return true;
}
return false;
}
}