Quellcode durchsuchen

gestion d'urls spotify

Bastien Sevajol vor 11 Jahren
Ursprung
Commit
bc4bec7f0e

+ 2 - 0
src/Muzich/CoreBundle/Entity/Element.php Datei anzeigen

@@ -80,6 +80,8 @@ class Element
80 80
    */
81 81
   const DATA_GIFT_URL       = "data_gift_url";
82 82
   
83
+  const DATA_PLAYLIST_AUTHOR = "data_playlist_author";
84
+  
83 85
   /**
84 86
    * @ORM\Id
85 87
    * @ORM\Column(type="integer")

+ 9 - 3
src/Muzich/CoreBundle/Factory/ElementFactory.php Datei anzeigen

@@ -36,7 +36,7 @@ abstract class ElementFactory
36 36
    * 
37 37
    * @return string
38 38
    */
39
-  protected function getCleanedUrl($decode = false)
39
+  protected function getCleanedUrl($decode = false, $force_base_url = null)
40 40
   {
41 41
     // Procèdures de nettoyages après constat d'erreurs
42 42
     $url = $this->element->getUrl();
@@ -45,9 +45,15 @@ abstract class ElementFactory
45 45
       $url = urldecode($url);  
46 46
     }
47 47
     
48
+    $base_url = $this->element->getType();
49
+    if ($force_base_url)
50
+    {
51
+      $base_url = $force_base_url;
52
+    }
53
+    
48 54
     $url = str_replace('www.', '', $url);
49
-    $url = str_replace('http://'.$this->element->getType(), '', $url);
50
-    $url = str_replace('https://'.$this->element->getType(), '', $url);
55
+    $url = str_replace('http://'.$base_url, '', $url);
56
+    $url = str_replace('https://'.$base_url, '', $url);
51 57
     return $url;
52 58
   }
53 59
   

+ 157 - 0
src/Muzich/CoreBundle/Factory/Elements/Spotifycom.php Datei anzeigen

