Browse Source

Updated from Mitsukarenai

Sébastien SAUVAGE 10 years ago
parent
commit
e9902e2391
1 changed files with 47 additions and 13 deletions
  1. 47 13
      bridges/YoutubeBridge.php

+ 47 - 13
bridges/YoutubeBridge.php

@@ -4,8 +4,10 @@
 * Returns the newest videos
 *
 * @name Youtube Bridge
-* @description Returns the newest videos
+* @description Returns the newest videos by username or playlist
 * @use1(u="username")
+* @use2(p="playlist id")
+* @use3(s="search keyword",pa="page")
 */
 class YoutubeBridge extends BridgeAbstract{
     
@@ -16,20 +18,52 @@ class YoutubeBridge extends BridgeAbstract{
         if (isset($param['u'])) {   /* user timeline mode */
             $this->request = $param['u'];
             $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) {
+            	 $item = new \Item();
+           	 $item->uri = 'https://www.youtube.com'.$element->find('a',0)->href;
+           	 $item->thumbnailUri = 'https:'.$element->find('img',0)->src;
+           	 $item->title = trim($element->find('h3',0)->plaintext);
+           	 $item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
+           	 $this->items[] = $item;
+        	}
         }
-        else {
-            $this->returnError('You must specify a Youtbe username (?u=...).', 400);
+        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('li.playlist-video-item') as $element) {
+           	 $item = new \Item();
+           	 $item->uri = 'https://www.youtube.com'.$element->find('a',0)->href;
+            	$item->thumbnailUri = 'https:'.$element->find('img',0)->src;
+            	$item->title = trim($element->find('h3',0)->plaintext);
+            	$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 '.str_replace(' - YouTube', '', $html->find('title', 0)->plaintext).', by '.$html->find('h1', 0)->plaintext;
         }
-        
-    
-        foreach($html->find('li.channels-content-item') as $element) {
-            $item = new \Item();
-            $item->uri = 'https://www.youtube.com'.$element->find('a',0)->href;
-            $item->thumbnailUri = 'https:'.$element->find('img',0)->src;
-            $item->title = trim($element->find('h3',0)->plaintext);
-            $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['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.context-data-item') 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->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(){
@@ -41,6 +75,6 @@ class YoutubeBridge extends BridgeAbstract{
     }
 
     public function getCacheDuration(){
-        return 21600; // 6 hours
+        return 10800; // 3 hours
     }
 }