[DailymotionBridge] Use API for playlist and user account feeds (#1217)

This commit is contained in:
Joseph 2019-09-07 16:42:45 +00:00 committed by Lyra
parent b47189921f
commit 2ff27b92ff
1 changed files with 114 additions and 38 deletions

View File

@ -4,7 +4,7 @@ class DailymotionBridge extends BridgeAbstract {
const MAINTAINER = 'mitsukarenai'; const MAINTAINER = 'mitsukarenai';
const NAME = 'Dailymotion Bridge'; const NAME = 'Dailymotion Bridge';
const URI = 'https://www.dailymotion.com/'; const URI = 'https://www.dailymotion.com/';
const CACHE_TIMEOUT = 10800; // 3h const CACHE_TIMEOUT = 3600; // 1h
const DESCRIPTION = 'Returns the 5 newest videos by username/playlist or search'; const DESCRIPTION = 'Returns the 5 newest videos by username/playlist or search';
const PARAMETERS = array ( const PARAMETERS = array (
@ -27,74 +27,99 @@ class DailymotionBridge extends BridgeAbstract {
), ),
'pa' => array( 'pa' => array(
'name' => 'Page', 'name' => 'Page',
'type' => 'number' 'type' => 'number',
'defaultValue' => 1,
) )
) )
); );
protected function getMetadata($id){ private $feedName = '';
$metadata = array();
$html2 = getSimpleHTMLDOM(self::URI . 'video/' . $id);
if(!$html2) {
return $metadata;
}
$metadata['title'] = $html2->find('meta[property=og:title]', 0)->getAttribute('content'); private $apiUrl = 'https://api.dailymotion.com';
$metadata['timestamp'] = strtotime( private $apiFields = 'created_time,description,id,owner.screenname,tags,thumbnail_url,title,url';
$html2->find('meta[property=video:release_date]', 0)->getAttribute('content')
);
$metadata['thumbnailUri'] = $html2->find('meta[property=og:image]', 0)->getAttribute('content');
$metadata['uri'] = $html2->find('meta[property=og:url]', 0)->getAttribute('content');
return $metadata;
}
public function getIcon() { public function getIcon() {
return 'https://static1-ssl.dmcdn.net/images/neon/favicons/android-icon-36x36.png.vf806ca4ed0deed812'; return 'https://static1-ssl.dmcdn.net/images/neon/favicons/android-icon-36x36.png.vf806ca4ed0deed812';
} }
public function collectData(){ public function collectData() {
$html = '';
$limit = 5;
$count = 0;
$html = getSimpleHTMLDOM($this->getURI()) if ($this->queriedContext === 'By username' || $this->queriedContext === 'By playlist id') {
or returnServerError('Could not request Dailymotion.');
foreach($html->find('div.media a.preview_link') as $element) { $apiJson = getContents($this->getApiUrl())
if($count < $limit) { or returnServerError('Could not request: ' . $this->getApiUrl());
$apiData = json_decode($apiJson, true);
$this->feedName = $this->getPlaylistTitle($this->getInput('p'));
foreach ($apiData['list'] as $apiItem) {
$item = array(); $item = array();
$item['uri'] = $apiItem['url'];
$item['uid'] = $apiItem['id'];
$item['title'] = $apiItem['title'];
$item['timestamp'] = $apiItem['created_time'];
$item['author'] = $apiItem['owner.screenname'];
$item['content'] = '<p><a href="' . $apiItem['url'] . '">
<img src="' . $apiItem['thumbnail_url'] . '"></a></p><p>' . $apiItem['description'] . '</p>';
$item['categories'] = $apiItem['tags'];
$item['enclosures'][] = $apiItem['thumbnail_url'];
$this->items[] = $item;
}
}
if ($this->queriedContext === 'From search results') {
$html = getSimpleHTMLDOM($this->getURI())
or returnServerError('Could not request Dailymotion.');
foreach($html->find('div.media a.preview_link') as $element) {
$item = array();
$item['id'] = str_replace('/video/', '', strtok($element->href, '_')); $item['id'] = str_replace('/video/', '', strtok($element->href, '_'));
$metadata = $this->getMetadata($item['id']); $metadata = $this->getMetadata($item['id']);
if(empty($metadata)) { if(empty($metadata)) {
continue; continue;
} }
$item['uri'] = $metadata['uri']; $item['uri'] = $metadata['uri'];
$item['title'] = $metadata['title']; $item['title'] = $metadata['title'];
$item['timestamp'] = $metadata['timestamp']; $item['timestamp'] = $metadata['timestamp'];
$item['content'] = '<a href="' $item['content'] = '<a href="'
. $item['uri'] . $item['uri']
. '"><img src="' . '"><img src="'
. $metadata['thumbnailUri'] . $metadata['thumbnailUri']
. '" /></a><br><a href="' . '" /></a><br><a href="'
. $item['uri'] . $item['uri']
. '">' . '">'
. $item['title'] . $item['title']
. '</a>'; . '</a>';
$this->items[] = $item; $this->items[] = $item;
$count++;
if (count($this->items) >= 5) {
break;
}
} }
} }
} }
public function getName(){ public function getName() {
switch($this->queriedContext) { switch($this->queriedContext) {
case 'By username': case 'By username':
$specific = $this->getInput('u'); $specific = $this->getInput('u');
break; break;
case 'By playlist id': case 'By playlist id':
$specific = strtok($this->getInput('p'), '_'); $specific = strtok($this->getInput('p'), '_');
if ($this->feedName) {
$specific = $this->feedName;
}
break; break;
case 'From search results': case 'From search results':
$specific = $this->getInput('s'); $specific = $this->getInput('s');
@ -102,26 +127,77 @@ class DailymotionBridge extends BridgeAbstract {
default: return parent::getName(); default: return parent::getName();
} }
return $specific . ' : Dailymotion Bridge'; return $specific . ' : Dailymotion';
} }
public function getURI(){ public function getURI(){
$uri = self::URI; $uri = self::URI;
switch($this->queriedContext) { switch($this->queriedContext) {
case 'By username': case 'By username':
$uri .= 'user/' . urlencode($this->getInput('u')) . '/1'; $uri .= 'user/' . urlencode($this->getInput('u'));
break; break;
case 'By playlist id': case 'By playlist id':
$uri .= 'playlist/' . urlencode(strtok($this->getInput('p'), '_')); $uri .= 'playlist/' . urlencode(strtok($this->getInput('p'), '_'));
break; break;
case 'From search results': case 'From search results':
$uri .= 'search/' . urlencode($this->getInput('s')); $uri .= 'search/' . urlencode($this->getInput('s'));
if($this->getInput('pa')) {
$uri .= '/' . $this->getInput('pa'); if(!is_null($this->getInput('pa'))) {
$pa = $this->getInput('pa');
if ($this->getInput('pa') < 1) {
$pa = 1;
}
$uri .= '/' . $pa;
} }
break; break;
default: return parent::getURI(); default: return parent::getURI();
} }
return $uri; return $uri;
} }
private function getMetadata($id) {
$metadata = array();
$html = getSimpleHTMLDOM(self::URI . 'video/' . $id);
if(!$html) {
return $metadata;
}
$metadata['title'] = $html->find('meta[property=og:title]', 0)->getAttribute('content');
$metadata['timestamp'] = strtotime(
$html->find('meta[property=video:release_date]', 0)->getAttribute('content')
);
$metadata['thumbnailUri'] = $html->find('meta[property=og:image]', 0)->getAttribute('content');
$metadata['uri'] = $html->find('meta[property=og:url]', 0)->getAttribute('content');
return $metadata;
}
private function getPlaylistTitle($id) {
$title = '';
$url = self::URI . 'playlist/' . $id;
$html = getSimpleHTMLDOM($url)
or returnServerError('Could not request: ' . $url);
$title = $html->find('meta[property=og:title]', 0)->getAttribute('content');
return $title;
}
private function getApiUrl() {
switch($this->queriedContext) {
case 'By username':
return $this->apiUrl . '/user/' . $this->getInput('u')
. '/videos?fields=' . urlencode($this->apiFields) . '&availability=1&sort=recent&limit=5';
break;
case 'By playlist id':
return $this->apiUrl . '/playlist/' . $this->getInput('p')
. '/videos?fields=' . urlencode($this->apiFields) . '&limit=5';
break;
}
}
} }