瀏覽代碼

Debut de la refactorisation du traitement des url pour determiner la nature du partage.

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

+ 35 - 0
src/Muzich/CoreBundle/Entity/Element.php 查看文件

@@ -82,6 +82,12 @@ class Element
82 82
   
83 83
   const DATA_PLAYLIST_AUTHOR = "data_playlist_author";
84 84
   
85
+  
86
+  const TYPE_TRACK = 'track';
87
+  const TYPE_ALBUM = 'album';
88
+  const TYPE_PLAYLIST =  'playlist';
89
+  
90
+  
85 91
   /**
86 92
    * @ORM\Id
87 93
    * @ORM\Column(type="integer")
@@ -999,4 +1005,33 @@ class Element
999 1005
     return $this->hasTagsProposition();
1000 1006
   }
1001 1007
   
1008
+  // TODO: Pas booo ! Ecrit comme ca pour le moment lors de la refacto de UrlAnalyzer
1009
+  public function getCleanedUrl()
1010
+  {
1011
+    // Procèdures de nettoyages après constat d'erreurs
1012
+    $url = $this->getUrl();
1013
+    if ($this->getType() == 'deezer.com')
1014
+    {
1015
+      $url = urldecode($url);  
1016
+    }
1017
+    
1018
+    $base_url = $this->getType();
1019
+    if ($this->getType() == 'spotify.com')
1020
+    {
1021
+      $base_url = 'open.spotify.com';
1022
+    }
1023
+    
1024
+    $url = str_replace('www.', '', $url);
1025
+    $url = str_replace('http://'.$base_url, '', $url);
1026
+    $url = str_replace('https://'.$base_url, '', $url);
1027
+    
1028
+    if ($this->getType() == 'deezer.com')
1029
+    {
1030
+      $url = str_replace(' ', '-', $url);
1031
+      $url = strtolower($url);
1032
+    }
1033
+    
1034
+    return $url;
1035
+  }
1036
+  
1002 1037
 }

+ 14 - 0
src/Muzich/CoreBundle/Factory/ElementFactory.php 查看文件

@@ -6,6 +6,7 @@ use Muzich\CoreBundle\Entity\Element;
6 6
 use Symfony\Component\DependencyInjection\Container;
7 7
 use Doctrine\ORM\EntityManager;
8 8
 use Muzich\CoreBundle\lib\Api\Connector as ApiConnector;
9
+use Muzich\CoreBundle\lib\Element\UrlAnalyzer;
9 10
 
10 11
 /**
11 12
  *
@@ -18,6 +19,9 @@ abstract class ElementFactory
18 19
   protected $container;
19 20
   protected $entity_manager;
20 21
   protected $api_connector;
22
+  protected $url_analyzer;
23
+  
24
+  protected $url_matchs = array();
21 25
   
22 26
   /**
23 27
    *
@@ -30,6 +34,7 @@ abstract class ElementFactory
30 34
     $this->container = $container;
31 35
     $this->entity_manager = $entity_manager;
32 36
     $this->api_connector = new ApiConnector($element);
37
+    $this->url_analyzer = new UrlAnalyzer($element, $this->url_matchs);
33 38
   }
34 39
   
35 40
   protected function getApiConnector()
@@ -37,6 +42,15 @@ abstract class ElementFactory
37 42
     return $this->api_connector;
38 43
   }
39 44
   
45
+  public function setUrlDatas()
46
+  {
47
+    if ($this->url_analyzer->haveMatch())
48
+    {
49
+      $this->element->setData(Element::DATA_TYPE  , $this->url_analyzer->getType());
50
+      $this->element->setData(Element::DATA_REF_ID, $this->url_analyzer->getRefId());
51
+    }
52
+  }
53
+  
40 54
   /**
41 55
    * Retourne l'url relative dans le site
42 56
    * 

+ 4 - 39
src/Muzich/CoreBundle/Factory/Elements/Jamendocom.php 查看文件

@@ -4,6 +4,7 @@ namespace Muzich\CoreBundle\Factory\Elements;
4 4
 
5 5
 use Muzich\CoreBundle\Factory\ElementFactory;
6 6
 use Muzich\CoreBundle\Entity\Element;
7
+use Muzich\CoreBundle\Factory\UrlMatchs;
7 8
 
8 9
 /**
9 10
  * 
@@ -13,43 +14,10 @@ use Muzich\CoreBundle\Entity\Element;
13 14
 class Jamendocom extends ElementFactory
14 15
 {
15 16
   
16
-  protected function proceedTypeAndId()
17
+  public function __construct(Element $element, Container $container, EntityManager $entity_manager)
17 18
   {
18
-    $url_clean = $this->getCleanedUrl();
19
-        
20
-    // album
21
-    // http://www.jamendo.com/fr/album/3409
22
-    $type   = null;
23
-    $ref_id = null;
24
-    if (preg_match("#^\/[a-zA-Z0-9_-]+\/album\/([0-9]+)#", $url_clean, $chaines))
25
-    {
26
-      $type   = 'album';
27
-      $ref_id = $chaines[1];
28
-    }
29
-    // track
30
-    // http://www.jamendo.com/fr/track/894974
31
-    else if (preg_match("#^\/[a-zA-Z0-9_-]+\/track\/([0-9]+)#", $url_clean, $chaines))
32
-    {
33
-      $type = 'track';
34
-      $ref_id = $chaines[1];
35
-    }
36
-    // album new ver
37
-    // http://www.jamendo.com/fr/list/a45666/proceed-positron...
38
-    else if (preg_match("#^\/[a-zA-Z0-9_-]+\/list\/a([0-9]+)\/.#", $url_clean, $chaines))
39
-    {
40
-      $type   = 'album';
41
-      $ref_id = $chaines[1];
42
-    }
43
-    // track new ver
44
-    // http://www.jamendo.com/fr/track/347602/come-come
45
-    else if (preg_match("#^\/[a-zA-Z0-9_-]+\/track\/([0-9]+)\/.#", $url_clean, $chaines))
46
-    {
47
-      $type = 'track';
48
-      $ref_id = $chaines[1];
49
-    }
50
-    
51
-    $this->element->setData(Element::DATA_TYPE  , $type);
52
-    $this->element->setData(Element::DATA_REF_ID, $ref_id);
19
+    parent::__construct($element, $container, $entity_manager);
20
+    $this->url_matchs = UrlMatchs::$jamendo;
53 21
   }
54 22
   
55 23
   public function getStreamData()
@@ -106,9 +74,6 @@ class Jamendocom extends ElementFactory
106 74
    */
