Browse Source

Finalisation de la refactorisation du code de soundcloude et jamendo

Sevajol Bastien 12 years ago
parent
commit
cd9d97aeee

+ 1 - 0
src/Muzich/CoreBundle/Entity/Element.php View File

86
   const TYPE_TRACK = 'track';
86
   const TYPE_TRACK = 'track';
87
   const TYPE_ALBUM = 'album';
87
   const TYPE_ALBUM = 'album';
88
   const TYPE_PLAYLIST =  'playlist';
88
   const TYPE_PLAYLIST =  'playlist';
89
+  const TYPE_OTHER = 'other';
89
   
90
   
90
   
91
   
91
   /**
92
   /**

+ 0 - 5
src/Muzich/CoreBundle/Factory/ElementFactory.php View File

108
     return json_decode(curl_exec($api_url), true);
108
     return json_decode(curl_exec($api_url), true);
109
   }
109
   }
110
   
110
   
111
-  protected function configureApiConnector()
112
-  {
113
-    
114
-  }
115
-  
116
 }
111
 }
117
 
112
 
118
 ?>
113
 ?>

+ 19 - 78
src/Muzich/CoreBundle/Factory/Elements/Jamendocom.php View File

6
 use Muzich\CoreBundle\Entity\Element;
6
 use Muzich\CoreBundle\Entity\Element;
7
 use Muzich\CoreBundle\Factory\UrlMatchs;
7
 use Muzich\CoreBundle\Factory\UrlMatchs;
8
 
8
 
9
-/**
10
- * 
11
- *
12
- * @author bux
13
- */
14
 class Jamendocom extends ElementFactory
9
 class Jamendocom extends ElementFactory
