1
0

YoutubeBridge.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * RssBridgeYoutube
  4. * Returns the newest videos
  5. *
  6. * @name Youtube Bridge
  7. * @homepage https://www.youtube.com/
  8. * @description Returns the 10 newest videos by username/channel/playlist or search
  9. * @maintainer mitsukarenai
  10. * @update 2014-06-20
  11. * @use1(u="username")
  12. * @use2(c="channel id")
  13. * @use3(p="playlist id")
  14. * @use4(s="search keyword",pa="page")
  15. *
  16. * WARNING: to parse big playlists (over ~90 videos), you need to edit simple_html_dom.php:
  17. * change: define('MAX_FILE_SIZE', 600000);
  18. * into: define('MAX_FILE_SIZE', 900000); (or more)
  19. */
  20. class YoutubeBridge extends BridgeAbstract{
  21. private $request;
  22. public function collectData(array $param){
  23. function getPublishDate($id) {
  24. // relies on Youtube API; deprecated
  25. $json = json_decode(file_get_contents("https://gdata.youtube.com/feeds/api/videos/$id?v=2&alt=json"), TRUE);
  26. $timestamp = strtotime($json['entry']['published']['$t']);
  27. return $timestamp;
  28. }
  29. $html = '';
  30. $limit = 10;
  31. $count = 0;
  32. if (isset($param['u'])) { /* user timeline mode */
  33. $this->request = $param['u'];
  34. $html = file_get_html('https://www.youtube.com/user/'.urlencode($this->request).'/videos') or $this->returnError('Could not request Youtube.', 404);
  35. foreach($html->find('li.channels-content-item') as $element) {
  36. if($count < $limit) {
  37. $item = new \Item();
  38. $videoquery = parse_url($element->find('a',0)->href, PHP_URL_QUERY); parse_str($videoquery, $videoquery);
  39. $item->id = $videoquery['v'];
  40. $item->uri = 'https://www.youtube.com/watch?v='.$item->id;
  41. $item->thumbnailUri = 'https:'.$element->find('img',0)->src;
  42. $item->title = trim($element->find('h3',0)->plaintext);
  43. $item->timestamp = getPublishDate($item->id);
  44. $item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
  45. $this->items[] = $item;
  46. $count++;
  47. }
  48. }
  49. }
  50. else if (isset($param['c'])) { /* channel timeline mode */
  51. $this->request = $param['c'];
  52. $html = file_get_html('https://www.youtube.com/channel/'.urlencode($this->request).'/videos') or $this->returnError('Could not request Youtube.', 404);
  53. foreach($html->find('li.channels-content-item') as $element) {
  54. if($count < $limit) {
  55. $item = new \Item();
  56. $videoquery = parse_url($element->find('a',0)->href, PHP_URL_QUERY); parse_str($videoquery, $videoquery);
  57. $item->id = $videoquery['v'];
  58. $item->uri = 'https://www.youtube.com/watch?v='.$item->id;
  59. $item->thumbnailUri = 'https:'.$element->find('img',0)->src;
  60. $item->title = trim($element->find('h3',0)->plaintext);
  61. $item->timestamp = getPublishDate($item->id);
  62. $item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
  63. $this->items[] = $item;
  64. $count++;
  65. }
  66. }
  67. }
  68. else if (isset($param['p'])) { /* playlist mode */
  69. $this->request = $param['p'];
  70. $html = file_get_html('https://www.youtube.com/playlist?list='.urlencode($this->request).'') or $this->returnError('Could not request Youtube.', 404);
  71. foreach($html->find('tr.pl-video') as $element) {
  72. if($count < $limit) {
  73. $item = new \Item();
  74. $item->uri = 'https://www.youtube.com'.$element->find('.pl-video-title a',0)->href;
  75. $item->thumbnailUri = 'https:'.str_replace('/default.','/mqdefault.',$element->find('.pl-video-thumbnail img',0)->src);
  76. $item->title = trim($element->find('.pl-video-title a',0)->plaintext);
  77. $item->id = str_replace('/watch?v=', '', $element->find('a',0)->href);
  78. $item->timestamp = getPublishDate($item->id);
  79. $item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
  80. $this->items[] = $item;
  81. $count++;
  82. }
  83. $this->request = 'Playlist '.trim(str_replace(' - YouTube', '', $html->find('title', 0)->plaintext)).', by '.$html->find('h1', 0)->plaintext;
  84. }
  85. }
  86. else if (isset($param['s'])) { /* search mode */
  87. $this->request = $param['s']; $page = 1; if (isset($param['pa'])) $page = (int)preg_replace("/[^0-9]/",'', $param['pa']);
  88. $html = file_get_html('https://www.youtube.com/results?search_query='.urlencode($this->request).'&&page='.$page.'&filters=video&search_sort=video_date_uploaded') or $this->returnError('Could not request Youtube.', 404);
  89. foreach($html->find('li.yt-lockup') as $element) {
  90. $item = new \Item();
  91. $item->uri = 'https://www.youtube.com'.$element->find('a',0)->href;
  92. $checkthumb = $element->find('img', 0)->getAttribute('data-thumb');
  93. if($checkthumb !== FALSE)
  94. $item->thumbnailUri = $checkthumb;
  95. else
  96. $item->thumbnailUri = ''.$element->find('img',0)->src;
  97. $item->title = trim($element->find('h3',0)->plaintext);
  98. $item->id = str_replace('/watch?v=', '', $element->find('a',0)->href);
  99. //$item->timestamp = getPublishDate($item->id); /* better not use it here */
  100. $item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
  101. $this->items[] = $item;
  102. }
  103. $this->request = 'Search: '.str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
  104. }
  105. else
  106. $this->returnError('You must either specify a Youtube username (?u=...) or a channel id (?c=...) or a playlist id (?p=...) or search (?s=...)', 400);
  107. }
  108. public function getName(){
  109. return (!empty($this->request) ? $this->request .' - ' : '') .'Youtube Bridge';
  110. }
  111. public function getURI(){
  112. return 'https://www.youtube.com/';
  113. }
  114. public function getCacheDuration(){
  115. return 10800; // 3 hours
  116. }
  117. }