2013-08-15 14:05:58 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-08 20:06:35 +03:00
|
|
|
* RssBridgeYoutube
|
2013-08-15 14:05:58 +04:00
|
|
|
* Returns the newest videos
|
2015-11-01 14:17:36 +03:00
|
|
|
* WARNING: to parse big playlists (over ~90 videos), you need to edit simple_html_dom.php:
|
2014-05-14 16:34:06 +04:00
|
|
|
* change: define('MAX_FILE_SIZE', 600000);
|
|
|
|
* into: define('MAX_FILE_SIZE', 900000); (or more)
|
2013-08-15 14:05:58 +04:00
|
|
|
*/
|
2015-11-01 14:17:36 +03:00
|
|
|
class YoutubeBridge extends BridgeAbstract {
|
|
|
|
|
2016-08-30 12:23:55 +03:00
|
|
|
const NAME = 'YouTube Bridge';
|
|
|
|
const URI = 'https://www.youtube.com/';
|
2016-09-25 18:04:28 +03:00
|
|
|
const CACHE_TIMEOUT = 10800; // 3h
|
2016-08-30 12:23:55 +03:00
|
|
|
const DESCRIPTION = 'Returns the 10 newest videos by username/channel/playlist or search';
|
2021-05-16 23:02:45 +03:00
|
|
|
const MAINTAINER = 'em92';
|
2015-11-01 14:17:36 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
const PARAMETERS = array(
|
|
|
|
'By username' => array(
|
|
|
|
'u' => array(
|
|
|
|
'name' => 'username',
|
|
|
|
'exampleValue' => 'test',
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'By channel id' => array(
|
|
|
|
'c' => array(
|
|
|
|
'name' => 'channel id',
|
2018-06-30 00:55:33 +03:00
|
|
|
'exampleValue' => '15',
|
2017-02-11 18:16:56 +03:00
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
),
|
2021-09-11 11:20:14 +03:00
|
|
|
'By custom name' => array(
|
|
|
|
'custom' => array(
|
|
|
|
'name' => 'custom name',
|
|
|
|
'exampleValue' => 'nasa',
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
),
|
2017-02-11 18:16:56 +03:00
|
|
|
'By playlist Id' => array(
|
|
|
|
'p' => array(
|
|
|
|
'name' => 'playlist id',
|
2018-06-30 00:55:33 +03:00
|
|
|
'exampleValue' => '15'
|
2017-02-11 18:16:56 +03:00
|
|
|
)
|
|
|
|
),
|
|
|
|
'Search result' => array(
|
|
|
|
's' => array(
|
|
|
|
'name' => 'search keyword',
|
|
|
|
'exampleValue' => 'test'
|
|
|
|
),
|
|
|
|
'pa' => array(
|
|
|
|
'name' => 'page',
|
|
|
|
'type' => 'number',
|
2021-09-11 11:20:14 +03:00
|
|
|
'title' => 'This option is not work anymore, as YouTube will always return the same page',
|
2017-02-11 18:16:56 +03:00
|
|
|
'exampleValue' => 1
|
|
|
|
)
|
2018-07-21 15:22:53 +03:00
|
|
|
),
|
|
|
|
'global' => array(
|
|
|
|
'duration_min' => array(
|
|
|
|
'name' => 'min. duration (minutes)',
|
|
|
|
'type' => 'number',
|
|
|
|
'title' => 'Minimum duration for the video in minutes',
|
|
|
|
'exampleValue' => 5
|
|
|
|
),
|
|
|
|
'duration_max' => array(
|
|
|
|
'name' => 'max. duration (minutes)',
|
|
|
|
'type' => 'number',
|
|
|
|
'title' => 'Maximum duration for the video in minutes',
|
|
|
|
'exampleValue' => 10
|
|
|
|
)
|
2017-02-11 18:16:56 +03:00
|
|
|
)
|
|
|
|
);
|
2015-11-01 14:17:36 +03:00
|
|
|
|
2018-07-21 15:22:53 +03:00
|
|
|
private $feedName = '';
|
2021-09-11 11:20:14 +03:00
|
|
|
private $feeduri = '';
|
|
|
|
private $channel_name = '';
|
|
|
|
// This took from repo BetterVideoRss of VerifiedJoseph.
|
|
|
|
const URI_REGEX = '/(https?:\/\/(?:www\.)?(?:[a-zA-Z0-9-.]{2,256}\.[a-z]{2,20})(\:[0-9]{2 ,4})?(?:\/[a-zA-Z0-9@:%_\+.,~#"\'!?&\/\/=\-*]+|\/)?)/ims'; //phpcs:ignore
|
2018-07-21 15:22:53 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
private function ytBridgeQueryVideoInfo($vid, &$author, &$desc, &$time){
|
2019-06-10 16:31:35 +03:00
|
|
|
$html = $this->ytGetSimpleHTMLDOM(self::URI . "watch?v=$vid", true);
|
2018-02-17 00:11:03 +03:00
|
|
|
|
|
|
|
// Skip unavailable videos
|
2021-05-16 23:02:45 +03:00
|
|
|
if(strpos($html->innertext, 'IS_UNAVAILABLE_PAGE') !== false) {
|
2018-02-17 00:11:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-16 23:02:45 +03:00
|
|
|
$elAuthor = $html->find('span[itemprop=author] > link[itemprop=name]', 0);
|
|
|
|
if (!is_null($elAuthor)) {
|
|
|
|
$author = $elAuthor->getAttribute('content');
|
|
|
|
}
|
2018-02-17 00:12:24 +03:00
|
|
|
|
2021-05-16 23:02:45 +03:00
|
|
|
$elDatePublished = $html->find('meta[itemprop=datePublished]', 0);
|
|
|
|
if(!is_null($elDatePublished))
|
|
|
|
$time = strtotime($elDatePublished->getAttribute('content'));
|
2018-02-17 00:12:24 +03:00
|
|
|
|
2021-09-11 11:20:14 +03:00
|
|
|
$jsonData = $this->getJSONData($html);
|
2021-05-16 23:02:45 +03:00
|
|
|
$jsonData = $jsonData->contents->twoColumnWatchNextResults->results->results->contents;
|
2018-02-17 00:12:24 +03:00
|
|
|
|
2021-05-16 23:02:45 +03:00
|
|
|
$videoSecondaryInfo = null;
|
|
|
|
foreach($jsonData as $item) {
|
|
|
|
if (isset($item->videoSecondaryInfoRenderer)) {
|
|
|
|
$videoSecondaryInfo = $item->videoSecondaryInfoRenderer;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-17 00:12:24 +03:00
|
|
|
}
|
2021-05-16 23:02:45 +03:00
|
|
|
if (!$videoSecondaryInfo) {
|
2021-09-11 11:20:14 +03:00
|
|
|
returnServerError('Could not find videoSecondaryInfoRenderer. Error at: ' . $vid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($videoSecondaryInfo->description)) {
|
|
|
|
foreach($videoSecondaryInfo->description->runs as $description) {
|
|
|
|
if(isset($description->navigationEndpoint)) {
|
|
|
|
$metadata = $description->navigationEndpoint->commandMetadata->webCommandMetadata;
|
|
|
|
$web_type = $metadata->webPageType;
|
|
|
|
$url = $metadata->url;
|
|
|
|
$text = '';
|
|
|
|
switch ($web_type) {
|
|
|
|
case 'WEB_PAGE_TYPE_UNKNOWN':
|
|
|
|
$url_components = parse_url($url);
|
|
|
|
if(isset($url_components['query']) && strpos($url_components['query'], '&q=') !== false) {
|
|
|
|
parse_str($url_components['query'], $params);
|
|
|
|
$url = urldecode($params['q']);
|
|
|
|
}
|
|
|
|
$text = $url;
|
|
|
|
break;
|
|
|
|
case 'WEB_PAGE_TYPE_WATCH':
|
|
|
|
case 'WEB_PAGE_TYPE_BROWSE':
|
|
|
|
$url = 'https://www.youtube.com' . $url;
|
|
|
|
$text = $description->text;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$desc .= "<a href=\"$url\" target=\"_blank\">$text</a>";
|
|
|
|
} else {
|
|
|
|
$desc .= nl2br($description->text);
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 23:02:45 +03:00
|
|
|
}
|
2016-05-04 00:45:32 +03:00
|
|
|
}
|
2014-06-20 18:41:51 +04:00
|
|
|
|
2021-09-11 11:20:14 +03:00
|
|
|
private function ytBridgeAddItem($vid, $title, $author, $desc, $time, $thumbnail = ''){
|
2016-08-22 19:55:59 +03:00
|
|
|
$item = array();
|
|
|
|
$item['id'] = $vid;
|
|
|
|
$item['title'] = $title;
|
|
|
|
$item['author'] = $author;
|
|
|
|
$item['timestamp'] = $time;
|
2017-02-11 18:16:56 +03:00
|
|
|
$item['uri'] = self::URI . 'watch?v=' . $vid;
|
2021-09-11 11:20:14 +03:00
|
|
|
if(!$thumbnail) {
|
|
|
|
$thumbnail = '0'; // Fallback to default thumbnail if there aren't any provided.
|
|
|
|
}
|
|
|
|
$thumbnailUri = str_replace('/www.', '/img.', self::URI) . 'vi/' . $vid . '/' . $thumbnail . '.jpg';
|
2017-02-11 18:16:56 +03:00
|
|
|
$item['content'] = '<a href="' . $item['uri'] . '"><img src="' . $thumbnailUri . '" /></a><br />' . $desc;
|
2016-05-04 00:45:32 +03:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
2014-06-20 18:41:51 +04:00
|
|
|
|
2016-05-04 00:45:32 +03:00
|
|
|
private function ytBridgeParseXmlFeed($xml) {
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($xml->find('entry') as $element) {
|
2017-02-11 18:16:56 +03:00
|
|
|
$title = $this->ytBridgeFixTitle($element->find('title', 0)->plaintext);
|
2016-05-04 00:45:32 +03:00
|
|
|
$author = $element->find('name', 0)->plaintext;
|
|
|
|
$desc = $element->find('media:description', 0)->innertext;
|
2017-04-27 22:40:20 +03:00
|
|
|
|
|
|
|
// Make sure the description is easy on the eye :)
|
|
|
|
$desc = htmlspecialchars($desc);
|
|
|
|
$desc = nl2br($desc);
|
2021-09-11 11:20:14 +03:00
|
|
|
$desc = preg_replace(self::URI_REGEX,
|
2017-04-27 22:40:20 +03:00
|
|
|
'<a href="$1" target="_blank">$1</a> ',
|
|
|
|
$desc);
|
|
|
|
|
2016-05-04 00:45:32 +03:00
|
|
|
$vid = str_replace('yt:video:', '', $element->find('id', 0)->plaintext);
|
|
|
|
$time = strtotime($element->find('published', 0)->plaintext);
|
2017-09-24 18:23:00 +03:00
|
|
|
if(strpos($vid, 'googleads') === false)
|
|
|
|
$this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
|
2016-05-04 00:45:32 +03:00
|
|
|
}
|
2017-12-29 04:14:11 +03:00
|
|
|
$this->feedName = $this->ytBridgeFixTitle($xml->find('feed > title', 0)->plaintext); // feedName will be used by getName()
|
2016-05-04 00:45:32 +03:00
|
|
|
}
|
2014-05-14 14:39:12 +04:00
|
|
|
|
2016-05-21 12:45:09 +03:00
|
|
|
private function ytBridgeFixTitle($title) {
|
|
|
|
// convert both Ӓ and " to UTF-8
|
2017-02-11 18:16:56 +03:00
|
|
|
return html_entity_decode($title, ENT_QUOTES, 'UTF-8');
|
2016-05-21 12:45:09 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 16:31:35 +03:00
|
|
|
private function ytGetSimpleHTMLDOM($url, $cached = false){
|
|
|
|
$header = array(
|
|
|
|
'Accept-Language: en-US'
|
|
|
|
);
|
|
|
|
$opts = array();
|
|
|
|
$lowercase = true;
|
|
|
|
$forceTagsClosed = true;
|
|
|
|
$target_charset = DEFAULT_TARGET_CHARSET;
|
|
|
|
$stripRN = false;
|
|
|
|
$defaultBRText = DEFAULT_BR_TEXT;
|
|
|
|
$defaultSpanText = DEFAULT_SPAN_TEXT;
|
|
|
|
if ($cached) {
|
|
|
|
return getSimpleHTMLDOMCached($url,
|
|
|
|
86400,
|
|
|
|
$header,
|
|
|
|
$opts,
|
|
|
|
$lowercase,
|
|
|
|
$forceTagsClosed,
|
|
|
|
$target_charset,
|
|
|
|
$stripRN,
|
|
|
|
$defaultBRText,
|
|
|
|
$defaultSpanText);
|
|
|
|
}
|
2017-04-27 22:40:20 +03:00
|
|
|
return getSimpleHTMLDOM($url,
|
2019-06-10 16:31:35 +03:00
|
|
|
$header,
|
|
|
|
$opts,
|
|
|
|
$lowercase,
|
|
|
|
$forceTagsClosed,
|
|
|
|
$target_charset,
|
|
|
|
$stripRN,
|
|
|
|
$defaultBRText,
|
|
|
|
$defaultSpanText);
|
2017-04-27 22:40:20 +03:00
|
|
|
}
|
|
|
|
|
2021-09-11 11:20:14 +03:00
|
|
|
private function getJSONData($html) {
|
|
|
|
$scriptRegex = '/var ytInitialData = (.*?);<\/script>/';
|
|
|
|
preg_match($scriptRegex, $html, $matches) or returnServerError('Could not find ytInitialData');
|
|
|
|
return json_decode($matches[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function parseJSONListing($jsonData) {
|
|
|
|
$duration_min = $this->getInput('duration_min') ?: -1;
|
|
|
|
$duration_min = $duration_min * 60;
|
|
|
|
|
|
|
|
$duration_max = $this->getInput('duration_max') ?: INF;
|
|
|
|
$duration_max = $duration_max * 60;
|
|
|
|
|
|
|
|
if($duration_max < $duration_min) {
|
|
|
|
returnClientError('Max duration must be greater than min duration!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// $vid_list = '';
|
|
|
|
|
|
|
|
foreach($jsonData as $item) {
|
|
|
|
$wrapper = null;
|
|
|
|
if(isset($item->gridVideoRenderer)) {
|
|
|
|
$wrapper = $item->gridVideoRenderer;
|
|
|
|
} elseif(isset($item->videoRenderer)) {
|
|
|
|
$wrapper = $item->videoRenderer;
|
|
|
|
} elseif(isset($item->playlistVideoRenderer)) {
|
|
|
|
$wrapper = $item->playlistVideoRenderer;
|
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$vid = $wrapper->videoId;
|
|
|
|
$title = $wrapper->title->runs[0]->text;
|
|
|
|
if(isset($wrapper->ownerText)) {
|
|
|
|
$this->channel_name = $wrapper->ownerText->runs[0]->text;
|
|
|
|
} elseif(isset($wrapper->shortBylineText)) {
|
|
|
|
$this->channel_name = $wrapper->shortBylineText->runs[0]->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
$author = '';
|
|
|
|
$desc = '';
|
|
|
|
$time = '';
|
|
|
|
|
|
|
|
// The duration comes in one of the formats:
|
|
|
|
// hh:mm:ss / mm:ss / m:ss
|
|
|
|
// 01:03:30 / 15:06 / 1:24
|
|
|
|
$durationText = 0;
|
|
|
|
if(isset($wrapper->lengthText)) {
|
|
|
|
$durationText = $wrapper->lengthText;
|
|
|
|
} else {
|
|
|
|
foreach($wrapper->thumbnailOverlays as $overlay) {
|
|
|
|
if(isset($overlay->thumbnailOverlayTimeStatusRenderer)) {
|
|
|
|
$durationText = $overlay->thumbnailOverlayTimeStatusRenderer->text;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($durationText->simpleText)) {
|
|
|
|
$durationText = trim($durationText->simpleText);
|
|
|
|
} else {
|
|
|
|
$durationText = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(preg_match('/([\d]{1,2}):([\d]{1,2})\:([\d]{2})/', $durationText)) {
|
|
|
|
$durationText = preg_replace('/([\d]{1,2}):([\d]{1,2})\:([\d]{2})/', '$1:$2:$3', $durationText);
|
|
|
|
} else {
|
|
|
|
$durationText = preg_replace('/([\d]{1,2})\:([\d]{2})/', '00:$1:$2', $durationText);
|
|
|
|
}
|
|
|
|
sscanf($durationText, '%d:%d:%d', $hours, $minutes, $seconds);
|
|
|
|
$duration = $hours * 3600 + $minutes * 60 + $seconds;
|
|
|
|
if($duration < $duration_min || $duration > $duration_max) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// $vid_list .= $vid . ',';
|
|
|
|
$this->ytBridgeQueryVideoInfo($vid, $author, $desc, $time);
|
|
|
|
$this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 02:24:53 +03:00
|
|
|
public function collectData(){
|
2016-05-04 00:45:32 +03:00
|
|
|
|
|
|
|
$xml = '';
|
|
|
|
$html = '';
|
|
|
|
$url_feed = '';
|
|
|
|
$url_listing = '';
|
2014-05-14 14:39:12 +04:00
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
if($this->getInput('u')) { /* User and Channel modes */
|
2016-08-28 02:25:33 +03:00
|
|
|
$this->request = $this->getInput('u');
|
2017-02-11 18:16:56 +03:00
|
|
|
$url_feed = self::URI . 'feeds/videos.xml?user=' . urlencode($this->request);
|
|
|
|
$url_listing = self::URI . 'user/' . urlencode($this->request) . '/videos';
|
2017-07-29 20:28:00 +03:00
|
|
|
} elseif($this->getInput('c')) {
|
2016-08-28 02:25:33 +03:00
|
|
|
$this->request = $this->getInput('c');
|
2017-02-11 18:16:56 +03:00
|
|
|
$url_feed = self::URI . 'feeds/videos.xml?channel_id=' . urlencode($this->request);
|
|
|
|
$url_listing = self::URI . 'channel/' . urlencode($this->request) . '/videos';
|
2021-09-11 11:20:14 +03:00
|
|
|
} elseif($this->getInput('custom')) {
|
|
|
|
$this->request = $this->getInput('custom');
|
|
|
|
$url_listing = self::URI . urlencode($this->request) . '/videos';
|
2016-05-04 00:45:32 +03:00
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2021-09-11 11:20:14 +03:00
|
|
|
if(!empty($url_feed) || !empty($url_listing)) {
|
|
|
|
$this->feeduri = $url_listing;
|
|
|
|
if(!empty($this->getInput('custom'))) {
|
|
|
|
$html = $this->ytGetSimpleHTMLDOM($url_listing);
|
|
|
|
$jsonData = $this->getJSONData($html);
|
|
|
|
$url_feed = $jsonData->metadata->channelMetadataRenderer->rssUrl;
|
|
|
|
}
|
|
|
|
if(!$this->skipFeeds()) {
|
|
|
|
$html = $this->ytGetSimpleHTMLDOM($url_feed);
|
|
|
|
$this->ytBridgeParseXmlFeed($html);
|
2017-02-11 18:16:56 +03:00
|
|
|
} else {
|
2021-09-11 11:20:14 +03:00
|
|
|
if(empty($this->getInput('custom'))) {
|
|
|
|
$html = $this->ytGetSimpleHTMLDOM($url_listing);
|
|
|
|
$jsonData = $this->getJSONData($html);
|
|
|
|
}
|
|
|
|
$channel_id = '';
|
|
|
|
if(isset($jsonData->contents)) {
|
|
|
|
$channel_id = $jsonData->metadata->channelMetadataRenderer->externalId;
|
|
|
|
$jsonData = $jsonData->contents->twoColumnBrowseResultsRenderer->tabs[1];
|
|
|
|
$jsonData = $jsonData->tabRenderer->content->sectionListRenderer->contents[0];
|
|
|
|
$jsonData = $jsonData->itemSectionRenderer->contents[0]->gridRenderer->items;
|
|
|
|
$this->parseJSONListing($jsonData);
|
|
|
|
} else {
|
|
|
|
returnServerError('Unable to get data from YouTube. Username/Channel: ' . $this->request);
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2021-09-11 11:20:14 +03:00
|
|
|
$this->feedName = str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
|
2017-07-29 20:28:00 +03:00
|
|
|
} elseif($this->getInput('p')) { /* playlist mode */
|
2021-05-16 23:02:45 +03:00
|
|
|
// TODO: this mode makes a lot of excess video query requests.
|
|
|
|
// To make less requests, we need to cache following dictionary "videoId -> datePublished, duration"
|
|
|
|
// This cache will be used to find out, which videos to fetch
|
|
|
|
// to make feed of 15 items or more, if there a lot of videos published on that date.
|
2016-08-28 02:25:33 +03:00
|
|
|
$this->request = $this->getInput('p');
|
2018-03-19 15:41:53 +03:00
|
|
|
$url_feed = self::URI . 'feeds/videos.xml?playlist_id=' . urlencode($this->request);
|
2017-02-11 18:16:56 +03:00
|
|
|
$url_listing = self::URI . 'playlist?list=' . urlencode($this->request);
|
2022-01-02 12:36:09 +03:00
|
|
|
$html = $this->ytGetSimpleHTMLDOM($url_listing);
|
2021-09-11 11:20:14 +03:00
|
|
|
$jsonData = $this->getJSONData($html);
|
2021-05-16 23:02:45 +03:00
|
|
|
// TODO: this method returns only first 100 video items
|
|
|
|
// if it has more videos, playlistVideoListRenderer will have continuationItemRenderer as last element
|
|
|
|
$jsonData = $jsonData->contents->twoColumnBrowseResultsRenderer->tabs[0];
|
|
|
|
$jsonData = $jsonData->tabRenderer->content->sectionListRenderer->contents[0]->itemSectionRenderer;
|
|
|
|
$jsonData = $jsonData->contents[0]->playlistVideoListRenderer->contents;
|
|
|
|
$item_count = count($jsonData);
|
2021-09-11 11:20:14 +03:00
|
|
|
|
2018-07-21 15:22:53 +03:00
|
|
|
if ($item_count <= 15 && !$this->skipFeeds() && ($xml = $this->ytGetSimpleHTMLDOM($url_feed))) {
|
2018-03-19 15:41:53 +03:00
|
|
|
$this->ytBridgeParseXmlFeed($xml);
|
|
|
|
} else {
|
2021-09-11 11:20:14 +03:00
|
|
|
$this->parseJSONListing($jsonData);
|
2018-03-19 15:41:53 +03:00
|
|
|
}
|
2017-12-29 04:14:11 +03:00
|
|
|
$this->feedName = 'Playlist: ' . str_replace(' - YouTube', '', $html->find('title', 0)->plaintext); // feedName will be used by getName()
|
2018-03-13 14:24:40 +03:00
|
|
|
usort($this->items, function ($item1, $item2) {
|
2021-09-11 11:20:14 +03:00
|
|
|
if(!is_int($item1['timestamp']) && !is_int($item2['timestamp'])) {
|
|
|
|
$item1['timestamp'] = strtotime($item1['timestamp']);
|
|
|
|
$item2['timestamp'] = strtotime($item2['timestamp']);
|
|
|
|
}
|
2018-03-13 14:24:40 +03:00
|
|
|
return $item2['timestamp'] - $item1['timestamp'];
|
|
|
|
});
|
2017-07-29 20:28:00 +03:00
|
|
|
} elseif($this->getInput('s')) { /* search mode */
|
2017-02-11 18:16:56 +03:00
|
|
|
$this->request = $this->getInput('s');
|
|
|
|
$url_listing = self::URI
|
|
|
|
. 'results?search_query='
|
|
|
|
. urlencode($this->request)
|
2021-09-11 11:20:14 +03:00
|
|
|
. '&sp=CAI%253D';
|
2014-06-20 19:00:36 +04:00
|
|
|
|
2022-01-02 12:36:09 +03:00
|
|
|
$html = $this->ytGetSimpleHTMLDOM($url_listing);
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2021-09-11 11:20:14 +03:00
|
|
|
$jsonData = $this->getJSONData($html);
|
|
|
|
$jsonData = $jsonData->contents->twoColumnSearchResultsRenderer->primaryContents;
|
|
|
|
$jsonData = $jsonData->sectionListRenderer->contents;
|
|
|
|
foreach($jsonData as $data) { // Search result includes some ads, have to filter them
|
|
|
|
if(isset($data->itemSectionRenderer->contents[0]->videoRenderer)) {
|
|
|
|
$jsonData = $data->itemSectionRenderer->contents;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->parseJSONListing($jsonData);
|
|
|
|
$this->feeduri = $url_listing;
|
|
|
|
$this->feedName = 'Search: ' . $this->request; // feedName will be used by getName()
|
2017-02-11 18:16:56 +03:00
|
|
|
} else { /* no valid mode */
|
|
|
|
returnClientError("You must either specify either:\n - YouTube
|
|
|
|
username (?u=...)\n - Channel id (?c=...)\n - Playlist id (?p=...)\n - Search (?s=...)");
|
2016-05-04 00:45:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 15:22:53 +03:00
|
|
|
private function skipFeeds() {
|
|
|
|
return ($this->getInput('duration_min') || $this->getInput('duration_max'));
|
|
|
|
}
|
|
|
|
|
2021-05-16 23:02:45 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
if (!is_null($this->getInput('p'))) {
|
|
|
|
return static::URI . 'playlist?list=' . $this->getInput('p');
|
2021-09-11 11:20:14 +03:00
|
|
|
} elseif($this->feeduri) {
|
|
|
|
return $this->feeduri;
|
2021-05-16 23:02:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getURI();
|
|
|
|
}
|
|
|
|
|
2014-06-20 19:00:36 +04:00
|
|
|
public function getName(){
|
2017-12-29 04:14:11 +03:00
|
|
|
// Name depends on queriedContext:
|
|
|
|
switch($this->queriedContext) {
|
|
|
|
case 'By username':
|
|
|
|
case 'By channel id':
|
2021-09-11 11:20:14 +03:00
|
|
|
case 'By custom name':
|
2017-12-29 04:14:11 +03:00
|
|
|
case 'By playlist Id':
|
|
|
|
case 'Search result':
|
2021-05-16 23:02:45 +03:00
|
|
|
return htmlspecialchars_decode($this->feedName) . ' - YouTube'; // We already know it's a bridge, right?
|
2017-12-29 04:19:35 +03:00
|
|
|
default:
|
|
|
|
return parent::getName();
|
2017-12-29 04:14:11 +03:00
|
|
|
}
|
2018-06-23 22:28:27 +03:00
|
|
|
}
|
2013-08-15 14:05:58 +04:00
|
|
|
}
|