Soundcloudcom.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace Muzich\CoreBundle\Factory\Elements;
  3. use Muzich\CoreBundle\Factory\ElementFactory;
  4. use Muzich\CoreBundle\Entity\Element;
  5. use Muzich\CoreBundle\Util\TagLike;
  6. /**
  7. *
  8. *
  9. * @author bux
  10. */
  11. class Soundcloudcom extends ElementFactory
  12. {
  13. /**
  14. * ??SET = http://soundcloud.com/matas/sets/library-project
  15. * ?? = http://soundcloud.com/matas/anadrakonic-waltz
  16. */
  17. public function retrieveDatas()
  18. {
  19. $url_clean = $this->getCleanedUrl();
  20. $match = false;
  21. //
  22. if (preg_match("#^\/[a-zA-Z0-9_-]+\/sets\/[a-zA-Z0-9_-]+#", $url_clean, $chaines))
  23. {
  24. $match = true;
  25. }
  26. // /noisia/black-sun-empire-noisia-feed
  27. // /user4818423/mechanika-crew-andrew-dj-set
  28. else if (preg_match("#^\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+#", $url_clean, $chaines))
  29. {
  30. $match = true;
  31. }
  32. // On en gère pas encore les recherches
  33. if (preg_match("#\/search\?q#", $url_clean, $chaines))
  34. {
  35. $match = false;
  36. }
  37. //$this->element->setData(Element::DATA_REF_ID, $this->element->getUrl());
  38. // récupération de données avec l'API
  39. if ($match)
  40. {
  41. // La première étape consiste a résoudre l'url
  42. $ch = curl_init('http://api.soundcloud.com/resolve.json?url='.$this->element->getUrl().'&client_id=39946ea18e3d78d64c0ac95a025794e1');
  43. $options = array(
  44. CURLOPT_RETURNTRANSFER => true,
  45. CURLOPT_HTTPHEADER => array('Content-type: application/json')
  46. );
  47. curl_setopt_array( $ch, $options );
  48. $result = json_decode(curl_exec($ch));
  49. if (isset($result->errors))
  50. {
  51. if (count($result->errors))
  52. {
  53. return;
  54. }
  55. }
  56. if (!isset($result->location))
  57. {
  58. return;
  59. }
  60. if (!$result->location)
  61. {
  62. return;
  63. }
  64. $getjsonurl = $result->location;
  65. // On a maintenant la bonne url pour demander les infos
  66. $ch = curl_init($getjsonurl);
  67. curl_setopt_array($ch, $options);
  68. $result = json_decode(curl_exec($ch), true);
  69. /*
  70. * array
  71. 'kind' => string 'track' (length=5)
  72. 'id' => int 57452080
  73. 'created_at' => string '2012/08/24 20:39:44 +0000' (length=25)
  74. 'user_id' => int 11235441
  75. 'duration' => int 4206558
  76. 'commentable' => boolean true
  77. 'state' => string 'finished' (length=8)
  78. 'original_content_size' => int 168196212
  79. 'sharing' => string 'public' (length=6)
  80. 'tag_list' => string '' (length=0)
  81. 'permalink' => string 'mechanika-crew-andrew-dj-set' (length=28)
  82. 'streamable' => boolean true
  83. 'embeddable_by' => string 'all' (length=3)
  84. 'downloadable' => boolean true
  85. 'purchase_url' => null
  86. 'label_id' => null
  87. 'purchase_title' => null
  88. 'genre' => string '' (length=0)
  89. 'title' => string 'MECHANIKA CREW / ANDREW dj set 24.08.12' (length=39)
  90. 'description' => string '' (length=0)
  91. 'label_name' => string '' (length=0)
  92. 'release' => string '' (length=0)
  93. 'track_type' => string '' (length=0)
  94. 'key_signature' => string '' (length=0)
  95. 'isrc' => string '' (length=0)
  96. 'video_url' => null
  97. 'bpm' => null
  98. 'release_year' => null
  99. 'release_month' => null
  100. 'release_day' => null
  101. 'original_format' => string 'mp3' (length=3)
  102. 'license' => string 'all-rights-reserved' (length=19)
  103. 'uri' => string 'http://api.soundcloud.com/tracks/57452080' (length=41)
  104. 'user' =>
  105. array
  106. 'id' => int 11235441
  107. 'kind' => string 'user' (length=4)
  108. 'permalink' => string 'user4818423' (length=11)
  109. 'username' => string 'Andrea Andrew mechanika' (length=23)
  110. 'uri' => string 'http://api.soundcloud.com/users/11235441' (length=40)
  111. 'permalink_url' => string 'http://soundcloud.com/user4818423' (length=33)
  112. 'avatar_url' => string 'http://i1.sndcdn.com/avatars-000023343399-cp1lvg-large.jpg?04ad178' (length=66)
  113. 'permalink_url' => string 'http://soundcloud.com/user4818423/mechanika-crew-andrew-dj-set' (length=62)
  114. 'artwork_url' => string 'http://i1.sndcdn.com/artworks-000029057120-6fz4k4-large.jpg?04ad178' (length=67)
  115. 'waveform_url' => string 'http://w1.sndcdn.com/udItSnzA5J22_m.png' (length=39)
  116. 'stream_url' => string 'http://api.soundcloud.com/tracks/57452080/stream' (length=48)
  117. 'download_url' => string 'http://api.soundcloud.com/tracks/57452080/download' (length=50)
  118. 'playback_count' => int 502
  119. 'download_count' => int 85
  120. 'favoritings_count' => int 12
  121. 'comment_count' => int 13
  122. 'attachments_uri' => string 'http://api.soundcloud.com/tracks/57452080/attachments' (length=53)
  123. */
  124. // En premier lieux il nous faut être sur d'avoir le droit d'utiliser le lecteur exportable
  125. $sharing = false;
  126. if (array_key_exists('sharing', $result) && array_key_exists('embeddable_by', $result))
  127. {
  128. if ($result['sharing'] == 'public' && ($result['embeddable_by'] == 'all' || $result['embeddable_by'] == 'me'))
  129. {
  130. $sharing = true;
  131. }
  132. }
  133. if ($sharing)
  134. {
  135. if (array_key_exists('id', $result) )
  136. {
  137. $this->element->setData(Element::DATA_REF_ID, $result['id']);
  138. }
  139. if (array_key_exists('uri', $result) )
  140. {
  141. $this->element->setData(Element::DATA_NORMALIZED_URL, $result['uri']);
  142. }
  143. if (array_key_exists('artwork_url', $result) )
  144. {
  145. $this->element->setData(Element::DATA_THUMB_URL, $result['artwork_url']);
  146. }
  147. if (array_key_exists('kind', $result) )
  148. {
  149. $this->element->setData(Element::DATA_TYPE, $result['kind']);
  150. }
  151. if (array_key_exists('downloadable', $result) )
  152. {
  153. $this->element->setData(Element::DATA_DOWNLOAD, $result['downloadable']);
  154. // FIXME
  155. $this->element->setData(Element::DATA_DOWNLOAD_URL, $this->element->getUrl().'/download');
  156. }
  157. if (array_key_exists('title', $result) )
  158. {
  159. $this->element->setData(Element::DATA_TITLE, $result['title']);
  160. }
  161. if (array_key_exists('user', $result) )
  162. {
  163. $this->element->setData(Element::DATA_ARTIST, $result['user']['username']);
  164. }
  165. $genres = '';
  166. if (array_key_exists('genre', $result) )
  167. {
  168. if (strlen($result['genre']))
  169. {
  170. $genres = $result['genre'];
  171. }
  172. }
  173. $tags_list = '';
  174. if (array_key_exists('tag_list', $result) )
  175. {
  176. if (strlen($result['tag_list']))
  177. {
  178. $tags_list = $result['tag_list'];
  179. }
  180. }
  181. $tags_string = $genres.' '.$tags_list.' '.str_replace(' ', '-', $genres);
  182. $tags_like = array();
  183. if (strlen($tags_string))
  184. {
  185. $tag_like = new TagLike($this->entity_manager);
  186. foreach (explode(' ', $tags_string) as $word)
  187. {
  188. $similar_tags = $tag_like->getSimilarTags($word, $this->element->getOwner()->getId());
  189. if (count($similar_tags))
  190. {
  191. if ($similar_tags['same_found'])
  192. {
  193. $tags_like[] = $similar_tags['tags'][0]['name'];
  194. }
  195. }
  196. }
  197. $tags_like[] = $genres;
  198. if (count($tags_like))
  199. {
  200. $this->element->setData(Element::DATA_TAGS, array_unique($tags_like));
  201. }
  202. }
  203. }
  204. }
  205. }
  206. public function proceedEmbedCode()
  207. {
  208. if (($ref_id = $this->element->getData(Element::DATA_REF_ID))
  209. && ($this->element->getData(Element::DATA_TYPE) == 'track' || $this->element->getData(Element::DATA_TYPE) == 'playlist' ))
  210. {
  211. $ref_id = $this->element->getUrl();
  212. $embed_id = md5($ref_id);
  213. $height = $this->container->getParameter('soundcloud_player_height');
  214. $this->element->setEmbed(
  215. '<object height="'.$height.'" width="100%" id="embed_'.$embed_id.'" '
  216. .'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
  217. <param name="movie" value="http://player.soundcloud.com/player.swf?url='.$ref_id.'&amp;'
  218. .'enable_api=true&amp;object_id=embed_'.$embed_id.'"></param>
  219. <param name="allowscriptaccess" value="always"></param>
  220. <embed allowscriptaccess="always" height="'.$height.'" '
  221. .'src="http://player.soundcloud.com/player.swf?url='.$ref_id.'&amp;enable_api=true'
  222. .'&amp;object_id=embed_'.$embed_id.'" type="application/x-shockwave-flash" '
  223. .'width="100%" name="embed_'.$embed_id.'"></embed>
  224. </object>'
  225. );
  226. // $this->element->setEmbed(
  227. // '<iframe id="sc-widget_'.$this->element->getData(Element::DATA_REF_ID).
  228. // '" src="http://w.soundcloud.com/player/?url='.
  229. // $ref_id.'" width="100%" '.
  230. // 'height="'.$height.'" scrolling="no" frameborder="no"></iframe>'
  231. // );
  232. }
  233. }
  234. public function proceedThumbnailUrl()
  235. {
  236. if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))
  237. {
  238. $this->element->setThumbnailUrl($thumb);
  239. }
  240. }
  241. }