2014-07-24 13:51:42 +02:00
< ? php
class SoundCloudBridge extends BridgeAbstract {
2015-09-09 00:13:56 +02:00
2014-07-24 13:51:42 +02:00
private $request ;
2015-11-04 00:05:10 +01:00
public $name ;
2015-11-05 16:50:18 +01: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 01:25:56 +02:00
$this -> parameters [] = array (
'u' => array (
'name' => 'username' ,
'required' => true
)
);
2015-11-05 16:50:18 +01:00
}
const CLIENT_ID = '0aca19eae3843844e4053c6d8fdb7875' ;
2015-09-09 00:13:56 +02:00
2016-08-25 01:24:53 +02:00
public function collectData (){
$param = $this -> parameters [ $this -> queriedContext ];
2015-09-09 00:13:56 +02:00
2016-08-25 01:24:53 +02:00
if ( isset ( $param [ 'u' ][ 'value' ]) && ! empty ( $param [ 'u' ][ 'value' ]))
2014-07-24 13:51:42 +02:00
{
2016-08-25 01:24:53 +02:00
$this -> request = $param [ 'u' ][ 'value' ];
2015-09-09 00:13:56 +02:00
2016-08-22 23:39:40 +02: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 00:13:56 +02:00
}
2014-07-24 13:51:42 +02:00
else
{
2016-08-17 14:45:08 +02:00
$this -> returnClientError ( 'You must specify username' );
2014-07-24 13:51:42 +02:00
}
for ( $i = 0 ; $i < 10 ; $i ++ ) {
2016-08-22 18:55:59 +02: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 13:51:42 +02:00
$this -> items [] = $item ;
}
2015-09-09 00:13:56 +02:00
2014-07-24 13:51:42 +02:00
}
public function getName (){
2016-02-16 12:33:35 +01:00
return ( ! empty ( $this -> name ) ? $this -> name . ' - ' : '' ) . ( ! empty ( $this -> request ) ? $this -> request : '' );
2014-07-24 13:51:42 +02:00
}
public function getCacheDuration (){
2015-09-10 00:00:29 +02:00
return 600 ; // 10 minutes
2014-07-24 13:51:42 +02:00
}
}