Deezercom.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Muzich\CoreBundle\Factory\Elements;
  3. use Muzich\CoreBundle\Factory\ElementFactory;
  4. use Muzich\CoreBundle\Entity\Element;
  5. use Symfony\Component\DependencyInjection\Container;
  6. use Doctrine\ORM\EntityManager;
  7. use Muzich\CoreBundle\Factory\UrlMatchs;
  8. /**
  9. *
  10. *
  11. * @author bux
  12. */
  13. class Deezercom extends ElementFactory
  14. {
  15. public function __construct(Element $element, Container $container, EntityManager $entity_manager)
  16. {
  17. $this->url_matchs = UrlMatchs::$deezer;
  18. parent::__construct($element, $container, $entity_manager);
  19. }
  20. public function proceedDatas()
  21. {
  22. $this->setElementDatasWithApi();
  23. $this->proceedEmbedCode();
  24. $this->proceedThumbnailUrl();
  25. }
  26. protected function setElementDatasWithApi()
  27. {
  28. $response = $this->getApiConnector()->getResponseForUrl('http://api.deezer.com/2.0/'.$this->url_analyzer->getType().'/'.$this->url_analyzer->getRefId());
  29. $this->getApiConnector()->setElementDatasWithResponse($response, array(
  30. Element::DATA_THUMB_URL => 'cover',
  31. Element::DATA_TITLE => 'title',
  32. Element::DATA_ARTIST => array('artist' => 'name')
  33. ));
  34. if ($this->url_analyzer->getType() == Element::TYPE_TRACK && !$this->element->getData(Element::DATA_THUMB_URL))
  35. {
  36. $this->getApiConnector()->setElementDatasWithResponse($response, array(
  37. Element::DATA_THUMB_URL => array('album' => 'cover')
  38. ));
  39. }
  40. }
  41. protected function getCleanedUrl($decode = false, $force_base_url = null)
  42. {
  43. $data = parent::getCleanedUrl($decode);
  44. $data = str_replace(' ', '-', $data);
  45. $data = strtolower($data);
  46. return $data;
  47. }
  48. /**
  49. * URL_PLAYLIST: http://www.deezer.com/fr/music/playlist/18701350
  50. * URL_ALBUM: http://www.deezer.com/fr/music/pantera/far-beyond-driven-80398
  51. *
  52. * http://www.deezer.com/fr/music/the-delta/Thing%20EP-379324
  53. * http://www.deezer.com/fr/album/379324
  54. */
  55. //public function retrieveDatas()
  56. //{
  57. // $url_clean = $this->getCleanedUrl(true);
  58. //
  59. // // album
  60. // $type = null;
  61. // $ref_id = null;
  62. // if (preg_match("#^\/[a-zA-Z_-]+\/music\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+-([0-9]+)#", $url_clean, $chaines))
  63. // {
  64. // $type = 'album';
  65. // $ref_id = $chaines[1];
  66. // }
  67. // // http://www.deezer.com/fr/album/379324
  68. // else if (preg_match("#^\/[a-zA-Z_-]+\/album\/([0-9]+)#", $url_clean, $chaines))
  69. // {
  70. // $type = 'album';
  71. // $ref_id = $chaines[1];
  72. // }
  73. // // playlist
  74. // else if (preg_match("#^\/[a-zA-Z_-]+\/music\/playlist\/([0-9]+)#", $url_clean, $chaines))
  75. // {
  76. // $type = 'playlist';
  77. // $ref_id = $chaines[1];
  78. // }
  79. // // http://www.deezer.com/track/4067216
  80. // else if (preg_match("#^\/track\/([0-9]+)#", $url_clean, $chaines))
  81. // {
  82. // $type = 'track';
  83. // $ref_id = $chaines[1];
  84. // }
  85. //
  86. //
  87. // $this->element->setData(Element::DATA_TYPE , $type);
  88. // $this->element->setData(Element::DATA_REF_ID, $ref_id);
  89. //
  90. // if ($type && $ref_id)
  91. // {
  92. // // Récupération d'infos auprès de l'API
  93. // $ch = curl_init('http://api.deezer.com/2.0/'.$type.'/'.$ref_id);
  94. // $options = array(
  95. // CURLOPT_RETURNTRANSFER => true,
  96. // CURLOPT_HTTPHEADER => array('Content-type: application/json')
  97. // );
  98. // curl_setopt_array( $ch, $options );
  99. // $result = json_decode(curl_exec($ch));
  100. //
  101. // if (isset($result->cover))
  102. // {
  103. // $this->element->setData(Element::DATA_THUMB_URL, $result->cover);
  104. // }
  105. // else if ($type == 'track')
  106. // {
  107. // if (isset($result->album))
  108. // {
  109. // if (isset($result->album->cover))
  110. // {
  111. // $this->element->setData(Element::DATA_THUMB_URL, $result->album->cover);
  112. // }
  113. // }
  114. // }
  115. //
  116. // if ($type == 'album' || $type == 'track')
  117. // {
  118. // if (isset($result->title))
  119. // {
  120. // $this->element->setData(Element::DATA_TITLE, $result->title);
  121. // }
  122. // if (isset($result->artist))
  123. // {
  124. // if (isset($result->artist->name))
  125. // {
  126. // $this->element->setData(Element::DATA_ARTIST, $result->artist->name);
  127. // }
  128. // }
  129. // }
  130. //
  131. // }
  132. //}
  133. public function proceedEmbedCode()
  134. {
  135. if (($ref_id = $this->element->getData(Element::DATA_REF_ID))
  136. && ($type = $this->element->getData(Element::DATA_TYPE))
  137. && ($this->element->getData(Element::DATA_TYPE) == 'album' ||
  138. $this->element->getData(Element::DATA_TYPE) == 'playlist'||
  139. $this->element->getData(Element::DATA_TYPE) == 'track')
  140. )
  141. {
  142. if ($type == 'track')
  143. {
  144. $type = 'tracks';
  145. }
  146. $width = $this->container->getParameter('deezer_player_width');
  147. $heigth = $this->container->getParameter('deezer_player_height');
  148. $this->element->setEmbed(
  149. '<iframe scrolling="no" frameborder="0" allowTransparency="true" '
  150. .'src="http://www.deezer.com/fr/plugins/player?autoplay=true&playlist=true'
  151. .'&width='.$width.'&height='.$heigth.'&cover=true&type='.$type.'&id='.$ref_id.'&title=&app_id=undefined"'
  152. .' width="'.$width.'" height="'.$heigth.'"></iframe>'
  153. );
  154. }
  155. }
  156. public function proceedThumbnailUrl()
  157. {
  158. if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))
  159. {
  160. $this->element->setThumbnailUrl($thumb);
  161. }
  162. }
  163. }