Jamendocom.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace Muzich\CoreBundle\Factory\Elements;
  3. use Muzich\CoreBundle\Factory\ElementFactory;
  4. use Muzich\CoreBundle\Entity\Element;
  5. /**
  6. *
  7. *
  8. * @author bux
  9. */
  10. class Jamendocom extends ElementFactory
  11. {
  12. protected function proceedTypeAndId()
  13. {
  14. $url_clean = $this->getCleanedUrl();
  15. // album
  16. // http://www.jamendo.com/fr/album/3409
  17. $type = null;
  18. $ref_id = null;
  19. if (preg_match("#^\/[a-zA-Z0-9_-]+\/album\/([0-9]+)#", $url_clean, $chaines))
  20. {
  21. $type = 'album';
  22. $ref_id = $chaines[1];
  23. }
  24. // track
  25. // http://www.jamendo.com/fr/track/894974
  26. else if (preg_match("#^\/[a-zA-Z0-9_-]+\/track\/([0-9]+)#", $url_clean, $chaines))
  27. {
  28. $type = 'track';
  29. $ref_id = $chaines[1];
  30. }
  31. // album new ver
  32. // http://www.jamendo.com/fr/list/a45666/proceed-positron...
  33. else if (preg_match("#^\/[a-zA-Z0-9_-]+\/list\/a([0-9]+)\/.#", $url_clean, $chaines))
  34. {
  35. $type = 'album';
  36. $ref_id = $chaines[1];
  37. }
  38. // track new ver
  39. // http://www.jamendo.com/fr/track/347602/come-come
  40. else if (preg_match("#^\/[a-zA-Z0-9_-]+\/track\/([0-9]+)\/.#", $url_clean, $chaines))
  41. {
  42. $type = 'track';
  43. $ref_id = $chaines[1];
  44. }
  45. $this->element->setData(Element::DATA_TYPE , $type);
  46. $this->element->setData(Element::DATA_REF_ID, $ref_id);
  47. }
  48. public function getStreamData()
  49. {
  50. // On determine le type et l'url
  51. $this->proceedTypeAndId();
  52. $type = $this->element->getData(Element::DATA_TYPE);
  53. $ref_id = $this->element->getData(Element::DATA_REF_ID);
  54. // Récupération de données avec l'API
  55. $api_url = null;
  56. switch ($type)
  57. {
  58. case 'track':
  59. $api_url = "http://api.jamendo.com/get2/name+stream/track/json/?track_id=".$ref_id;
  60. break;
  61. case 'album':
  62. $api_url = "http://api.jamendo.com/get2/name+stream/track/json/?album_id=".$ref_id;
  63. break;
  64. }
  65. if ($api_url)
  66. {
  67. $ch = curl_init($api_url);
  68. $options = array(
  69. CURLOPT_RETURNTRANSFER => true,
  70. CURLOPT_HTTPHEADER => array('Content-type: text/plain')
  71. );
  72. curl_setopt_array( $ch, $options );
  73. $result = json_decode(curl_exec($ch), true);
  74. if (count($result))
  75. {
  76. $data_return = array();
  77. foreach ($result as $song)
  78. {
  79. $data_return[] = array(
  80. 'name' => $song['name'],
  81. 'url' => $song['stream'],
  82. );
  83. }
  84. return $data_return;
  85. }
  86. }
  87. }
  88. /**
  89. * ALBUM = http://www.jamendo.com/fr/album/30661
  90. * TRACK = http://www.jamendo.com/fr/track/207079
  91. *
  92. * API: http://developer.jamendo.com/fr/wiki/Musiclist2ApiFields
  93. */
  94. public function retrieveDatas()
  95. {
  96. // On determine le type et l'url
  97. $this->proceedTypeAndId();
  98. $type = $this->element->getData(Element::DATA_TYPE);
  99. $ref_id = $this->element->getData(Element::DATA_REF_ID);
  100. // Récupération de données avec l'API
  101. $api_url = null;
  102. switch ($type)
  103. {
  104. case 'album':
  105. $api_url = "http://api.jamendo.com/get2/"
  106. ."id+name+url+image+artist_name+artist_url/album/json/?album_id=".$ref_id;
  107. $api_tag_url = "http://api.jamendo.com/get2/name+weight/tag/json/album_tag/?album_id=".$ref_id;
  108. break;
  109. case 'track':
  110. $api_url = "http://api.jamendo.com/get2/"
  111. ."id+name+url+image+artist_name+artist_url+track_name/album/json/?track_id=".$ref_id;
  112. $api_tag_url = "http://api.jamendo.com/get2/name+weight/tag/json/track_tag/?track_id=".$ref_id;
  113. break;
  114. }
  115. if ($api_url)
  116. {
  117. $ch = curl_init($api_url);
  118. $options = array(
  119. CURLOPT_RETURNTRANSFER => true,
  120. CURLOPT_HTTPHEADER => array('Content-type: text/plain')
  121. );
  122. curl_setopt_array( $ch, $options );
  123. $result = json_decode(curl_exec($ch), true);
  124. if (count($result))
  125. {
  126. // Thumb
  127. if (array_key_exists('image', $result[0]))
  128. {
  129. $this->element->setData(Element::DATA_THUMB_URL, $result[0]['image']);
  130. }
  131. // Album name
  132. if (array_key_exists('name', $result[0]) && $type == 'album')
  133. {
  134. $this->element->setData(Element::DATA_TITLE, $result[0]['name']);
  135. }
  136. // Artist name
  137. if (array_key_exists('artist_name', $result[0]))
  138. {
  139. $this->element->setData(Element::DATA_ARTIST, $result[0]['artist_name']);
  140. }
  141. // track name
  142. if (array_key_exists('track_name', $result[0]) && $type == 'track')
  143. {
  144. $this->element->setData(Element::DATA_TITLE, $result[0]['track_name']);
  145. }
  146. // Maintenant au tour des tags (deuxième requete a l'api)
  147. $ch = curl_init($api_tag_url);
  148. $options = array(
  149. CURLOPT_RETURNTRANSFER => true,
  150. CURLOPT_HTTPHEADER => array('Content-type: text/plain')
  151. );
  152. curl_setopt_array( $ch, $options );
  153. $result = json_decode(curl_exec($ch), true);
  154. if (count($result))
  155. {
  156. $tags = array();
  157. foreach ($result as $tag)
  158. {
  159. $tags[] = $tag['name'];
  160. }
  161. $this->element->setData(Element::DATA_TAGS, $tags);
  162. }
  163. }
  164. }
  165. // Un contenu jamendo est toujours téléchargeable
  166. $this->element->setData(Element::DATA_DOWNLOAD, true);
  167. }
  168. public function proceedEmbedCode()
  169. {
  170. if (($ref_id = $this->element->getData(Element::DATA_REF_ID))
  171. && ($type = $this->element->getData(Element::DATA_TYPE)))
  172. {
  173. $height = $this->container->getParameter('jamendo_player_height');
  174. $width = $this->container->getParameter('jamendo_player_width');
  175. $embed_url = "http://widgets.jamendo.com/fr/$type/?".$type."_id=$ref_id&playertype=2008";
  176. $this->element->setEmbed(
  177. '<object width="'.$width.'" height="'.$height.'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"'
  178. .' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" align="middle">
  179. <param name="allowScriptAccess" value="always" />
  180. <param name="wmode" value="transparent" />
  181. <param name="movie" value="'.$embed_url.'" />
  182. <param name="quality" value="high" />
  183. <param name="bgcolor" value="#FFFFFF" />
  184. <embed src="'.$embed_url.'" quality="high" wmode="transparent" bgcolor="#FFFFFF"'
  185. .' width="'.$width.'" height="'.$height.'" align="middle" allowScriptAccess="always"'
  186. .' type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
  187. &nbsp;
  188. </embed>
  189. &nbsp;
  190. </object>'
  191. );
  192. }
  193. }
  194. public function proceedThumbnailUrl()
  195. {
  196. if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))
  197. {
  198. $this->element->setThumbnailUrl($thumb);
  199. }
  200. }
  201. }