DailymotionBridge.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. class DailymotionBridge extends BridgeAbstract {
  3. const MAINTAINER = 'mitsukarenai';
  4. const NAME = 'Dailymotion Bridge';
  5. const URI = 'https://www.dailymotion.com/';
  6. const CACHE_TIMEOUT = 10800; // 3h
  7. const DESCRIPTION = 'Returns the 5 newest videos by username/playlist or search';
  8. const PARAMETERS = array (
  9. 'By username' => array(
  10. 'u' => array(
  11. 'name' => 'username',
  12. 'required' => true
  13. )
  14. ),
  15. 'By playlist id' => array(
  16. 'p' => array(
  17. 'name' => 'playlist id',
  18. 'required' => true
  19. )
  20. ),
  21. 'From search results' => array(
  22. 's' => array(
  23. 'name' => 'Search keyword',
  24. 'required' => true
  25. ),
  26. 'pa' => array(
  27. 'name' => 'Page',
  28. 'type' => 'number'
  29. )
  30. )
  31. );
  32. protected function getMetadata($id){
  33. $metadata = array();
  34. $html2 = getSimpleHTMLDOM(self::URI . 'video/' . $id);
  35. if(!$html2) {
  36. return $metadata;
  37. }
  38. $metadata['title'] = $html2->find('meta[property=og:title]', 0)->getAttribute('content');
  39. $metadata['timestamp'] = strtotime(
  40. $html2->find('meta[property=video:release_date]', 0)->getAttribute('content')
  41. );
  42. $metadata['thumbnailUri'] = $html2->find('meta[property=og:image]', 0)->getAttribute('content');
  43. $metadata['uri'] = $html2->find('meta[property=og:url]', 0)->getAttribute('content');
  44. return $metadata;
  45. }
  46. public function collectData(){
  47. $html = '';
  48. $limit = 5;
  49. $count = 0;
  50. $html = getSimpleHTMLDOM($this->getURI())
  51. or returnServerError('Could not request Dailymotion.');
  52. foreach($html->find('div.media a.preview_link') as $element) {
  53. if($count < $limit) {
  54. $item = array();
  55. $item['id'] = str_replace('/video/', '', strtok($element->href, '_'));
  56. $metadata = $this->getMetadata($item['id']);
  57. if(empty($metadata)) {
  58. continue;
  59. }
  60. $item['uri'] = $metadata['uri'];
  61. $item['title'] = $metadata['title'];
  62. $item['timestamp'] = $metadata['timestamp'];
  63. $item['content'] = '<a href="'
  64. . $item['uri']
  65. . '"><img src="'
  66. . $metadata['thumbnailUri']
  67. . '" /></a><br><a href="'
  68. . $item['uri']
  69. . '">'
  70. . $item['title']
  71. . '</a>';
  72. $this->items[] = $item;
  73. $count++;
  74. }
  75. }
  76. }
  77. public function getName(){
  78. switch($this->queriedContext) {
  79. case 'By username':
  80. $specific = $this->getInput('u');
  81. break;
  82. case 'By playlist id':
  83. $specific = strtok($this->getInput('p'), '_');
  84. break;
  85. case 'From search results':
  86. $specific = $this->getInput('s');
  87. break;
  88. default: return parent::getName();
  89. }
  90. return $specific . ' : Dailymotion Bridge';
  91. }
  92. public function getURI(){
  93. $uri = self::URI;
  94. switch($this->queriedContext) {
  95. case 'By username':
  96. $uri .= 'user/' . urlencode($this->getInput('u')) . '/1';
  97. break;
  98. case 'By playlist id':
  99. $uri .= 'playlist/' . urlencode(strtok($this->getInput('p'), '_'));
  100. break;
  101. case 'From search results':
  102. $uri .= 'search/' . urlencode($this->getInput('s'));
  103. if($this->getInput('pa')) {
  104. $uri .= '/' . $this->getInput('pa');
  105. }
  106. break;
  107. default: return parent::getURI();
  108. }
  109. return $uri;
  110. }
  111. }