YoutubeBridge.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 = getSimpleHTMLDOM(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. $vid = str_replace('yt:video:', '', $element->find('id', 0)->plaintext);
  73. $time = strtotime($element->find('published', 0)->plaintext);
  74. $this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
  75. }
  76. $this->request = $this->ytBridgeFixTitle($xml->find('feed > title', 0)->plaintext);
  77. }
  78. private function ytBridgeParseHtmlListing($html, $element_selector, $title_selector){
  79. $limit = 10;
  80. $count = 0;
  81. foreach($html->find($element_selector) as $element){
  82. if($count < $limit){
  83. $author = '';
  84. $desc = '';
  85. $time = 0;
  86. $vid = str_replace('/watch?v=', '', $element->find('a', 0)->href);
  87. $title = $this->ytBridgeFixTitle($element->find($title_selector, 0)->plaintext);
  88. if($title != '[Private Video]'){
  89. $this->ytBridgeQueryVideoInfo($vid, $author, $desc, $time);
  90. $this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
  91. $count++;
  92. }
  93. }
  94. }
  95. }
  96. private function ytBridgeFixTitle($title) {
  97. // convert both &#1234; and &quot; to UTF-8
  98. return html_entity_decode($title, ENT_QUOTES, 'UTF-8');
  99. }
  100. public function collectData(){
  101. $xml = '';
  102. $html = '';
  103. $url_feed = '';
  104. $url_listing = '';
  105. if($this->getInput('u')){ /* User and Channel modes */
  106. $this->request = $this->getInput('u');
  107. $url_feed = self::URI . 'feeds/videos.xml?user=' . urlencode($this->request);
  108. $url_listing = self::URI . 'user/' . urlencode($this->request) . '/videos';
  109. } elseif($this->getInput('c')){
  110. $this->request = $this->getInput('c');
  111. $url_feed = self::URI . 'feeds/videos.xml?channel_id=' . urlencode($this->request);
  112. $url_listing = self::URI . 'channel/' . urlencode($this->request) . '/videos';
  113. }
  114. if(!empty($url_feed) && !empty($url_listing)){
  115. if($xml = getSimpleHTMLDOM($url_feed)){
  116. $this->ytBridgeParseXmlFeed($xml);
  117. } elseif($html = getSimpleHTMLDOM($url_listing)){
  118. $this->ytBridgeParseHtmlListing($html, 'li.channels-content-item', 'h3');
  119. } else {
  120. returnServerError("Could not request YouTube. Tried:\n - $url_feed\n - $url_listing");
  121. }
  122. } elseif($this->getInput('p')){ /* playlist mode */
  123. $this->request = $this->getInput('p');
  124. $url_listing = self::URI . 'playlist?list=' . urlencode($this->request);
  125. $html = getSimpleHTMLDOM($url_listing)
  126. or returnServerError("Could not request YouTube. Tried:\n - $url_listing");
  127. $this->ytBridgeParseHtmlListing($html, 'tr.pl-video', '.pl-video-title a');
  128. $this->request = 'Playlist: ' . str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
  129. } elseif($this->getInput('s')){ /* search mode */
  130. $this->request = $this->getInput('s');
  131. $page = 1;
  132. if($this->getInput('pa'))
  133. $page = (int)preg_replace("/[^0-9]/", '', $this->getInput('pa'));
  134. $url_listing = self::URI
  135. . 'results?search_query='
  136. . urlencode($this->request)
  137. . '&page='
  138. . $page
  139. . '&filters=video&search_sort=video_date_uploaded';
  140. $html = getSimpleHTMLDOM($url_listing)
  141. or returnServerError("Could not request YouTube. Tried:\n - $url_listing");
  142. $this->ytBridgeParseHtmlListing($html, 'div.yt-lockup', 'h3');
  143. $this->request = 'Search: ' . str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
  144. } else { /* no valid mode */
  145. returnClientError("You must either specify either:\n - YouTube
  146. username (?u=...)\n - Channel id (?c=...)\n - Playlist id (?p=...)\n - Search (?s=...)");
  147. }
  148. }
  149. public function getName(){
  150. return (!empty($this->request) ? $this->request . ' - ' : '') . 'YouTube Bridge';
  151. }
  152. }