15
 {
10
 {
16
   
11
   
22
   
17
   
23
   public function getStreamData()
18
   public function getStreamData()
24
   {
19
   {
25
-    // On determine le type et l'url
26
-    $this->proceedTypeAndId();
27
-    
28
-    $type = $this->element->getData(Element::DATA_TYPE);
29
     $ref_id = $this->element->getData(Element::DATA_REF_ID);
20
     $ref_id = $this->element->getData(Element::DATA_REF_ID);
21
+    $api_url = "http://api.jamendo.com/get2/name+stream/track/json/?". $this->element->getData(Element::DATA_TYPE)."_id=".$ref_id;
30
     
22
     
31
-    // Récupération de données avec l'API
32
-    $api_url = null;
33
-    switch ($type)
34
-    {
35
-      case 'track':
36
-        $api_url = "http://api.jamendo.com/get2/name+stream/track/json/?track_id=".$ref_id;
37
-      break;
38
-    
39
-      case 'album':
40
-        $api_url = "http://api.jamendo.com/get2/name+stream/track/json/?album_id=".$ref_id;
41
-      break;
42
-    }
43
-    
44
-    if ($api_url)
23
+    if ($ref_id)
45
     {
24
     {
46
-      $ch = curl_init($api_url);
47
-      $options = array(
48
-        CURLOPT_RETURNTRANSFER => true,
49
-        CURLOPT_HTTPHEADER => array('Content-type: text/plain')
50
-      );
51
-      curl_setopt_array( $ch, $options );
52
-      $result = json_decode(curl_exec($ch), true);
53
-      
54
-      if (count($result))
25
+      $response = $this->getApiConnector()->getResponseForUrl($api_url);
26
+      if (count($response->getContent()))
55
       {
27
       {
56
         $data_return = array();
28
         $data_return = array();
57
         foreach ($result as $song)
29
         foreach ($result as $song)
66
     }
38
     }
67
   }
39
   }
68
   
40
   
69
-  /**
70
-   *  ALBUM = http://www.jamendo.com/fr/album/30661
71
-   *  TRACK = http://www.jamendo.com/fr/track/207079
72
-   * 
73
-   * API: http://developer.jamendo.com/fr/wiki/Musiclist2ApiFields
74
-   */
75
   public function retrieveDatas()
41
   public function retrieveDatas()
76
   {
42
   {
77
     $type = $this->element->getData(Element::DATA_TYPE);
43
     $type = $this->element->getData(Element::DATA_TYPE);
81
     $api_url = null;
47
     $api_url = null;
82
     switch ($type)
48
     switch ($type)
83
     {
49
     {
84
-      case 'album':
50
+      case Element::TYPE_ALBUM:
85
         $api_url = "http://api.jamendo.com/get2/"
51
         $api_url = "http://api.jamendo.com/get2/"
86
           ."id+name+url+image+artist_name+artist_url/album/json/?album_id=".$ref_id;
52
           ."id+name+url+image+artist_name+artist_url/album/json/?album_id=".$ref_id;
87
         $api_tag_url = "http://api.jamendo.com/get2/name+weight/tag/json/album_tag/?album_id=".$ref_id;
53
         $api_tag_url = "http://api.jamendo.com/get2/name+weight/tag/json/album_tag/?album_id=".$ref_id;
88
       break;
54
       break;
89
     
55
     
90
-      case 'track':
56
+      case Element::TYPE_TRACK:
91
         $api_url = "http://api.jamendo.com/get2/"
57
         $api_url = "http://api.jamendo.com/get2/"
92
           ."id+name+url+image+artist_name+artist_url+track_name/album/json/?track_id=".$ref_id;
58
           ."id+name+url+image+artist_name+artist_url+track_name/album/json/?track_id=".$ref_id;
93
         $api_tag_url = "http://api.jamendo.com/get2/name+weight/tag/json/track_tag/?track_id=".$ref_id;
59
         $api_tag_url = "http://api.jamendo.com/get2/name+weight/tag/json/track_tag/?track_id=".$ref_id;
96
     
62
     
97
     if ($api_url)
63
     if ($api_url)
98
     {
64
     {
99
-      $ch = curl_init($api_url);
100
-      $options = array(
101
-        CURLOPT_RETURNTRANSFER => true,
102
-        CURLOPT_HTTPHEADER => array('Content-type: text/plain')
103
-      );
104
-      curl_setopt_array( $ch, $options );
105
-      $result = json_decode(curl_exec($ch), true);
106
-      
107
-      if (count($result))
65
+      if (($response = $this->getApiConnector()->getResponseForUrl($api_url)))
108
       {
66
       {
109
-        // Thumb
110
-        if (array_key_exists('image', $result[0]))
111
-        {
112
-          $this->element->setData(Element::DATA_THUMB_URL, $result[0]['image']);
113
-        }
67
+        // Check si tout se passe bien si pas de retour de l'api
68
+        $this->getApiConnector()->setElementDatasWithResponse($response, array(
69
+          Element::DATA_THUMB_URL      => array(0 => 'image'),
70
+          Element::DATA_ARTIST         => array(0 => 'artist_name'),
71
+        ));
114
         
72
         
115
-        // Album name
116
-        if (array_key_exists('name', $result[0]) && $type == 'album')
73
+        if ($this->url_analyzer->getType() == Element::TYPE_ALBUM)
117
         {
74
         {
118
-          $this->element->setData(Element::DATA_TITLE, $result[0]['name']);
75
+          $this->element->setData(Element::DATA_TITLE, $response->get(array(0 => 'name')));
119
         }
76
         }
120
-        
121
-        // Artist name
122
-        if (array_key_exists('artist_name', $result[0]))
123
-        {
124
-          $this->element->setData(Element::DATA_ARTIST, $result[0]['artist_name']);
125
-        }
126
-        
127
-        // track name
128
-        if (array_key_exists('track_name', $result[0])  && $type == 'track')
77
+        if ($this->url_analyzer->getType() == Element::TYPE_TRACK)
129
         {
78
         {
130
-          $this->element->setData(Element::DATA_TITLE, $result[0]['track_name']);
79
+          $this->element->setData(Element::DATA_TITLE, $response->get(array(0 => 'track_name')));
131
         }
80
         }
132
         
81
         
133
-        // Maintenant au tour des tags (deuxième requete a l'api)
134
-        $ch = curl_init($api_tag_url);
135
-        $options = array(
136
-          CURLOPT_RETURNTRANSFER => true,
137
-          CURLOPT_HTTPHEADER => array('Content-type: text/plain')
138
-        );
139
-        curl_setopt_array( $ch, $options );
140
-        $result = json_decode(curl_exec($ch), true);
141
-      
142
-        if (count($result))
82
+        if (($response = $this->getApiConnector()->getResponseForUrl($api_url)))
143
         {
83
         {
84
+          // TODO: Check si tout ce passe bien avec pas de tags en retour de l'api
144
           $tags = array();
85
           $tags = array();
145
-          foreach ($result as $tag)
86
+          foreach ($result->getContent() as $tag)
146
           {
87
           {
147
             $tags[] = $tag['name'];
88
             $tags[] = $tag['name'];
148
           }
89
           }

+ 10 - 40
src/Muzich/CoreBundle/Factory/Elements/Soundcloudcom.php View File

6
 use Muzich\CoreBundle\Entity\Element;
6
 use Muzich\CoreBundle\Entity\Element;
7
 use Muzich\CoreBundle\Util\TagLike;
7
 use Muzich\CoreBundle\Util\TagLike;
8
 use Muzich\CoreBundle\lib\Api\Response as ApiResponse;
8
 use Muzich\CoreBundle\lib\Api\Response as ApiResponse;
9
+use Muzich\CoreBundle\Factory\UrlMatchs;
9
 
10
 
10
-/**
11
- * 
12
- *
13
- * @author bux
14
- */
15
 class Soundcloudcom extends ElementFactory
11
 class Soundcloudcom extends ElementFactory
16
-{  
17
-  /**
18
-   * ??SET = http://soundcloud.com/matas/sets/library-project
19
-   * ??    = http://soundcloud.com/matas/anadrakonic-waltz
20
-   */
12
+{
13
+  
14
+  public function __construct(Element $element, Container $container, EntityManager $entity_manager)
15
+  {
16
+    parent::__construct($element, $container, $entity_manager);
17
+    $this->url_matchs = UrlMatchs::$soundcloud;
18
+  }
19
+  
21
   public function retrieveDatas()
20
   public function retrieveDatas()
22
   {
21
   {
23
-    $url_clean = $this->getCleanedUrl();
24
-    
25
-    $match = false;
26
-    //
27
-    if (preg_match("#^\/[a-zA-Z0-9_-]+\/sets\/[a-zA-Z0-9_-]+#", $url_clean, $chaines))
28
-    {
29
-      $match = true;
30
-    }
31
-    // /noisia/black-sun-empire-noisia-feed
32
-    // /user4818423/mechanika-crew-andrew-dj-set
33
-    else if (preg_match("#^\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+#", $url_clean, $chaines))
34
-    {
35
-      $match = true;
36
-    }
37
-    
38
-    // On en gère pas encore les recherches
39
-    if (preg_match("#\/search\?q#", $url_clean, $chaines))
40
-    {
41
-      $match = false;
42
-    }
43
-    
44
-    /////////////
45
-    if ($match)
22
+    if ($this->url_analyzer->haveMatch())
46
     {
23
     {
47
       $this->setElementDatasWithApi();
24
       $this->setElementDatasWithApi();
48
     }
25
     }
49
-    
50
   }
26
   }
51
   
27
   
52
   protected function setElementDatasWithApi()
28
   protected function setElementDatasWithApi()
158
           .'width="100%" name="embed_'.$embed_id.'"></embed>
134
           .'width="100%" name="embed_'.$embed_id.'"></embed>
159
         </object>'
135
         </object>'
160
       );
136
       );
161
-//      $this->element->setEmbed(
162
-//        '<iframe id="sc-widget_'.$this->element->getData(Element::DATA_REF_ID).
163
-//          '" src="http://w.soundcloud.com/player/?url='.
164
-//          $ref_id.'" width="100%" '.
165
-//          'height="'.$height.'" scrolling="no" frameborder="no"></iframe>'
166
-//      );
167
     }
137
     }
168
   }
138
   }
169
   
139
   

+ 10 - 0
src/Muzich/CoreBundle/Factory/UrlMatchs.php View File

20
       "#^\/[a-zA-Z0-9_-]+\/list\/a([0-9]+)\/.#" => 1
20
       "#^\/[a-zA-Z0-9_-]+\/list\/a([0-9]+)\/.#" => 1
21
     )
21
     )
22
   );
22
   );
23
+  
24
+  public static $soundcloud = array(
25
+    Element::TYPE_OTHER => array(
26
+      // http://soundcloud.com/matas/sets/library-project
27
+      "#^\/[a-zA-Z0-9_-]+\/sets\/[a-zA-Z0-9_-]+#" => null,
28
+      // http://soundcloud.com/noisia/black-sun-empire-noisia-feed
29
+      // http://soundcloud.com/user4818423/mechanika-crew-andrew-dj-set
30
+      "#^\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+#" => null
31
+    )
32
+  );
23
 }
33
 }

+ 5 - 0
src/Muzich/CoreBundle/lib/Api/Response.php View File

82
     return null;
82
     return null;
83
   }
83
   }
84
   
84
   
85
+  public function getContent()
86
+  {
87
+    return $this->content;
88
+  }
89
+  
85
 }
90
 }

+ 7 - 0
src/Muzich/CoreBundle/lib/Element/UrlAnalyzer.php View File

32
               Element::DATA_REF_ID => $preg_result[$ref_id_position]
32
               Element::DATA_REF_ID => $preg_result[$ref_id_position]
33
             );
33
             );
34
           }
34
           }
35
+          elseif ($ref_id_position === null)
36
+          {
37
+            return array(
38
+              Element::DATA_TYPE => $type,
39
+              Element::DATA_REF_ID => null
40
+            );
41
+          }
35
         }
42
         }
36
       }
43
       }
37
     }
44
     }