瀏覽代碼

Refactorisation de la factory deezer

Sevajol Bastien 11 年之前
父節點
當前提交
e875600a5f

+ 112 - 78
src/Muzich/CoreBundle/Factory/Elements/Deezercom.php 查看文件

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

+ 17 - 0
src/Muzich/CoreBundle/Factory/UrlMatchs.php 查看文件

@@ -44,4 +44,21 @@ class UrlMatchs
44 44
       "#\/(video\/)([a-zA-Z0-9]+)([a-zA-Z0-9_-]*)#" => 2
45 45
     )
46 46
   );
47
+  
48
+  public static $deezer = array(
49
+    Element::TYPE_ALBUM => array(
50
+      // http://www.deezer.com/fr/music/pantera/far-beyond-driven-80398
51
+      "#^\/[a-zA-Z_-]+\/music\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+-([0-9]+)#" => 1,
52
+      // http://www.deezer.com/fr/album/379324
53
+      "#^\/[a-zA-Z_-]+\/album\/([0-9]+)#" => 1
54
+    ),
55
+    Element::TYPE_PLAYLIST => array(
56
+      // http://www.deezer.com/fr/music/playlist/18701350
57
+      "#^\/[a-zA-Z_-]+\/music\/playlist\/([0-9]+)#" => 1
58
+    ),
59
+    Element::TYPE_TRACK => array(
60
+      // http://www.deezer.com/track/4067216
61
+      "#^\/track\/([0-9]+)#" => 1
62
+    )
63
+  );
47 64
 }

+ 2 - 1
src/Muzich/CoreBundle/Tests/ElementFactory/ElementFactoryTest.php 查看文件

@@ -553,7 +553,8 @@ class ElementFactoryTest extends UnitTest
553 553
     //
554 554
     //$this->assertEquals(array(
555 555
     //  'data_ref_id' => '18701350',
556
-    //  'data_type' => 'playlist',
556
+    //  'data_type'   => 'playlist',
557
+    //  'data_title'  => 'Trucs Cools'
557 558
     //),$this->proceed_element_datas_api(
558 559
     //  $bux, 
559 560
     //  'http://www.deezer.com/fr/music/playlist/18701350'

+ 4 - 1
src/Muzich/CoreBundle/lib/Api/Connector.php 查看文件

@@ -31,7 +31,10 @@ class Connector
31 31
   {
32 32
     foreach ($parameters as $data_id => $searched)
33 33
     {
34
-      $this->element->setData($data_id, $response->get($searched));
34
+      if ($response->have($searched))
35
+      {
36
+        $this->element->setData($data_id, $response->get($searched));
37
+      }
35 38
     }
36 39
   }
37 40