Jamendocom.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Muzich\CoreBundle\Factory\Elements;
  3. use Muzich\CoreBundle\Factory\ElementFactory;
  4. use Muzich\CoreBundle\Entity\Element;
  5. use Muzich\CoreBundle\Factory\UrlMatchs;
  6. class Jamendocom extends ElementFactory
  7. {
  8. public function __construct(Element $element, Container $container, EntityManager $entity_manager)
  9. {
  10. parent::__construct($element, $container, $entity_manager);
  11. $this->url_matchs = UrlMatchs::$jamendo;
  12. }
  13. public function getStreamData()
  14. {
  15. $ref_id = $this->element->getData(Element::DATA_REF_ID);
  16. $api_url = "http://api.jamendo.com/get2/name+stream/track/json/?". $this->element->getData(Element::DATA_TYPE)."_id=".$ref_id;
  17. if ($ref_id)
  18. {
  19. $response = $this->getApiConnector()->getResponseForUrl($api_url);
  20. if (count($response->getContent()))
  21. {
  22. $data_return = array();
  23. foreach ($result as $song)
  24. {
  25. $data_return[] = array(
  26. 'name' => $song['name'],
  27. 'url' => $song['stream'],
  28. );
  29. }
  30. return $data_return;
  31. }
  32. }
  33. }
  34. public function proceedDatas()
  35. {
  36. $this->retrieveDatas();
  37. // TODO: A t-on toujours besoin du code embed ou on génrère le player générique a la volée ?
  38. $this->proceedEmbedCode();
  39. $this->proceedThumbnailUrl();
  40. }
  41. protected function getApiUrl()
  42. {
  43. switch ($this->url_analyzer->getType())
  44. {
  45. case Element::TYPE_ALBUM:
  46. return "http://api.jamendo.com/get2/id+name+url+image+artist_name+artist_url/album/json/?album_id="
  47. .$this->url_analyzer->getRefId();
  48. break;
  49. case Element::TYPE_TRACK:
  50. return "http://api.jamendo.com/get2/id+name+url+image+artist_name+artist_url+track_name/album/json/?track_id="
  51. .$this->url_analyzer->getRefId();
  52. break;
  53. }
  54. }
  55. protected function getApiTagUrl()
  56. {
  57. return "http://api.jamendo.com/get2/name+weight/tag/json/album_tag/?".$this->url_analyzer->getType()."_id="
  58. .$this->url_analyzer->getRefId();
  59. }
  60. public function retrieveDatas()
  61. {
  62. if (($response = $this->getApiConnector()->getResponseForUrl($this->getApiUrl())))
  63. {
  64. // Check si tout se passe bien si pas de retour de l'api
  65. $this->getApiConnector()->setElementDatasWithResponse($response, array(
  66. Element::DATA_THUMB_URL => array(0 => 'image'),
  67. Element::DATA_ARTIST => array(0 => 'artist_name'),
  68. ));
  69. if ($this->url_analyzer->getType() == Element::TYPE_ALBUM)
  70. {
  71. $this->element->setData(Element::DATA_TITLE, $response->get(array(0 => 'name')));
  72. }
  73. if ($this->url_analyzer->getType() == Element::TYPE_TRACK)
  74. {
  75. $this->element->setData(Element::DATA_TITLE, $response->get(array(0 => 'track_name')));
  76. }
  77. if (($response = $this->getApiConnector()->getResponseForUrl($this->getApiTagUrl())))
  78. {
  79. // TODO: Check si tout ce passe bien avec pas de tags en retour de l'api
  80. $tags = array();
  81. foreach ($result->getContent() as $tag)
  82. {
  83. $tags[] = $tag['name'];
  84. }
  85. $this->element->setData(Element::DATA_TAGS, $tags);
  86. }
  87. }
  88. // Un contenu jamendo est toujours téléchargeable
  89. $this->element->setData(Element::DATA_DOWNLOAD, true);
  90. }
  91. public function proceedEmbedCode()
  92. {
  93. if (($ref_id = $this->element->getData(Element::DATA_REF_ID))
  94. && ($type = $this->element->getData(Element::DATA_TYPE)))
  95. {
  96. $height = $this->container->getParameter('jamendo_player_height');
  97. $width = $this->container->getParameter('jamendo_player_width');
  98. $embed_url = "http://widgets.jamendo.com/fr/$type/?".$type."_id=$ref_id&playertype=2008";
  99. $this->element->setEmbed(
  100. '<object width="'.$width.'" height="'.$height.'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"'
  101. .' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" align="middle">
  102. <param name="allowScriptAccess" value="always" />
  103. <param name="wmode" value="transparent" />
  104. <param name="movie" value="'.$embed_url.'" />
  105. <param name="quality" value="high" />
  106. <param name="bgcolor" value="#FFFFFF" />
  107. <embed src="'.$embed_url.'" quality="high" wmode="transparent" bgcolor="#FFFFFF"'
  108. .' width="'.$width.'" height="'.$height.'" align="middle" allowScriptAccess="always"'
  109. .' type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
  110. &nbsp;
  111. </embed>
  112. &nbsp;
  113. </object>'
  114. );
  115. }
  116. }
  117. public function proceedThumbnailUrl()
  118. {
  119. if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))
  120. {
  121. $this->element->setThumbnailUrl($thumb);
  122. }
  123. }
  124. }