Soundcloud.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. require_once 'soundcloud-api/Services/Soundcloud.php';
  3. class Application_Model_Soundcloud
  4. {
  5. private $_soundcloud;
  6. public function __construct()
  7. {
  8. $CC_CONFIG = Config::getConfig();
  9. $this->_soundcloud = new Services_Soundcloud(
  10. $CC_CONFIG['soundcloud-client-id'],
  11. $CC_CONFIG['soundcloud-client-secret']);
  12. }
  13. private function getToken()
  14. {
  15. $username = Application_Model_Preference::GetSoundCloudUser();
  16. $password = Application_Model_Preference::GetSoundCloudPassword();
  17. $token = $this->_soundcloud->accessTokenResourceOwner($username, $password);
  18. return $token;
  19. }
  20. public function uploadTrack($filepath, $filename, $description,
  21. $tags=array(), $release=null, $genre=null)
  22. {
  23. if (!$this->getToken()) {
  24. throw new NoSoundCloundToken();
  25. }
  26. if (count($tags)) {
  27. $tags = join(" ", $tags);
  28. $tags = $tags." ".Application_Model_Preference::GetSoundCloudTags();
  29. } else {
  30. $tags = Application_Model_Preference::GetSoundCloudTags();
  31. }
  32. $downloadable = Application_Model_Preference::GetSoundCloudDownloadbleOption() == '1';
  33. $track_data = array(
  34. 'track[sharing]' => 'private',
  35. 'track[title]' => $filename,
  36. 'track[asset_data]' => '@' . $filepath,
  37. 'track[tag_list]' => $tags,
  38. 'track[description]' => $description,
  39. 'track[downloadable]' => $downloadable,
  40. );
  41. if (isset($release)) {
  42. $release = str_replace(" ", "-", $release);
  43. $release = str_replace(":", "-", $release);
  44. //YYYY-MM-DD-HH-mm-SS
  45. $release = explode("-", $release);
  46. $track_data['track[release_year]'] = $release[0];
  47. $track_data['track[release_month]'] = $release[1];
  48. $track_data['track[release_day]'] = $release[2];
  49. }
  50. if (isset($genre) && $genre != "") {
  51. $track_data['track[genre]'] = $genre;
  52. } else {
  53. $default_genre = Application_Model_Preference::GetSoundCloudGenre();
  54. if ($default_genre != "") {
  55. $track_data['track[genre]'] = $default_genre;
  56. }
  57. }
  58. $track_type = Application_Model_Preference::GetSoundCloudTrackType();
  59. if ($track_type != "") {
  60. $track_data['track[track_type]'] = $track_type;
  61. }
  62. $license = Application_Model_Preference::GetSoundCloudLicense();
  63. if ($license != "") {
  64. $track_data['track[license]'] = $license;
  65. }
  66. $response = json_decode(
  67. $this->_soundcloud->post('tracks', $track_data),
  68. true
  69. );
  70. return $response;
  71. }
  72. public static function uploadSoundcloud($id)
  73. {
  74. $cmd = "/usr/lib/airtime/utils/soundcloud-uploader $id > /dev/null &";
  75. Logging::info("Uploading soundcloud with command: $cmd");
  76. exec($cmd);
  77. }
  78. }
  79. class NoSoundCloundToken extends Exception {}