forked from blallo/rss-bridge
Youtube: fix, indent, limits
This commit is contained in:
parent
2f4b325a7d
commit
dcf554d8d8
1 changed files with 81 additions and 73 deletions
|
@ -2,12 +2,12 @@
|
||||||
/**
|
/**
|
||||||
* RssBridgeYoutube
|
* RssBridgeYoutube
|
||||||
* Returns the newest videos
|
* Returns the newest videos
|
||||||
* 2014-05-25
|
|
||||||
*
|
*
|
||||||
* @name Youtube Bridge
|
* @name Youtube Bridge
|
||||||
* @homepage https://www.youtube.com/
|
* @homepage https://www.youtube.com/
|
||||||
* @description Returns the newest videos by username or playlist
|
* @description Returns the 10 newest videos by username/playlist or search
|
||||||
* @maintainer mitsukarenai
|
* @maintainer mitsukarenai
|
||||||
|
* @update 2014-06-20
|
||||||
* @use1(u="username")
|
* @use1(u="username")
|
||||||
* @use2(p="playlist id")
|
* @use2(p="playlist id")
|
||||||
* @use3(s="search keyword",pa="page")
|
* @use3(s="search keyword",pa="page")
|
||||||
|
@ -18,85 +18,93 @@
|
||||||
*/
|
*/
|
||||||
class YoutubeBridge extends BridgeAbstract{
|
class YoutubeBridge extends BridgeAbstract{
|
||||||
|
|
||||||
private $request;
|
private $request;
|
||||||
|
|
||||||
public function collectData(array $param){
|
public function collectData(array $param){
|
||||||
|
|
||||||
function getPublishDate($id) {
|
function getPublishDate($id) {
|
||||||
# relies on Youtube API; deprecated
|
// relies on Youtube API; deprecated
|
||||||
$json = json_decode(file_get_contents("https://gdata.youtube.com/feeds/api/videos/$id?v=2&alt=json"), TRUE) or return time();
|
$json = json_decode(file_get_contents("https://gdata.youtube.com/feeds/api/videos/$id?v=2&alt=json"), TRUE) or $this->returnError('Youtube API is down', 404);
|
||||||
$timestamp = strtotime($json['entry']['published']['$t']);
|
$timestamp = strtotime($json['entry']['published']['$t']);
|
||||||
return $timestamp;
|
return $timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$html = '';
|
$html = '';
|
||||||
if (isset($param['u'])) { /* user timeline mode */
|
$limit = 10;
|
||||||
$this->request = $param['u'];
|
$count = 0;
|
||||||
$html = file_get_html('https://www.youtube.com/user/'.urlencode($this->request).'/videos') or $this->returnError('Could not request Youtube.', 404);
|
|
||||||
|
|
||||||
foreach($html->find('li.channels-content-item') as $element) {
|
if (isset($param['u'])) { /* user timeline mode */
|
||||||
$item = new \Item();
|
$this->request = $param['u'];
|
||||||
$item->uri = 'https://www.youtube.com'.$element->find('a',0)->href;
|
$html = file_get_html('https://www.youtube.com/user/'.urlencode($this->request).'/videos') or $this->returnError('Could not request Youtube.', 404);
|
||||||
$item->thumbnailUri = 'https:'.$element->find('img',0)->src;
|
|
||||||
$item->title = trim($element->find('h3',0)->plaintext);
|
|
||||||
$item->id = str_replace('/watch?v=', '', $element->find('a',0)->href);
|
|
||||||
$item->timestamp = getPublishDate($item->id);
|
|
||||||
$item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
|
|
||||||
$this->items[] = $item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (isset($param['p'])) { /* playlist mode */
|
|
||||||
$this->request = $param['p'];
|
|
||||||
$html = file_get_html('https://www.youtube.com/playlist?list='.urlencode($this->request).'') or $this->returnError('Could not request Youtube.', 404);
|
|
||||||
|
|
||||||
foreach($html->find('tr.pl-video') as $element) {
|
foreach($html->find('li.channels-content-item') as $element) {
|
||||||
$item = new \Item();
|
if($count < $limit) {
|
||||||
$item->uri = 'https://www.youtube.com'.$element->find('.pl-video-title a',0)->href;
|
$item = new \Item();
|
||||||
$item->thumbnailUri = 'https:'.str_replace('/default.','/mqdefault.',$element->find('.pl-video-thumbnail img',0)->src);
|
$item->uri = 'https://www.youtube.com'.$element->find('a',0)->href;
|
||||||
$item->title = trim($element->find('.pl-video-title a',0)->plaintext);
|
$item->thumbnailUri = 'https:'.$element->find('img',0)->src;
|
||||||
$item->id = str_replace('/watch?v=', '', $element->find('a',0)->href);
|
$item->title = trim($element->find('h3',0)->plaintext);
|
||||||
$item->timestamp = getPublishDate($item->id);
|
$item->id = str_replace('/watch?v=', '', $element->find('a',0)->href);
|
||||||
$item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
|
$item->timestamp = getPublishDate($item->id);
|
||||||
$this->items[] = $item;
|
$item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
|
||||||
}
|
$this->items[] = $item;
|
||||||
$this->request = 'Playlist '.trim(str_replace(' - YouTube', '', $html->find('title', 0)->plaintext)).', by '.$html->find('h1', 0)->plaintext;
|
$count++;
|
||||||
}
|
}
|
||||||
else if (isset($param['s'])) { /* search mode */
|
}
|
||||||
$this->request = $param['s']; $page = 1; if (isset($param['pa'])) $page = (int)preg_replace("/[^0-9]/",'', $param['pa']);
|
}
|
||||||
$html = file_get_html('https://www.youtube.com/results?search_query='.urlencode($this->request).'&&page='.$page.'&filters=video&search_sort=video_date_uploaded') or $this->returnError('Could not request Youtube.', 404);
|
|
||||||
|
|
||||||
foreach($html->find('li.yt-lockup') as $element) {
|
else if (isset($param['p'])) { /* playlist mode */
|
||||||
$item = new \Item();
|
$this->request = $param['p'];
|
||||||
$item->uri = 'https://www.youtube.com'.$element->find('a',0)->href;
|
$html = file_get_html('https://www.youtube.com/playlist?list='.urlencode($this->request).'') or $this->returnError('Could not request Youtube.', 404);
|
||||||
$checkthumb = $element->find('img', 0)->getAttribute('data-thumb');
|
|
||||||
if($checkthumb !== FALSE)
|
foreach($html->find('tr.pl-video') as $element) {
|
||||||
$item->thumbnailUri = $checkthumb;
|
if($count < $limit) {
|
||||||
else
|
$item = new \Item();
|
||||||
$item->thumbnailUri = ''.$element->find('img',0)->src;
|
$item->uri = 'https://www.youtube.com'.$element->find('.pl-video-title a',0)->href;
|
||||||
$item->title = trim($element->find('h3',0)->plaintext);
|
$item->thumbnailUri = 'https:'.str_replace('/default.','/mqdefault.',$element->find('.pl-video-thumbnail img',0)->src);
|
||||||
$item->id = str_replace('/watch?v=', '', $element->find('a',0)->href);
|
$item->title = trim($element->find('.pl-video-title a',0)->plaintext);
|
||||||
$item->timestamp = getPublishDate($item->id);
|
$item->id = str_replace('/watch?v=', '', $element->find('a',0)->href);
|
||||||
$item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
|
$item->timestamp = getPublishDate($item->id);
|
||||||
$this->items[] = $item;
|
$item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
|
||||||
}
|
$this->items[] = $item;
|
||||||
$this->request = 'Search: '.str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
|
$count++;
|
||||||
}
|
}
|
||||||
else {
|
$this->request = 'Playlist '.trim(str_replace(' - YouTube', '', $html->find('title', 0)->plaintext)).', by '.$html->find('h1', 0)->plaintext;
|
||||||
$this->returnError('You must either specify a Youtube username (?u=...) or a playlist id (?p=...) or search (?s=...)', 400);
|
}
|
||||||
|
|
||||||
|
else if (isset($param['s'])) { /* search mode */
|
||||||
|
$this->request = $param['s']; $page = 1; if (isset($param['pa'])) $page = (int)preg_replace("/[^0-9]/",'', $param['pa']);
|
||||||
|
$html = file_get_html('https://www.youtube.com/results?search_query='.urlencode($this->request).'&&page='.$page.'&filters=video&search_sort=video_date_uploaded') or $this->returnError('Could not request Youtube.', 404);
|
||||||
|
|
||||||
|
foreach($html->find('li.yt-lockup') as $element) {
|
||||||
|
$item = new \Item();
|
||||||
|
$item->uri = 'https://www.youtube.com'.$element->find('a',0)->href;
|
||||||
|
$checkthumb = $element->find('img', 0)->getAttribute('data-thumb');
|
||||||
|
if($checkthumb !== FALSE)
|
||||||
|
$item->thumbnailUri = $checkthumb;
|
||||||
|
else
|
||||||
|
$item->thumbnailUri = ''.$element->find('img',0)->src;
|
||||||
|
$item->title = trim($element->find('h3',0)->plaintext);
|
||||||
|
$item->id = str_replace('/watch?v=', '', $element->find('a',0)->href);
|
||||||
|
//$item->timestamp = getPublishDate($item->id); /* better not use it here */
|
||||||
|
$item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
|
||||||
|
$this->items[] = $item;
|
||||||
|
}
|
||||||
|
$this->request = 'Search: '.str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
$this->returnError('You must either specify a Youtube username (?u=...) or a playlist id (?p=...) or search (?s=...)', 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName(){
|
||||||
|
return (!empty($this->request) ? $this->request .' - ' : '') .'Youtube Bridge';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getName(){
|
public function getURI(){
|
||||||
return (!empty($this->request) ? $this->request .' - ' : '') .'Youtube Bridge';
|
return 'https://www.youtube.com/';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getURI(){
|
public function getCacheDuration(){
|
||||||
return 'https://www.youtube.com/';
|
return 10800; // 3 hours
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCacheDuration(){
|
|
||||||
return 10800; // 3 hours
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue