YoutubeBridge.php 6.4 KB

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