107 75
   public function retrieveDatas()
108 76
   {
109
-    // On determine le type et l'url
110
-    $this->proceedTypeAndId();
111
-    
112 77
     $type = $this->element->getData(Element::DATA_TYPE);
113 78
     $ref_id = $this->element->getData(Element::DATA_REF_ID);
114 79
     

+ 0 - 153
src/Muzich/CoreBundle/Factory/Elements/Soundcloudcom.php 查看文件

@@ -47,159 +47,6 @@ class Soundcloudcom extends ElementFactory
47 47
       $this->setElementDatasWithApi();
48 48
     }
49 49
     
50
-    ////$this->element->setData(Element::DATA_REF_ID, $this->element->getUrl());
51
-    //
52
-    //// récupération de données avec l'API
53
-    //if ($match)
54
-    //{
55
-    //  
56
-    //  
57
-    //  
58
-    //  
59
-    //  
60
-    //  // La première étape consiste a résoudre l'url
61
-    //  $ch = curl_init('http://api.soundcloud.com/resolve.json?url='.$this->element->getUrl().'&client_id=39946ea18e3d78d64c0ac95a025794e1');
62
-    //
63
-    //  $options = array(
64
-    //    CURLOPT_RETURNTRANSFER => true,
65
-    //    CURLOPT_HTTPHEADER => array('Content-type: application/json')
66
-    //  );
67
-    //
68
-    //  curl_setopt_array( $ch, $options );
69
-    //  $result = json_decode(curl_exec($ch));
70
-    //
71
-    //  if (isset($result->errors))
72
-    //  {
73
-    //    if (count($result->errors))
74
-    //    {
75
-    //      return;  
76
-    //    }
77
-    //  }
78
-    //  
79
-    //  if (!isset($result->location))
80
-    //  {
81
-    //    return;
82
-    //  }
83
-    //  
84
-    //  if (!$result->location)
85
-    //  {
86
-    //    return;
87
-    //  }
88
-    //  
89
-    //  $getjsonurl = $result->location;
90
-    //  // On a maintenant la bonne url pour demander les infos
91
-    //  $ch = curl_init($getjsonurl);
92
-    //  curl_setopt_array($ch, $options);
93
-    //  $result = json_decode(curl_exec($ch), true);
94
-    //  
95
-    //  // En premier lieux il nous faut être sur d'avoir le droit d'utiliser le lecteur exportable
96
-    //  $sharing = false;
97
-    //  if (array_key_exists('sharing', $result) && array_key_exists('embeddable_by', $result))
98
-    //  {
99
-    //    if ($result['sharing'] == 'public' && ($result['embeddable_by'] == 'all' || $result['embeddable_by'] == 'me'))
100
-    //    {
101
-    //      $sharing = true;
102
-    //    }
103
-    //  }
104
-    //  
105
-    //  if ($sharing)
106
-    //  {
107
-    //    if (array_key_exists('id', $result) )
108
-    //    {
109
-    //      $this->element->setData(Element::DATA_REF_ID, $result['id']);
110
-    //    }
111
-    //    
112
-    //    if (array_key_exists('uri', $result) )
113
-    //    {
114
-    //      $this->element->setData(Element::DATA_NORMALIZED_URL, $result['uri']);
115
-    //    }
116
-    //
117
-    //    if (array_key_exists('artwork_url', $result) )
118
-    //    {
119
-    //      if ($result['artwork_url'])
120
-    //      {
121
-    //        $this->element->setData(Element::DATA_THUMB_URL, $result['artwork_url']);
122
-    //      }
123
-    //      else
124
-    //      {
125
-    //        if (array_key_exists('user', $result) )
126
-    //        {
127
-    //          if (array_key_exists('avatar_url', $result['user']) )
128
-    //          {
129
-    //            if ($result['user']['avatar_url'])
130
-    //            {
131
-    //              $this->element->setData(Element::DATA_THUMB_URL, $result['user']['avatar_url']);
132
-    //            }
133
-    //          }
134
-    //        }
135
-    //      }
136
-    //    }
137
-    //    
138
-    //    if (array_key_exists('kind', $result) )
139
-    //    {
140
-    //      $this->element->setData(Element::DATA_TYPE, $result['kind']);
141
-    //    }
142
-    //
143
-    //    if (array_key_exists('downloadable', $result) )
144
-    //    {
145
-    //      $this->element->setData(Element::DATA_DOWNLOAD, $result['downloadable']);
146
-    //      // FIXME
147
-    //      $this->element->setData(Element::DATA_DOWNLOAD_URL, $this->element->getUrl().'/download');
148
-    //    }
149
-    //
150
-    //    if (array_key_exists('title', $result) )
151
-    //    {
152
-    //      $this->element->setData(Element::DATA_TITLE, $result['title']);
153
-    //    }
154
-    //
155
-    //    if (array_key_exists('user', $result) )
156
-    //    {
157
-    //      $this->element->setData(Element::DATA_ARTIST, $result['user']['username']);
158
-    //    }
159
-    //    
160
-    //    $genres = '';
161
-    //    if (array_key_exists('genre', $result) )
162
-    //    {
163
-    //      if (strlen($result['genre']))
164
-    //      {
165
-    //        $genres = $result['genre'];
166
-    //      }
167
-    //    }
168
-    //    
169
-    //    $tags_list = '';
170
-    //    if (array_key_exists('tag_list', $result) )
171
-    //    {
172
-    //      if (strlen($result['tag_list']))
173
-    //      {
174
-    //        $tags_list = $result['tag_list'];
175
-    //      }
176
-    //    }
177
-    //    
178
-    //    $tags_string = $genres.' '.$tags_list.' '.str_replace(' ', '-', $genres);
179
-    //    $tags_like = array();
180
-    //    if (strlen($tags_string))
181
-    //    {
182
-    //      $tag_like = new TagLike($this->entity_manager);
183
-    //      foreach (explode(' ', $tags_string) as $word)
184
-    //      {
185
-    //        $similar_tags = $tag_like->getSimilarTags($word, ($this->element->getOwner())?$this->element->getOwner()->getId():null);
186
-    //        if (count($similar_tags))
187
-    //        {
188
-    //          if ($similar_tags['same_found'])
189
-    //          {
190
-    //            $tags_like[] = $similar_tags['tags'][0]['name'];
191
-    //          }
192
-    //        }
193
-    //      }
194
-    //      $tags_like[] = $genres;
195
-    //      if (count($tags_like))
196
-    //      {
197
-    //        $this->element->setData(Element::DATA_TAGS, array_unique($tags_like));
198
-    //      }
199
-    //    }
200
-    //    
201
-    //  }
202
-    //}
203 50
   }
