2014-07-24 15:51:42 +04:00
< ? php
class SoundCloudBridge extends BridgeAbstract {
2015-09-09 01:13:56 +03:00
2014-07-24 15:51:42 +04:00
private $request ;
2015-11-04 02:05:10 +03:00
public $name ;
2015-11-05 18:50:18 +03:00
public function loadMetadatas () {
$this -> maintainer = " kranack " ;
$this -> name = " Soundcloud Bridge " ;
$this -> uri = " http://www.soundcloud.com/ " ;
$this -> description = " Returns 10 newest music from user profile " ;
2016-08-22 02:25:56 +03:00
$this -> parameters [] = array (
'u' => array (
'name' => 'username' ,
'required' => true
)
);
2015-11-05 18:50:18 +03:00
}
const CLIENT_ID = '0aca19eae3843844e4053c6d8fdb7875' ;
2015-09-09 01:13:56 +03:00
2016-08-25 02:24:53 +03:00
public function collectData (){
$param = $this -> parameters [ $this -> queriedContext ];
2015-09-09 01:13:56 +03:00
2016-08-25 02:24:53 +03:00
if ( isset ( $param [ 'u' ][ 'value' ]) && ! empty ( $param [ 'u' ][ 'value' ]))
2014-07-24 15:51:42 +04:00
{
2016-08-25 02:24:53 +03:00
$this -> request = $param [ 'u' ][ 'value' ];
2015-09-09 01:13:56 +03:00
2016-08-23 00:39:40 +03:00
$res = json_decode ( $this -> getContents ( 'https://api.soundcloud.com/resolve?url=http://www.soundcloud.com/' . urlencode ( $this -> request ) . '&client_id=' . self :: CLIENT_ID )) or $this -> returnServerError ( 'No results for this query' );
$tracks = json_decode ( $this -> getContents ( 'https://api.soundcloud.com/users/' . urlencode ( $res -> id ) . '/tracks?client_id=' . self :: CLIENT_ID )) or $this -> returnServerError ( 'No results for this user' );
2015-09-09 01:13:56 +03:00
}
2014-07-24 15:51:42 +04:00
else
{
2016-08-17 15:45:08 +03:00
$this -> returnClientError ( 'You must specify username' );
2014-07-24 15:51:42 +04:00
}
for ( $i = 0 ; $i < 10 ; $i ++ ) {
2016-08-22 19:55:59 +03:00
$item = array ();
$item [ 'author' ] = $tracks [ $i ] -> user -> username . ' - ' . $tracks [ $i ] -> title ;
$item [ 'title' ] = $tracks [ $i ] -> user -> username . ' - ' . $tracks [ $i ] -> title ;
$item [ 'content' ] = '<audio src="' . $tracks [ $i ] -> uri . '/stream?client_id=' . self :: CLIENT_ID . '">' ;
$item [ 'id' ] = 'https://soundcloud.com/' . urlencode ( $this -> request ) . '/' . urlencode ( $tracks [ $i ] -> permalink );
$item [ 'uri' ] = 'https://soundcloud.com/' . urlencode ( $this -> request ) . '/' . urlencode ( $tracks [ $i ] -> permalink );
2014-07-24 15:51:42 +04:00
$this -> items [] = $item ;
}
2015-09-09 01:13:56 +03:00
2014-07-24 15:51:42 +04:00
}
public function getName (){
2016-02-16 14:33:35 +03:00
return ( ! empty ( $this -> name ) ? $this -> name . ' - ' : '' ) . ( ! empty ( $this -> request ) ? $this -> request : '' );
2014-07-24 15:51:42 +04:00
}
public function getCacheDuration (){
2015-09-10 01:00:29 +03:00
return 600 ; // 10 minutes
2014-07-24 15:51:42 +04:00
}
}