SoundcloudBridge.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. class SoundCloudBridge extends BridgeAbstract{
  3. private $request;
  4. public $name;
  5. public function loadMetadatas() {
  6. $this->maintainer = "kranack";
  7. $this->name = "Soundcloud Bridge";
  8. $this->uri = "http://www.soundcloud.com/";
  9. $this->description = "Returns 10 newest music from user profile";
  10. $this->update = "2015-09-08";
  11. $this->parameters[] =
  12. '[
  13. {
  14. "name" : "username",
  15. "identifier" : "u"
  16. }
  17. ]';
  18. }
  19. const CLIENT_ID = '0aca19eae3843844e4053c6d8fdb7875';
  20. public function collectData(array $param){
  21. if (isset($param['u']) && !empty($param['u']))
  22. {
  23. $this->request = $param['u'];
  24. $res = json_decode(file_get_contents('https://api.soundcloud.com/resolve?url=http://www.soundcloud.com/'. urlencode($this->request) .'&client_id=' . self::CLIENT_ID)) or $this->returnError('No results for this query', 404);
  25. $tracks = json_decode(file_get_contents('https://api.soundcloud.com/users/'. urlencode($res->id) .'/tracks?client_id=' . self::CLIENT_ID)) or $this->returnError('No results for this user', 404);
  26. }
  27. else
  28. {
  29. $this->returnError('You must specify username', 400);
  30. }
  31. for ($i=0; $i < 10; $i++) {
  32. $item = new \Item();
  33. $item->name = $tracks[$i]->user->username .' - '. $tracks[$i]->title;
  34. $item->title = $tracks[$i]->user->username .' - '. $tracks[$i]->title;
  35. $item->content = '<audio src="'. $tracks[$i]->uri .'/stream?client_id='. self::CLIENT_ID .'">';
  36. $item->id = 'https://soundcloud.com/'. urlencode($this->request) .'/'. urlencode($tracks[$i]->permalink);
  37. $item->uri = 'https://soundcloud.com/'. urlencode($this->request) .'/'. urlencode($tracks[$i]->permalink);
  38. $this->items[] = $item;
  39. }
  40. }
  41. public function getName(){
  42. return (!empty($this->name) ? $this->name .' - ' : '') . (!empty($this->request) ? $this->request : '');
  43. }
  44. public function getURI(){
  45. return 'https://www.soundcloud.com/';
  46. }
  47. public function getCacheDuration(){
  48. return 600; // 10 minutes
  49. }
  50. }