YoutubeBridge.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * RssBridgeYoutube
  4. * Returns the newest videos
  5. * WARNING: to parse big playlists (over ~90 videos), you need to edit simple_html_dom.php:
  6. * change: define('MAX_FILE_SIZE', 600000);
  7. * into: define('MAX_FILE_SIZE', 900000); (or more)
  8. */
  9. class YoutubeBridge extends BridgeAbstract {
  10. public function loadMetadatas() {
  11. $this->name = 'YouTube Bridge';
  12. $this->homepage = $this->getURI();
  13. $this->description = 'Returns the 10 newest videos by username/channel/playlist or search';
  14. $this->maintainer = 'mitsukarenai';
  15. $this->update = '02/05/2016';
  16. $this->parameters['By username'] =
  17. '[
  18. {
  19. "type" : "text",
  20. "identifier" : "u",
  21. "name" : "username",
  22. "exampleValue" : "test",
  23. "required" : "required"
  24. }
  25. ]';
  26. $this->parameters['By channel id'] =
  27. '[
  28. {
  29. "type" : "text",
  30. "identifier" : "c",
  31. "name" : "channel id",
  32. "exampleValue" : "15",
  33. "required" : "required"
  34. }
  35. ]';
  36. $this->parameters['By playlist Id'] =
  37. '[
  38. {
  39. "type" : "text",
  40. "identifier" : "p",
  41. "name" : "playlist id",
  42. "exampleValue" : "15"
  43. }
  44. ]';
  45. $this->parameters['Search result'] =
  46. '[
  47. {
  48. "type" : "text",
  49. "identifier" : "s",
  50. "name" : "search keyword",
  51. "exampleValue" : "test"
  52. },
  53. {
  54. "type" : "number",
  55. "identifier" : "pa",
  56. "name" : "page",
  57. "exampleValue" : "1"
  58. }
  59. ]';
  60. }
  61. private function ytBridgeQueryVideoInfo($vid, &$author, &$desc, &$time) {
  62. $html = $this->file_get_html($this->getURI()."watch?v=$vid");
  63. $author = $html->innertext;
  64. $author = substr($author, strpos($author, '"author=') + 8);
  65. $author = substr($author, 0, strpos($author, '\u0026'));
  66. $desc = $html->find('div#watch-description-text', 0)->innertext;
  67. $time = strtotime($html->find('meta[itemprop=datePublished]', 0)->getAttribute('content'));
  68. }
  69. private function ytBridgeAddItem($vid, $title, $author, $desc, $time) {
  70. $item = new \Item();
  71. $item->id = $vid;
  72. $item->title = $title;
  73. $item->author = $author;
  74. $item->timestamp = $time;
  75. $item->uri = $this->getURI().'watch?v='.$vid;
  76. $item->thumbnailUri = str_replace('/www.', '/img.', $this->getURI()).'vi/'.$vid.'/0.jpg';
  77. $item->content = '<a href="'.$item->uri.'"><img src="'.$item->thumbnailUri.'" /></a><br />'.$desc;
  78. $this->items[] = $item;
  79. }
  80. private function ytBridgeParseXmlFeed($xml) {
  81. foreach ($xml->find('entry') as $element) {
  82. $title = $this->ytBridgeFixTitle($element->find('title',0)->plaintext);
  83. $author = $element->find('name', 0)->plaintext;
  84. $desc = $element->find('media:description', 0)->innertext;
  85. $vid = str_replace('yt:video:', '', $element->find('id', 0)->plaintext);
  86. $time = strtotime($element->find('published', 0)->plaintext);
  87. $this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
  88. }
  89. $this->request = $this->ytBridgeFixTitle($xml->find('feed > title', 0)->plaintext);
  90. }
  91. private function ytBridgeParseHtmlListing($html, $element_selector, $title_selector) {
  92. $limit = 10; $count = 0;
  93. foreach ($html->find($element_selector) as $element) {
  94. if ($count < $limit) {
  95. $author = ''; $desc = ''; $time = 0;
  96. $vid = str_replace('/watch?v=', '', $element->find('a', 0)->href);
  97. $title = $this->ytBridgeFixTitle($element->find($title_selector, 0)->plaintext);
  98. if ($title != '[Private Video]') {
  99. $this->ytBridgeQueryVideoInfo($vid, $author, $desc, $time);
  100. $this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
  101. $count++;
  102. }
  103. }
  104. }
  105. }
  106. private function ytBridgeFixTitle($title) {
  107. // convert both &#1234; and &quot; to UTF-8
  108. return html_entity_decode($title,ENT_QUOTES,'UTF-8');
  109. }
  110. public function collectData(array $param) {
  111. $xml = '';
  112. $html = '';
  113. $url_feed = '';
  114. $url_listing = '';
  115. if (isset($param['u'])) { /* User and Channel modes */
  116. $this->request = $param['u'];
  117. $url_feed = $this->getURI().'feeds/videos.xml?user='.urlencode($this->request);
  118. $url_listing = $this->getURI().'user/'.urlencode($this->request).'/videos';
  119. } else if (isset($param['c'])) {
  120. $this->request = $param['c'];
  121. $url_feed = $this->getURI().'feeds/videos.xml?channel_id='.urlencode($this->request);
  122. $url_listing = $this->getURI().'channel/'.urlencode($this->request).'/videos';
  123. }
  124. if (!empty($url_feed) && !empty($url_listing)) {
  125. if ($xml = $this->file_get_html($url_feed)) {
  126. $this->ytBridgeParseXmlFeed($xml);
  127. } else if ($html = $this->file_get_html($url_listing)) {
  128. $this->ytBridgeParseHtmlListing($html, 'li.channels-content-item', 'h3');
  129. } else $this->returnError("Could not request YouTube. Tried:\n - $url_feed\n - $url_listing", 500);
  130. }
  131. else if (isset($param['p'])) { /* playlist mode */
  132. $this->request = $param['p'];
  133. $url_listing = $this->getURI().'playlist?list='.urlencode($this->request);
  134. $html = $this->file_get_html($url_listing) or $this->returnError("Could not request YouTube. Tried:\n - $url_listing", 500);
  135. $this->ytBridgeParseHtmlListing($html, 'tr.pl-video', '.pl-video-title a');
  136. $this->request = 'Playlist: '.str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
  137. }
  138. else if (isset($param['s'])) { /* search mode */
  139. $this->request = $param['s']; $page = 1; if (isset($param['pa'])) $page = (int)preg_replace("/[^0-9]/",'', $param['pa']);
  140. $url_listing = $this->getURI().'results?search_query='.urlencode($this->request).'&page='.$page.'&filters=video&search_sort=video_date_uploaded';
  141. $html = $this->file_get_html($url_listing) or $this->returnError("Could not request YouTube. Tried:\n - $url_listing", 500);
  142. $this->ytBridgeParseHtmlListing($html, 'div.yt-lockup', 'h3');
  143. $this->request = 'Search: '.str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
  144. }
  145. else { /* no valid mode */
  146. $this->returnError("You must either specify either:\n - YouTube username (?u=...)\n - Channel id (?c=...)\n - Playlist id (?p=...)\n - Search (?s=...)", 400);
  147. }
  148. }
  149. public function getName(){
  150. return (!empty($this->request) ? $this->request .' - ' : '') .'YouTube Bridge';
  151. }
  152. public function getURI(){
  153. return 'https://www.youtube.com/';
  154. }
  155. public function getCacheDuration(){
  156. return 10800; // 3 hours
  157. }
  158. }