@@ -0,0 +1,157 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Factory\Elements;
4
+
5
+use Muzich\CoreBundle\Factory\ElementFactory;
6
+use Muzich\CoreBundle\Entity\Element;
7
+
8
+/**
9
+ * 
10
+ *
11
+ * @author bux
12
+ */
13
+class Spotifycom extends ElementFactory
14
+{
15
+  
16
+  /**
17
+   * 
18
+    track http url: http://open.spotify.com/track/7ylMdCOkqumPAwIMb6j2D5
19
+    track spotify uri: spotify:track:7ylMdCOkqumPAwIMb6j2D5
20
+
21
+    album hhtp url: http://open.spotify.com/album/1VAB3Xn92dPKPWzocgQqkh
22
+    album spotify uri: spotify:album:1VAB3Xn92dPKPWzocgQqkh
23
+
24
+    playlist: open.spotify.com/user/bux/playlist/2kNeCiQaATbUi3lixhNwco
25
+   */
26
+  public function retrieveDatas()
27
+  {
28
+    $url_clean = $this->getCleanedUrl(false, 'open.spotify.com');
29
+        
30
+    $type   = null;
31
+    $ref_id = null;
32
+    $get_data_from_api = false;
33
+    if (preg_match("#^\/track\/([a-zA-Z0-9]+)#", $url_clean, $chaines))
34
+    {
35
+      $type   = 'track';
36
+      $ref_id = $chaines[1];
37
+      $get_data_from_api = true;
38
+    }
39
+    if (preg_match("#^\/album\/([a-zA-Z0-9]+)#", $url_clean, $chaines))
40
+    {
41
+      $type   = 'album';
42
+      $ref_id = $chaines[1];
43
+      $get_data_from_api = true;
44
+    }
45
+    if (preg_match("#^\/user\/([a-zA-Z0-9_-]+)\/playlist\/([a-zA-Z0-9]+)#", $url_clean, $chaines))
46
+    {
47
+      $type   = 'playlist';
48
+      $this->element->setData(Element::DATA_PLAYLIST_AUTHOR, $chaines[1]);
49
+      $ref_id = $chaines[2];
50
+      $get_data_from_api = false;
51
+    }
52
+    
53
+    $this->element->setData(Element::DATA_TYPE  , $type);
54
+    $this->element->setData(Element::DATA_REF_ID, $ref_id);
55
+      
56
+    if ($get_data_from_api)
57
+    {
58
+      $this->getDataFromApi($ref_id);
59
+    }
60
+  }
61
+  
62
+  protected function getDataFromApi($ref_id)
63
+  {
64
+    if ($this->element->getData(Element::DATA_TYPE) == 'track')
65
+    {
66
+      $data = $this->getJsonDataFromApiWithUrl('http://ws.spotify.com/lookup/1/.json?uri=spotify:track:'.$ref_id);
67
+      
68
+      if (array_key_exists('track', $data))
69
+      {
70
+        if (array_key_exists('available', $data['track']))
71
+        {
72
+          if ($data['track']['available'])
73
+          {
74
+            if (array_key_exists('artist', $data['track']))
75
+            {
76
+              $this->element->setData(Element::DATA_ARTIST, $data['track']['artist']);
77
+            }
78
+            if (array_key_exists('artists', $data['track']))
79
+            {
80
+              if (count($data['track']['artists']))
81
+              {
82
+                if (array_key_exists('name', $data['track']['artists'][0]))
83
+                {
84
+                  $this->element->setData(Element::DATA_ARTIST, $data['track']['artists'][0]['name']);
85
+                }
86
+              }
87
+            }
88
+            if (array_key_exists('name', $data['track']))
89
+            {
90
+              $this->element->setData(Element::DATA_TITLE, $data['track']['name']);
91
+            }
92
+          }
93
+        }
94
+      }
95
+    }
96
+    if ($this->element->getData(Element::DATA_TYPE) == 'album')
97
+    {
98
+      $data = $this->getJsonDataFromApiWithUrl('http://ws.spotify.com/lookup/1/.json?uri=spotify:album:'.$ref_id);
99
+      
100
+      if (array_key_exists('album', $data))
101
+      {
102
+        if (array_key_exists('artist', $data['album']))
103
+        {
104
+          $this->element->setData(Element::DATA_ARTIST, $data['album']['artist']);
105
+        }
106
+        if (array_key_exists('artists', $data['album']))
107
+        {
108
+          if (count($data['album']['artists']))
109
+          {
110
+            if (array_key_exists('name', $data['album']['artists'][0]))
111
+            {
112
+              $this->element->setData(Element::DATA_ARTIST, $data['album']['artists'][0]['name']);
113
+            }
114
+          }
115
+        }
116
+        if (array_key_exists('name', $data['album']))
117
+        {
118
+          $this->element->setData(Element::DATA_TITLE, $data['album']['name']);
119
+        }
120
+      }
121
+    }
122
+    
123
+  }
124
+  
125
+  public function proceedEmbedCode()
126
+  {
127
+    if (($ref_id = $this->element->getData(Element::DATA_REF_ID)))
128
+    {
129
+      $uri = '';
130
+      $type = $this->element->getData(Element::DATA_TYPE);
131
+      
132
+      if ($type == 'playlist')
133
+      {
134
+        $uri = 
135
+          'spotify:user:'.$this->element->getData(Element::DATA_PLAYLIST_AUTHOR).':'
136
+          .'playlist:'.$ref_id
137
+        ;
138
+      }
139
+      if ($type == 'album')
140
+      {
141
+        $uri = 
142
+          'spotify:album:'.$ref_id
143
+        ;
144
+      }
145
+      if ($type == 'track')
146
+      {
147
+        $uri = 
148
+          'spotify:track:'.$ref_id
149
+        ;
150
+      }
151
+      
152
+      $this->element->setEmbed(
153
+        '<iframe src="https://embed.spotify.com/?uri='.$uri.'&theme=white" frameborder="0" allowtransparency="true"></iframe>');
154
+    }
155
+  }
156
+  
157
+}

+ 4 - 0
src/Muzich/CoreBundle/Managers/ElementManager.php Datei anzeigen

@@ -14,6 +14,7 @@ use Muzich\CoreBundle\Factory\Elements\Jamendocom;
14 14
 use Muzich\CoreBundle\Factory\Elements\Soundcloudcom;
15 15
 use Muzich\CoreBundle\Factory\Elements\Deezercom;
16 16
 use Muzich\CoreBundle\Factory\Elements\Vimeocom;
17
+use Muzich\CoreBundle\Factory\Elements\Spotifycom;
17 18
 
18 19
 /**
19 20
  * 
@@ -171,6 +172,9 @@ class ElementManager
171 172
       case 'vimeo.com':
172 173
         return new Vimeocom($this->element, $this->container);
173 174
       break;
175
+      case 'spotify.com':
176
+        return new Spotifycom($this->element, $this->container);
177
+      break;
174 178
       default:
175 179
         throw new \Exception("La Factory n'est pas prise en charge pour ce type.");
176 180
       break;