Browse Source

Logiciel: Evolution #739: Ajouter Grooveshark, mixcloud .. et autre ?

Bastien Sevajol 10 years ago
parent
commit
8d7619e28f

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

@@ -119,13 +119,13 @@ abstract class ElementFactory
119 119
     return $this->url_analyzer;
120 120
   }
121 121
   
122
-  protected function setDataTagsForElement($tags_string, $merge = array())
122
+  protected function setDataTagsForElement($tags_string, $merge = array(), $separator = ' ')
123 123
   {
124 124
     $tags_like = array();
125 125
     if (strlen(trim($tags_string)))
126 126
     {
127 127
       $tag_like = new TagLike($this->entity_manager);
128
-      foreach (explode(' ', $tags_string) as $word)
128
+      foreach (explode($separator, $tags_string) as $word)
129 129
       {
130 130
         $similar_tags = $tag_like->getSimilarTags($word, ($this->element->getOwner())?$this->element->getOwner()->getId():null);
131 131
         if (count($similar_tags))

+ 83 - 0
src/Muzich/CoreBundle/Factory/Elements/Mixcloudcom.php View File

@@ -0,0 +1,83 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Factory\Elements;
4
+
5
+use Muzich\CoreBundle\Factory\ElementFactory;
6
+use Muzich\CoreBundle\Entity\Element;
7
+use Muzich\CoreBundle\lib\Api\Response as ApiResponse;
8
+use Muzich\CoreBundle\Factory\UrlMatchs;
9
+use Symfony\Component\DependencyInjection\Container;
10
+use Doctrine\ORM\EntityManager;
11
+
12
+class Mixcloudcom extends ElementFactory
13
+{
14
+  
15
+  public function __construct(Element $element, Container $container, EntityManager $entity_manager)
16
+  {
17
+    $this->url_matchs = UrlMatchs::$mixcloud;
18
+    parent::__construct($element, $container, $entity_manager);
19
+  }
20
+  
21
+  public function proceedDatas()
22
+  {
23
+    $this->setElementDatasWithApi();
24
+    $this->proceedThumbnailUrl();
25
+    $this->proceedEmbedCode();
26
+  }
27
+  
28
+  protected function setElementDatasWithApi()
29
+  {
30
+    if (($response = $this->getApiDatasResponse()))
31
+    {
32
+      $this->setElementSharingData($response);
33
+      $this->setTagsData($response);
34
+    }
35
+  }
36
+  
37
+  protected function getApiDatasResponse()
38
+  {
39
+    if (($response = $this->getApiConnector()->getResponseForUrl('http://api.mixcloud.com'.$this->getCleanedUrl())))
40
+      if ($response->haveNot('error'))
41
+        return $response;
42
+    
43
+    return null;
44
+  }
45
+  
46
+  protected function setElementSharingData(ApiResponse $response)
47
+  {
48
+    $this->getApiConnector()->setElementDatasWithResponse($response, array(
49
+      Element::DATA_TITLE          => 'name',
50
+      Element::DATA_NORMALIZED_URL => 'url',
51
+      Element::DATA_REF_ID         => 'key',
52
+      Element::DATA_TYPE           => Element::TYPE_TRACK,
53
+      Element::DATA_ARTIST         => array('user' => 'name'),
54
+      Element::DATA_THUMB_URL      => array('pictures' => 'medium')
55
+    ));
56
+  }
57
+  
58
+  protected function setTagsData(ApiResponse $response)
59
+  {
60
+    $tags = $response->get('tags');
61
+    $tags_array = array();
62
+    if (count($tags))
63
+    {
64
+      foreach ($tags as $tag_data)
65
+      {
66
+        $tags_array[] = $tag_data['name'];
67
+      }
68
+    }
69
+    $this->setDataTagsForElement(implode(' ', $tags_array));
70
+  }
71
+  
72
+  public function proceedEmbedCode()
73
+  {
74
+    if (($response = $this->getApiConnector()->getResponseForUrl(
75
+          'http://api.mixcloud.com'.$this->element->getData(Element::DATA_REF_ID).'embed-json/?width=100%'
76
+       )))
77
+    {
78
+      if ($response->get('html'))
79
+        $this->element->setEmbed($response->get('html'));
80
+    }
81
+  }
82
+  
83
+}

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

@@ -107,4 +107,11 @@ class UrlMatchs
107 107
       "#\/(watch|)(\?|)feature\=player_embedded\&v=([a-zA-Z0-9_-]+)([.\w\W\d]*)#" => 3,
108 108
     )
109 109
   );
110
+  
111
+  public static $mixcloud = array(
112
+    Element::TYPE_TRACK => array(
113
+      // http://www.mixcloud.com/nevrakse_ISM/nevrakse-tranceplantation/
114
+      "#^\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+[\/]*#" => null,
115
+    )
116
+  );
110 117
 }

+ 4 - 0
src/Muzich/CoreBundle/Managers/ElementManager.php View File

@@ -16,6 +16,7 @@ use Muzich\CoreBundle\Factory\Elements\Sndsc;
16 16
 use Muzich\CoreBundle\Factory\Elements\Deezercom;
