2014-11-18 22:02:04 +03:00
< ? php
class DailymotionBridge extends BridgeAbstract {
2015-11-05 18:50:18 +03:00
2014-11-18 22:02:04 +03:00
private $request ;
2015-11-05 18:50:18 +03:00
public function loadMetadatas () {
$this -> maintainer = " mitsukarenai " ;
$this -> name = " Dailymotion Bridge " ;
$this -> uri = " https://www.dailymotion.com/ " ;
$this -> description = " Returns the 5 newest videos by username/playlist or search " ;
2016-08-22 02:25:56 +03:00
$this -> parameters [ " By username " ] = array (
'u' => array ( 'name' => 'username' )
);
2015-11-05 18:50:18 +03:00
2016-08-22 02:25:56 +03:00
$this -> parameters [ " By playlist id " ] = array (
'p' => array (
'name' => 'playlist id' ,
2016-08-25 01:19:41 +03:00
'type' => 'text' ,
'required' => true
)
2016-08-22 02:25:56 +03:00
);
2015-11-05 18:50:18 +03:00
2016-08-22 02:25:56 +03:00
$this -> parameters [ " From search results " ] = array (
2016-08-25 01:19:41 +03:00
's' => array (
'name' => 'Search keyword' ,
'required' => true
),
2016-08-22 02:25:56 +03:00
'pa' => array (
'name' => 'Page' ,
'type' => 'number'
)
);
2015-11-05 18:50:18 +03:00
}
2016-08-21 12:46:23 +03:00
function getMetadata ( $id ) {
$metadata = array ();
$html2 = $this -> getSimpleHTMLDOM ( 'http://www.dailymotion.com/video/' . $id ) or $this -> returnServerError ( 'Could not request Dailymotion.' );
$metadata [ 'title' ] = $html2 -> find ( 'meta[property=og:title]' , 0 ) -> getAttribute ( 'content' );
$metadata [ 'timestamp' ] = strtotime ( $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' );
2015-11-05 18:50:18 +03:00
2016-08-21 12:46:23 +03:00
return $metadata ;
}
2014-11-18 22:02:04 +03:00
2016-08-21 12:46:23 +03:00
public function collectData ( array $param ){
2014-11-18 22:02:04 +03:00
$html = '' ;
$limit = 5 ;
$count = 0 ;
if ( isset ( $param [ 'u' ])) { // user timeline mode
$this -> request = $param [ 'u' ];
2016-07-08 20:06:35 +03:00
$html = $this -> getSimpleHTMLDOM ( 'http://www.dailymotion.com/user/' . urlencode ( $this -> request ) . '/1' ) or $this -> returnServerError ( 'Could not request Dailymotion.' );
2014-11-18 22:02:04 +03:00
}
else if ( isset ( $param [ 'p' ])) { // playlist mode
$this -> request = strtok ( $param [ 'p' ], '_' );
2016-07-08 20:06:35 +03:00
$html = $this -> getSimpleHTMLDOM ( 'http://www.dailymotion.com/playlist/' . urlencode ( $this -> request ) . '' ) or $this -> returnServerError ( 'Could not request Dailymotion.' );
2014-11-18 22:02:04 +03:00
}
else if ( isset ( $param [ 's' ])) { // search mode
2016-07-08 20:06:35 +03:00
$this -> request = $param [ 's' ]; $page = 1 ; if ( isset ( $param [ 'pa' ])) $page = ( int ) preg_replace ( " /[^0-9]/ " , '' , $param [ 'pa' ]);
$html = $this -> getSimpleHTMLDOM ( 'http://www.dailymotion.com/search/' . urlencode ( $this -> request ) . '/' . $page . '' ) or $this -> returnServerError ( 'Could not request Dailymotion.' );
2014-11-18 22:02:04 +03:00
}
else {
2016-08-17 15:45:08 +03:00
$this -> returnClientError ( 'You must either specify a Dailymotion username (?u=...) or a playlist id (?p=...) or search (?s=...)' );
2014-11-18 22:02:04 +03:00
}
foreach ( $html -> find ( 'div.media a.preview_link' ) as $element ) {
if ( $count < $limit ) {
2016-08-22 19:55:59 +03:00
$item = array ();
$item [ 'id' ] = str_replace ( '/video/' , '' , strtok ( $element -> href , '_' ));
$metadata = $this -> getMetadata ( $item [ 'id' ]);
$item [ 'uri' ] = $metadata [ 'uri' ];
$item [ 'title' ] = $metadata [ 'title' ];
$item [ 'timestamp' ] = $metadata [ 'timestamp' ];
$item [ 'content' ] = '<a href="' . $item [ 'uri' ] . '"><img src="' . $metadata [ 'thumbnailUri' ] . '" /></a><br><a href="' . $item [ 'uri' ] . '">' . $item [ 'title' ] . '</a>' ;
2014-11-18 22:02:04 +03:00
$this -> items [] = $item ;
$count ++ ;
}
}
}
public function getName (){
return ( ! empty ( $this -> request ) ? $this -> request . ' - ' : '' ) . 'Dailymotion Bridge' ;
}
public function getCacheDuration (){
return 3600 * 3 ; // 3 hours
}
}