YoutubeBridge.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 DESCRIPTION = 'Returns the 10 newest videos by username/channel/playlist or search';
  13. const MAINTAINER = 'mitsukarenai';
  14. const PARAMETERS = array(
  15. 'By username' => array(
  16. 'u'=>array(
  17. 'name'=>'username',
  18. 'exampleValue'=>'test',
  19. 'required'=>true
  20. )
  21. ),
  22. 'By channel id' => array(
  23. 'c'=>array(
  24. 'name'=>'channel id',
  25. 'exampleValue'=>"15",
  26. 'required'=>true
  27. )
  28. ),
  29. 'By playlist Id' => array(
  30. 'p'=>array(
  31. 'name'=>'playlist id',
  32. 'exampleValue'=>"15"
  33. )
  34. ),
  35. 'Search result' => array(
  36. 's'=>array(
  37. 'name'=>'search keyword',
  38. 'exampleValue'=>'test'
  39. ),
  40. 'pa'=>array(
  41. 'name'=>'page',
  42. 'type'=>'number',
  43. 'exampleValue'=>1
  44. )
  45. )
  46. );
  47. private function ytBridgeQueryVideoInfo($vid, &$author, &$desc, &$time) {
  48. $html = getSimpleHTMLDOM(self::URI."watch?v=$vid");
  49. $author = $html->innertext;
  50. $author = substr($author, strpos($author, '"author=') + 8);
  51. $author = substr($author, 0, strpos($author, '\u0026'));
  52. $desc = $html->find('div#watch-description-text', 0)->innertext;
  53. $time = strtotime($html->find('meta[itemprop=datePublished]', 0)->getAttribute('content'));
  54. }
  55. private function ytBridgeAddItem($vid, $title, $author, $desc, $time) {
  56. $item = array();
  57. $item['id'] = $vid;
  58. $item['title'] = $title;
  59. $item['author'] = $author;
  60. $item['timestamp'] = $time;
  61. $item['uri'] = self::URI.'watch?v='.$vid;
  62. $thumbnailUri = str_replace('/www.', '/img.', self::URI).'vi/'.$vid.'/0.jpg';
  63. $item['content'] = '<a href="'.$item['uri'].'"><img src="'.$thumbnailUri.'" /></a><br />'.$desc;
  64. $this->items[] = $item;
  65. }
  66. private function ytBridgeParseXmlFeed($xml) {
  67. foreach ($xml->find('entry') as $element) {
  68. $title = $this->ytBridgeFixTitle($element->find('title',0)->plaintext);
  69. $author = $element->find('name', 0)->plaintext;
  70. $desc = $element->find('media:description', 0)->innertext;
  71. $vid = str_replace('yt:video:', '', $element->find('id', 0)->plaintext);
  72. $time = strtotime($element->find('published', 0)->plaintext);
  73. $this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
  74. }
  75. $this->request = $this->ytBridgeFixTitle($xml->find('feed > title', 0)->plaintext);
  76. }
  77. private function ytBridgeParseHtmlListing($html, $element_selector, $title_selector) {
  78. $limit = 10; $count = 0;
  79. foreach ($html->find($element_selector) as $element) {
  80. if ($count < $limit) {
  81. $author = ''; $desc = ''; $time = 0;
  82. $vid = str_replace('/watch?v=', '', $element->find('a', 0)->href);
  83. $title = $this->ytBridgeFixTitle($element->find($title_selector, 0)->plaintext);
  84. if ($title != '[Private Video]') {
  85. $this->ytBridgeQueryVideoInfo($vid, $author, $desc, $time);
  86. $this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
  87. $count++;
  88. }
  89. }
  90. }
  91. }
  92. private function ytBridgeFixTitle($title) {
  93. // convert both &#1234; and &quot; to UTF-8
  94. return html_entity_decode($title,ENT_QUOTES,'UTF-8');
  95. }
  96. public function collectData(){
  97. $xml = '';
  98. $html = '';
  99. $url_feed = '';
  100. $url_listing = '';
  101. if ($this->getInput('u')) { /* User and Channel modes */
  102. $this->request = $this->getInput('u');
  103. $url_feed = self::URI.'feeds/videos.xml?user='.urlencode($this->request);
  104. $url_listing = self::URI.'user/'.urlencode($this->request).'/videos';
  105. } else if ($this->getInput('c')) {
  106. $this->request = $this->getInput('c');
  107. $url_feed = self::URI.'feeds/videos.xml?channel_id='.urlencode($this->request);
  108. $url_listing = self::URI.'channel/'.urlencode($this->request).'/videos';
  109. }
  110. if (!empty($url_feed) && !empty($url_listing)) {
  111. if ($xml = getSimpleHTMLDOM($url_feed)) {
  112. $this->ytBridgeParseXmlFeed($xml);
  113. } else if ($html = getSimpleHTMLDOM($url_listing)) {
  114. $this->ytBridgeParseHtmlListing($html, 'li.channels-content-item', 'h3');
  115. } else returnServerError("Could not request YouTube. Tried:\n - $url_feed\n - $url_listing");
  116. }
  117. else if ($this->getInput('p')) { /* playlist mode */
  118. $this->request = $this->getInput('p');
  119. $url_listing = self::URI.'playlist?list='.urlencode($this->request);
  120. $html = getSimpleHTMLDOM($url_listing) or returnServerError("Could not request YouTube. Tried:\n - $url_listing");
  121. $this->ytBridgeParseHtmlListing($html, 'tr.pl-video', '.pl-video-title a');
  122. $this->request = 'Playlist: '.str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
  123. }
  124. else if ($this->getInput('s')) { /* search mode */
  125. $this->request = $this->getInput('s'); $page = 1; if ($this->getInput('pa')) $page = (int)preg_replace("/[^0-9]/",'', $this->getInput('pa'));
  126. $url_listing = self::URI.'results?search_query='.urlencode($this->request).'&page='.$page.'&filters=video&search_sort=video_date_uploaded';
  127. $html = getSimpleHTMLDOM($url_listing) or returnServerError("Could not request YouTube. Tried:\n - $url_listing");
  128. $this->ytBridgeParseHtmlListing($html, 'div.yt-lockup', 'h3');
  129. $this->request = 'Search: '.str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
  130. }
  131. else { /* no valid mode */
  132. returnClientError("You must either specify either:\n - YouTube username (?u=...)\n - Channel id (?c=...)\n - Playlist id (?p=...)\n - Search (?s=...)");
  133. }
  134. }
  135. public function getName(){
  136. return (!empty($this->request) ? $this->request .' - ' : '') .'YouTube Bridge';
  137. }
  138. public function getCacheDuration(){
  139. return 10800; // 3 hours
  140. }
  141. }