YoutubeBridge.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. const NAME = 'YouTube Bridge';
  11. const URI = 'https://www.youtube.com/';
  12. const CACHE_TIMEOUT = 10800; // 3h
  13. const DESCRIPTION = 'Returns the 10 newest videos by username/channel/playlist or search';
  14. const MAINTAINER = 'mitsukarenai';
  15. const PARAMETERS = array(
  16. 'By username' => array(
  17. 'u' => array(
  18. 'name' => 'username',
  19. 'exampleValue' => 'test',
  20. 'required' => true
  21. )
  22. ),
  23. 'By channel id' => array(
  24. 'c' => array(
  25. 'name' => 'channel id',
  26. 'exampleValue' => "15",
  27. 'required' => true
  28. )
  29. ),
  30. 'By playlist Id' => array(
  31. 'p' => array(
  32. 'name' => 'playlist id',
  33. 'exampleValue' => "15"
  34. )
  35. ),
  36. 'Search result' => array(
  37. 's' => array(
  38. 'name' => 'search keyword',
  39. 'exampleValue' => 'test'
  40. ),
  41. 'pa' => array(
  42. 'name' => 'page',
  43. 'type' => 'number',
  44. 'exampleValue' => 1
  45. )
  46. )
  47. );
  48. private function ytBridgeQueryVideoInfo($vid, &$author, &$desc, &$time){
  49. $html = $this->ytGetSimpleHTMLDOM(self::URI . "watch?v=$vid");
  50. $author = $html->innertext;
  51. $author = substr($author, strpos($author, '"author=') + 8);
  52. $author = substr($author, 0, strpos($author, '\u0026'));
  53. if(!is_null($html->find('div#watch-description-text', 0)))
  54. $desc = $html->find('div#watch-description-text', 0)->innertext;
  55. if(!is_null($html->find('meta[itemprop=datePublished]', 0)))
  56. $time = strtotime($html->find('meta[itemprop=datePublished]', 0)->getAttribute('content'));
  57. }
  58. private function ytBridgeAddItem($vid, $title, $author, $desc, $time){
  59. $item = array();
  60. $item['id'] = $vid;
  61. $item['title'] = $title;
  62. $item['author'] = $author;
  63. $item['timestamp'] = $time;
  64. $item['uri'] = self::URI . 'watch?v=' . $vid;
  65. $thumbnailUri = str_replace('/www.', '/img.', self::URI) . 'vi/' . $vid . '/0.jpg';
  66. $item['content'] = '<a href="' . $item['uri'] . '"><img src="' . $thumbnailUri . '" /></a><br />' . $desc;
  67. $this->items[] = $item;
  68. }
  69. private function ytBridgeParseXmlFeed($xml) {
  70. foreach($xml->find('entry') as $element) {
  71. $title = $this->ytBridgeFixTitle($element->find('title', 0)->plaintext);
  72. $author = $element->find('name', 0)->plaintext;
  73. $desc = $element->find('media:description', 0)->innertext;
  74. // Make sure the description is easy on the eye :)
  75. $desc = htmlspecialchars($desc);
  76. $desc = nl2br($desc);
  77. $desc = preg_replace('/(http[s]{0,1}\:\/\/[a-zA-Z0-9.\/\?\&=\-_]{4,})/ims',
  78. '<a href="$1" target="_blank">$1</a> ',
  79. $desc);
  80. $vid = str_replace('yt:video:', '', $element->find('id', 0)->plaintext);
  81. $time = strtotime($element->find('published', 0)->plaintext);
  82. $this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
  83. }
  84. $this->request = $this->ytBridgeFixTitle($xml->find('feed > title', 0)->plaintext);
  85. }
  86. private function ytBridgeParseHtmlListing($html, $element_selector, $title_selector){
  87. $limit = 10;
  88. $count = 0;
  89. foreach($html->find($element_selector) as $element) {
  90. if($count < $limit) {
  91. $author = '';
  92. $desc = '';
  93. $time = 0;
  94. $vid = str_replace('/watch?v=', '', $element->find('a', 0)->href);
  95. $vid = substr($vid, 0, strpos($vid, '&') ?: strlen($vid));
  96. $title = $this->ytBridgeFixTitle($element->find($title_selector, 0)->plaintext);
  97. if($title != '[Private Video]') {
  98. $this->ytBridgeQueryVideoInfo($vid, $author, $desc, $time);
  99. $this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
  100. $count++;
  101. }
  102. }
  103. }
  104. }
  105. private function ytBridgeFixTitle($title) {
  106. // convert both &#1234; and &quot; to UTF-8
  107. return html_entity_decode($title, ENT_QUOTES, 'UTF-8');
  108. }
  109. private function ytGetSimpleHTMLDOM($url){
  110. return getSimpleHTMLDOM($url,
  111. $use_include_path = false,
  112. $context = null,
  113. $offset = 0,
  114. $maxLen = null,
  115. $lowercase = true,
  116. $forceTagsClosed = true,
  117. $target_charset = DEFAULT_TARGET_CHARSET,
  118. $stripRN = false,
  119. $defaultBRText = DEFAULT_BR_TEXT,
  120. $defaultSpanText = DEFAULT_SPAN_TEXT);
  121. }
  122. public function collectData(){
  123. $xml = '';
  124. $html = '';
  125. $url_feed = '';
  126. $url_listing = '';
  127. if($this->getInput('u')) { /* User and Channel modes */
  128. $this->request = $this->getInput('u');
  129. $url_feed = self::URI . 'feeds/videos.xml?user=' . urlencode($this->request);
  130. $url_listing = self::URI . 'user/' . urlencode($this->request) . '/videos';
  131. } elseif($this->getInput('c')) {
  132. $this->request = $this->getInput('c');
  133. $url_feed = self::URI . 'feeds/videos.xml?channel_id=' . urlencode($this->request);
  134. $url_listing = self::URI . 'channel/' . urlencode($this->request) . '/videos';
  135. }
  136. if(!empty($url_feed) && !empty($url_listing)) {
  137. if($xml = $this->ytGetSimpleHTMLDOM($url_feed)) {
  138. $this->ytBridgeParseXmlFeed($xml);
  139. } elseif($html = $this->ytGetSimpleHTMLDOM($url_listing)) {
  140. $this->ytBridgeParseHtmlListing($html, 'li.channels-content-item', 'h3');
  141. } else {
  142. returnServerError("Could not request YouTube. Tried:\n - $url_feed\n - $url_listing");
  143. }
  144. } elseif($this->getInput('p')) { /* playlist mode */
  145. $this->request = $this->getInput('p');
  146. $url_listing = self::URI . 'playlist?list=' . urlencode($this->request);
  147. $html = $this->ytGetSimpleHTMLDOM($url_listing)
  148. or returnServerError("Could not request YouTube. Tried:\n - $url_listing");
  149. $this->ytBridgeParseHtmlListing($html, 'tr.pl-video', '.pl-video-title a');
  150. $this->request = 'Playlist: ' . str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
  151. } elseif($this->getInput('s')) { /* search mode */
  152. $this->request = $this->getInput('s');
  153. $page = 1;
  154. if($this->getInput('pa'))
  155. $page = (int)preg_replace("/[^0-9]/", '', $this->getInput('pa'));
  156. $url_listing = self::URI
  157. . 'results?search_query='
  158. . urlencode($this->request)
  159. . '&page='
  160. . $page
  161. . '&filters=video&search_sort=video_date_uploaded';
  162. $html = $this->ytGetSimpleHTMLDOM($url_listing)
  163. or returnServerError("Could not request YouTube. Tried:\n - $url_listing");
  164. $this->ytBridgeParseHtmlListing($html, 'div.yt-lockup', 'h3');
  165. $this->request = 'Search: ' . str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
  166. } else { /* no valid mode */
  167. returnClientError("You must either specify either:\n - YouTube
  168. username (?u=...)\n - Channel id (?c=...)\n - Playlist id (?p=...)\n - Search (?s=...)");
  169. }
  170. }
  171. public function getName(){
  172. return (!empty($this->request) ? $this->request . ' - ' : '') . 'YouTube Bridge';
  173. }
  174. }