17 17
 use Muzich\CoreBundle\Factory\Elements\Vimeocom;
18 18
 use Muzich\CoreBundle\Factory\Elements\Spotifycom;
19
+use Muzich\CoreBundle\Factory\Elements\Mixcloudcom;
19 20
 
20 21
 /**
21 22
  * 
@@ -181,6 +182,9 @@ class ElementManager
181 182
       case 'spotify.com':
182 183
         return new Spotifycom($this->element, $this->container, $this->em);
183 184
       break;
185
+      case 'mixcloud.com':
186
+        return new Mixcloudcom($this->element, $this->container, $this->em);
187
+      break;
184 188
       default:
185 189
         throw new \Exception("La Factory n'est pas prise en charge pour le type \"".$this->element->getType()."\".");
186 190
       break;

+ 30 - 0
src/Muzich/CoreBundle/Tests/ElementFactory/ElementFactoryTest.php View File

@@ -284,6 +284,15 @@ class ElementFactoryTest extends UnitTest
284 284
         .'"></iframe>'
285 285
     );
286 286
     
287
+    $this->proceed_elementAndFill(
288
+      $bux, 
289
+      'poil de carotte', 
290
+      'http://www.mixcloud.com/nevrakse_ISM/nevrakse-tranceplantation/', 
291
+      array($hardtek->getId(), $tribe->getId()), 
292
+      null,
293
+      'src="//www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2Fnevrakse_ISM%2Fnevrakse-tranceplantation%2F&amp;emb'
294
+    );
295
+    
287 296
   }
288 297
   
289 298
   public function testDataApiengine()
@@ -647,6 +656,27 @@ class ElementFactoryTest extends UnitTest
647 656
       'http://open.spotify.com/track/3d5FWJe19DkUJaO2wDEQHY'
648 657
     ));
649 658
     
659
+    /*
660
+     * Mixcloud
661
+     *
662
+     */
663
+    
664
+    $this->assertEquals(array(
665
+      'data_ref_id' => '/nevrakse_ISM/nevrakse-tranceplantation/',
666
+      'data_title'  => 'Nevrakse - Tranceplantation',
667
+      'data_artist' => 'nevrakse_ISM',
668
+      'data_type'   => 'track',
669
+      'data_normalized_url' => 'http://www.mixcloud.com/nevrakse_ISM/nevrakse-tranceplantation/',
670
+      'data_thumb_url' => 'http://images-mix.netdna-ssl.com/w/100/h/100/q/85/upload/images/profile/e0cf41b2-a34e-4638-abbe-4713ac17a9c8.jpeg',
671
+      'data_tags' => array(
672
+         0 => 'Trance',
673
+         1 => 'Psytrance'
674
+       )
675
+    ),$this->proceed_element_datas_api(
676
+      $bux, 
677
+      'http://www.mixcloud.com/nevrakse_ISM/nevrakse-tranceplantation/'
678
+    ));
679
+    
650 680
   }
651 681
   
652 682
 }

+ 7 - 0
src/Muzich/CoreBundle/Tests/ElementFactory/UrlAnalyzerTest.php View File

@@ -105,4 +105,11 @@ class UrlAnalyzerTest extends \PHPUnit_Framework_TestCase
105 105
     $this->assertEquals('QQ3L3mqP5JY', $url_analyzer->getRefId());
106 106
   }
107 107
   
108
+  public function testMixcloud()
109
+  {
110
+    $url_analyzer = new UrlAnalyzer($this->getNewElement('mixcloud.com', 'http://www.mixcloud.com/nevrakse_ISM/nevrakse-tranceplantation/'), UrlMatchs::$mixcloud);
111
+    $this->assertTrue($url_analyzer->haveMatch());
112
+    
113
+  }
114
+  
108 115
 }

+ 7 - 2
src/Muzich/CoreBundle/lib/UnitTest.php View File

@@ -37,7 +37,7 @@ class UnitTest extends \PHPUnit_Framework_TestCase
37 37
     return $this->_container->getParameter($param);
38 38
   }
39 39
   
40
-  protected function proceed_elementAndFill($user, $name, $url, $tag_ids, $final_embed)
40
+  protected function proceed_elementAndFill($user, $name, $url, $tag_ids, $final_embed = null, $embed_contained = null)
41 41
   {
42 42
     $element = new Element();
43 43
     $element->setName($name);
@@ -62,7 +62,12 @@ class UnitTest extends \PHPUnit_Framework_TestCase
62 62
     }
63 63
     
64 64
     $this->assertEquals($element_tag_ids, $tag_ids);
65
-    $this->assertEquals($element->getEmbed(), $final_embed);
65
+    
66
+    if ($final_embed)
67
+      $this->assertEquals($element->getEmbed(), $final_embed);
68
+    
69
+    if ($embed_contained)
70
+      $this->assertTrue((strpos($element->getEmbed(), $embed_contained) !== false));
66 71
   }
67 72
   
68 73
   protected function proceed_element_datas_api($user, $url)