YoutubeBridge.php 5.9 KB

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