204 51
   
205 52
   protected function setElementDatasWithApi()

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

@@ -0,0 +1,23 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Factory;
4
+
5
+use Muzich\CoreBundle\Entity\Element;
6
+
7
+class UrlMatchs
8
+{
9
+  public static $jamendo = array(
10
+    Element::TYPE_TRACK => array(
11
+      // http://www.jamendo.com/fr/track/894974
12
+      "#^\/[a-zA-Z0-9_-]+\/track\/([0-9]+)#" => 1,
13
+      // http://www.jamendo.com/fr/track/347602/come-come
14
+      "#^\/[a-zA-Z0-9_-]+\/track\/([0-9]+)\/.#" => 1
15
+    ),
16
+    Element::TYPE_ALBUM => array(
17
+      // http://www.jamendo.com/fr/album/3409
18
+      "#^\/[a-zA-Z0-9_-]+\/album\/([0-9]+)#" => 1,
19
+      // http://www.jamendo.com/fr/list/a45666/proceed-positron...
20
+      "#^\/[a-zA-Z0-9_-]+\/list\/a([0-9]+)\/.#" => 1
21
+    )
22
+  );
23
+}

+ 1 - 0
src/Muzich/CoreBundle/Managers/ElementManager.php 查看文件

@@ -136,6 +136,7 @@ class ElementManager
136 136
     {
137 137
       $site_factory = $this->getFactory();
138 138
       // On récupères les datas de l'élément
139
+      $site_factory->setUrlDatas();
139 140
       $site_factory->retrieveDatas();
140 141
       // On procède a la construction de nos informations
141 142
       $site_factory->proceedEmbedCode();

+ 49 - 0
src/Muzich/CoreBundle/Tests/ElementFactory/UrlAnalyzerTest.php 查看文件

@@ -0,0 +1,49 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Api;
4
+
5
+use Muzich\CoreBundle\lib\Element\UrlAnalyzer;
6
+use Muzich\CoreBundle\Entity\Element;
7
+use Muzich\CoreBundle\Factory\Elements\Jamendocom;
8
+use Muzich\CoreBundle\Factory\UrlMatchs;
9
+
10
+class UrlAnalyzerTest extends \PHPUnit_Framework_TestCase
11
+{
12
+  
13
+  protected function getNewElement($type, $url)
14
+  {
15
+    $element = new Element();
16
+    $element->setUrl($url);
17
+    $element->setType($type);
18
+    return $element;
19
+  }
20
+  
21
+  public function testJamendo()
22
+  {
23
+    $url_analyzer = new UrlAnalyzer($this->getNewElement('jamendo.com', 'http://www.jamendo.com/fr/track/894974'), UrlMatchs::$jamendo);
24
+    $this->assertTrue($url_analyzer->haveMatch());
25
+    $this->assertEquals(Element::TYPE_TRACK, $url_analyzer->getType());
26
+    $this->assertEquals('894974', $url_analyzer->getRefId());
27
+    
28
+    $url_analyzer = new UrlAnalyzer($this->getNewElement('jamendo.com', 'http://www.jamendo.com/fr/track/347602/come-come'), UrlMatchs::$jamendo);
29
+    $this->assertTrue($url_analyzer->haveMatch());
30
+    $this->assertEquals(Element::TYPE_TRACK, $url_analyzer->getType());
31
+    $this->assertEquals('347602', $url_analyzer->getRefId());
32
+    
33
+    $url_analyzer = new UrlAnalyzer($this->getNewElement('jamendo.com', 'http://www.jamendo.com/fr/album/3409'), UrlMatchs::$jamendo);
34
+    $this->assertTrue($url_analyzer->haveMatch());
35
+    $this->assertEquals(Element::TYPE_ALBUM, $url_analyzer->getType());
36
+    $this->assertEquals('3409', $url_analyzer->getRefId());
37
+    
38
+    $url_analyzer = new UrlAnalyzer($this->getNewElement('jamendo.com', 'http://www.jamendo.com/fr/list/a45666/proceed-positron...'), UrlMatchs::$jamendo);
39
+    $this->assertTrue($url_analyzer->haveMatch());
40
+    $this->assertEquals(Element::TYPE_ALBUM, $url_analyzer->getType());
41
+    $this->assertEquals('45666', $url_analyzer->getRefId());
42
+    
43
+    $url_analyzer = new UrlAnalyzer($this->getNewElement('jamendo.com', 'http://www.jamendo.com/fr/playlist/2134354'), UrlMatchs::$jamendo);
44
+    $this->assertFalse($url_analyzer->haveMatch());
45
+    $this->assertEquals(null, $url_analyzer->getType());
46
+    $this->assertEquals(null, $url_analyzer->getRefId());
47
+  }
48
+  
49
+}

+ 72 - 0
src/Muzich/CoreBundle/lib/Element/UrlAnalyzer.php 查看文件

@@ -0,0 +1,72 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\lib\Element;
4
+
5
+use Muzich\CoreBundle\Entity\Element;
6
+
7
+class UrlAnalyzer
8
+{
9
+  
10
+  protected $url;
11
+  protected $match_rules;
12
+  
13
+  // TODO: Ce serait bien que le match_rules soit dans un Element fille
14
+  public function __construct(Element $element, $match_rules = array())
15
+  {
16
+    $this->url = $element->getCleanedUrl();
17
+    $this->match_rules = $match_rules;
18
+  }
19
+  
20
+  protected function getMatchRules()
21
+  {
22
+    foreach ($this->match_rules as $type => $rules)
23
+    {
24
+      foreach ($rules as $expression => $ref_id_position)
25
+      {
26
+        if (preg_match($expression, $this->url, $preg_result))
27
+        {
28
+          if (array_key_exists($ref_id_position, $preg_result))
29
+          {
30
+            return array(
31
+              Element::DATA_TYPE => $type,
32
+              Element::DATA_REF_ID => $preg_result[$ref_id_position]
33
+            );
34
+          }
35
+        }
36
+      }
37
+    }
38
+    
39
+    return false;
40
+  }
41
+  
42
+  public function haveMatch()
43
+  {
44
+    if ($this->getMatchRules() !== false)
45
+    {
46
+      return true;
47
+    }
48
+    
49
+    return false;
50
+  }
51
+  
52
+  public function getRefId()
53
+  {
54
+    if (($match_rules = $this->getMatchRules()))
55
+    {
56
+      return $match_rules[Element::DATA_REF_ID];
57
+    }
58
+    
59
+    return null;
60
+  }
61
+  
62
+  public function getType()
63
+  {
64
+    if (($match_rules = $this->getMatchRules()))
65
+    {
66
+      return $match_rules[Element::DATA_TYPE];
67
+    }
68
+    
69
+    return null;
70
+  }
71
+  
72
+}