Browse Source

Refactorisation de la factory dailymotion + ajout de récupération title et tags

Sevajol Bastien 11 years ago
parent
commit
37b085a0ab

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

7
 use Doctrine\ORM\EntityManager;
7
 use Doctrine\ORM\EntityManager;
8
 use Muzich\CoreBundle\lib\Api\Connector as ApiConnector;
8
 use Muzich\CoreBundle\lib\Api\Connector as ApiConnector;
9
 use Muzich\CoreBundle\lib\Element\UrlAnalyzer;
9
 use Muzich\CoreBundle\lib\Element\UrlAnalyzer;
10
+use Muzich\CoreBundle\Util\TagLike;
10
 
11
 
11
 /**
12
 /**
12
  *
13
  *
113
     return $this->url_analyzer;
114
     return $this->url_analyzer;
114
   }
115
   }
115
   
116
   
117
+  protected function setDataTagsForElement($tags_string, $merge = array())
118
+  {
119
+    $tags_like = array();
120
+    if (strlen(trim($tags_string)))
121
+    {
122
+      $tag_like = new TagLike($this->entity_manager);
123
+      foreach (explode(' ', $tags_string) as $word)
124
+      {
125
+        $similar_tags = $tag_like->getSimilarTags($word, ($this->element->getOwner())?$this->element->getOwner()->getId():null);
126
+        if (count($similar_tags))
127
+        {
128
+          if ($similar_tags['same_found'])
129
+          {
130
+            $tags_like[] = $similar_tags['tags'][0]['name'];
131
+          }
132
+        }
133
+      }
134
+    }
135
+    
136
+    $tags_like = array_merge($tags_like, $merge);
137
+    if (count($tags_like))
138
+    {
139
+      $this->element->setData(Element::DATA_TAGS, array_unique($tags_like));
140
+    }
141
+  }
142
+  
116
 }
143
 }
117
 
144
 
118
 ?>
145
 ?>

+ 27 - 28
src/Muzich/CoreBundle/Factory/Elements/Dailymotioncom.php View File

4
 
4
 
5
 use Muzich\CoreBundle\Factory\ElementFactory;
5
 use Muzich\CoreBundle\Factory\ElementFactory;
6
 use Muzich\CoreBundle\Entity\Element;
6
 use Muzich\CoreBundle\Entity\Element;
7
+use Symfony\Component\DependencyInjection\Container;
8
+use Doctrine\ORM\EntityManager;
9
+use Muzich\CoreBundle\Factory\UrlMatchs;
7
 
10
 
8
 /**
11
 /**
9
  * 
12
  * 
13
 class Dailymotioncom extends ElementFactory
16
 class Dailymotioncom extends ElementFactory
14
 {
17
 {
15
   
18
   
19
+  public function __construct(Element $element, Container $container, EntityManager $entity_manager)
20
+  {
21
+    $this->url_matchs = UrlMatchs::$dailymotion;
22
+    parent::__construct($element, $container, $entity_manager);
23
+  }
24
+  
25
+  public function proceedDatas()
26
+  {
27
+    $this->setElementDatasWithApi();
28
+    $this->proceedEmbedCode();
29
+    $this->proceedThumbnailUrl();
30
+  }
31
+  
16
   /**
32
   /**
17
    * URL_API: http://www.dailymotion.com/doc/api/obj-video.html
33
    * URL_API: http://www.dailymotion.com/doc/api/obj-video.html
18
-   * URL_TYPE: /video/xnqcwx_le-nazisme-dans-le-couple_fun#hp-v-v2 (c'est quoi cette url ^^ ?) 
34
+   * URL_TYPE: /video/xnqcwx_le-nazisme-dans-le-couple_fun#hp-v-v2 
19
    */
35
    */
20
-  public function retrieveDatas()
36
+  public function setElementDatasWithApi()
21
   {
37
   {
22
-    $url_clean = $this->getCleanedUrl();
23
-    $ref_id = null;
38
+    $response = $this->getApiConnector()->getResponseForUrl('https://api.dailymotion.com/video/'
39
+      .$this->url_analyzer->getRefId().'&fields=thumbnail_medium_url,title,tags');
24
     
40
     
25
-    // Récupération de l'id
26
-    if (preg_match("#\/(video\/)([a-zA-Z0-9]+)([a-zA-Z0-9_-]*)#", $url_clean, $preg_result))
27
-    {
28
-      $ref_id = $preg_result[2];
29
-      $this->element->setData(Element::DATA_REF_ID, $ref_id);
30
-    }
41
+    $this->getApiConnector()->setElementDatasWithResponse($response, array(
42
+      Element::DATA_THUMB_URL      => 'thumbnail_medium_url',
43
+      Element::DATA_TITLE          => 'title',
44
+    ));
31
     
45
     
32
-    // Récupération de données auprés de l'API
33
-    if ($ref_id)
46
+    if ($response->have('tags'))
34
     {
47
     {
35
-      $api_url = curl_init('https://api.dailymotion.com/video/'.$ref_id
36
-        .'&fields=thumbnail_medium_url');
37
-      
38
-      $options = array(
39
-        CURLOPT_RETURNTRANSFER => true,
40
-        CURLOPT_HTTPHEADER => array('Content-type: application/json')
41
-      );
42
-      
43
-      curl_setopt_array($api_url, $options);
44
-      $api_result = json_decode(curl_exec($api_url));
45
-            
46
-      // On récupère l'url du thumbnail
47
-      if (isset($api_result->thumbnail_medium_url))
48
-      {
49
-        $this->element->setData(Element::DATA_THUMB_URL, $api_result->thumbnail_medium_url);
50
-      }
48
+      $this->setDataTagsForElement(implode(' ', $response->get('tags')));
51
     }
49
     }
50
+    
52
   }
51
   }
53
   
52
   
54
   public function proceedEmbedCode()
53
   public function proceedEmbedCode()

+ 1 - 30
src/Muzich/CoreBundle/Factory/Elements/Jamendocom.php View File

28
       if (count($response->getContent()))
28
       if (count($response->getContent()))
29
       {
29
       {
30
         $data_return = array();
30
         $data_return = array();
31
-        foreach ($result as $song)
31
+        foreach ($response->getContent() as $song)
32
         {
32
         {
33
           $data_return[] = array(
33
           $data_return[] = array(
34
             'name' => $song['name'],
34
             'name' => $song['name'],
44
   public function proceedDatas()
44
   public function proceedDatas()
45
   {
45
   {
46
     $this->retrieveDatas();
46
     $this->retrieveDatas();
47
-    // TODO: A t-on toujours besoin du code embed ou on génrère le player générique a la volée ?
48
-    $this->proceedEmbedCode();
49
     $this->proceedThumbnailUrl();
47
     $this->proceedThumbnailUrl();
50
   }
48
   }
51
   
49
   
105
     $this->element->setData(Element::DATA_DOWNLOAD, true);
103
     $this->element->setData(Element::DATA_DOWNLOAD, true);
106
   }
104
   }
107
   
105
   
108
-  public function proceedEmbedCode()
109
-  {
110
-    if (($ref_id = $this->element->getData(Element::DATA_REF_ID)) 
111
-      && ($type = $this->element->getData(Element::DATA_TYPE)))
112
-    {
113
-      $height = $this->container->getParameter('jamendo_player_height');
114
-      $width = $this->container->getParameter('jamendo_player_width');
115
-      $embed_url = "http://widgets.jamendo.com/fr/$type/?".$type."_id=$ref_id&playertype=2008";
116
-      $this->element->setEmbed(
117
-        '<object width="'.$width.'" height="'.$height.'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"'
118
-            .' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" align="middle">
119
-            <param name="allowScriptAccess" value="always" />
120
-            <param name="wmode" value="transparent" />
121
-            <param name="movie" value="'.$embed_url.'" />
122
-            <param name="quality" value="high" />
123
-            <param name="bgcolor" value="#FFFFFF" />
124
-            <embed src="'.$embed_url.'" quality="high" wmode="transparent" bgcolor="#FFFFFF"'
125
-            .' width="'.$width.'" height="'.$height.'" align="middle" allowScriptAccess="always"'
126
-            .' type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
127
-              &nbsp;
128
-            </embed>
129
-            &nbsp;
130
-          </object>'  
131
-      );
132
-    }
133
-  }
134
-  
135
   public function proceedThumbnailUrl()
106
   public function proceedThumbnailUrl()
136
   {
107
   {
137
     if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))
108
     if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))

+ 1 - 47
src/Muzich/CoreBundle/Factory/Elements/Soundcloudcom.php View File

4
 
4
 
5
 use Muzich\CoreBundle\Factory\ElementFactory;
5
 use Muzich\CoreBundle\Factory\ElementFactory;
6
 use Muzich\CoreBundle\Entity\Element;
6
 use Muzich\CoreBundle\Entity\Element;
7
-use Muzich\CoreBundle\Util\TagLike;
8
 use Muzich\CoreBundle\lib\Api\Response as ApiResponse;
7
 use Muzich\CoreBundle\lib\Api\Response as ApiResponse;
9
 use Muzich\CoreBundle\Factory\UrlMatchs;
8
 use Muzich\CoreBundle\Factory\UrlMatchs;
10
 use Symfony\Component\DependencyInjection\Container;
9
 use Symfony\Component\DependencyInjection\Container;
22
   public function proceedDatas()
21
   public function proceedDatas()
23
   {
22
   {
24
     $this->setElementDatasWithApi();
23
     $this->setElementDatasWithApi();
25
-    // TODO: Embed code ne devrais plus être necessaire (on créer les lecteurs avec JS)
26
-    $this->proceedEmbedCode();
27
     $this->proceedThumbnailUrl();
24
     $this->proceedThumbnailUrl();
28
   }
25
   }
29
   
26
   
86
   protected function setTagsData(ApiResponse $response)
83
   protected function setTagsData(ApiResponse $response)
87
   {
84
   {
88
     $tags_string = $response->get('genre').' '.$response->get('tag_list').' '.str_replace(' ', '-', $response->get('genre'));
85
     $tags_string = $response->get('genre').' '.$response->get('tag_list').' '.str_replace(' ', '-', $response->get('genre'));
89
-    $tags_like = array();
90
-    if (strlen($tags_string))
91
-    {
92
-      $tag_like = new TagLike($this->entity_manager);
93
-      foreach (explode(' ', $tags_string) as $word)
94
-      {
95
-        $similar_tags = $tag_like->getSimilarTags($word, ($this->element->getOwner())?$this->element->getOwner()->getId():null);
96
-        if (count($similar_tags))
97
-        {
98
-          if ($similar_tags['same_found'])
99
-          {
100
-            $tags_like[] = $similar_tags['tags'][0]['name'];
101
-          }
102
-        }
103
-      }
104
-      $tags_like[] = $response->get('genre');
105
-      if (count($tags_like))
106
-      {
107
-        $this->element->setData(Element::DATA_TAGS, array_unique($tags_like));
108
-      }
109
-    }
86
+    $this->setDataTagsForElement($tags_string, array($response->get('genre')));
110
   }
87
   }
111
   
88
   
112
   protected function setElementEmbeddableData($response)
89
   protected function setElementEmbeddableData($response)
116
     ));
93
     ));
117
   }
94
   }
118
   
95
   
119
-  public function proceedEmbedCode()
120
-  {
121
-    if (($ref_id = $this->element->getData(Element::DATA_REF_ID)) 
122
-      && ($this->element->getData(Element::DATA_TYPE) == 'track' || $this->element->getData(Element::DATA_TYPE) == 'playlist' ))
123
-    {
124
-      $ref_id = $this->element->getUrl();
125
-      $embed_id = md5($ref_id);
126
-      $height = $this->container->getParameter('soundcloud_player_height');
127
-      $this->element->setEmbed(
128
-        '<object height="'.$height.'" width="100%" id="embed_'.$embed_id.'" '
129
-          .'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
130
-          <param name="movie" value="http://player.soundcloud.com/player.swf?url='.$ref_id.'&amp;'
131
-          .'enable_api=true&amp;object_id=embed_'.$embed_id.'"></param>
132
-          <param name="allowscriptaccess" value="always"></param>
133
-          <embed allowscriptaccess="always" height="'.$height.'" '
134
-          .'src="http://player.soundcloud.com/player.swf?url='.$ref_id.'&amp;enable_api=true'
135
-          .'&amp;object_id=embed_'.$embed_id.'" type="application/x-shockwave-flash" '
136
-          .'width="100%" name="embed_'.$embed_id.'"></embed>
137
-        </object>'
138
-      );
139
-    }
140
-  }
141
-  
142
   public function proceedThumbnailUrl()
96
   public function proceedThumbnailUrl()
143
   {
97
   {
144
     if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))
98
     if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))

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

37
       "#^\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+#" => null
37
       "#^\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+#" => null
38
     )
38
     )
39
   );
39
   );
40
+  
41
+  public static $dailymotion = array(
42
+    Element::TYPE_OTHER => array(
43
+      // http://dailymotion.com/video/xnqcwx_le-nazisme-dans-le-couple_fun#hp-v-v2
44
+      "#\/(video\/)([a-zA-Z0-9]+)([a-zA-Z0-9_-]*)#" => 2
45
+    )
46
+  );
40
 }
47
 }

+ 33 - 15
src/Muzich/CoreBundle/Tests/ElementFactory/ElementFactoryTest.php View File

329
 //    ));
329
 //    ));
330
 //    
330
 //    
331
 //    
331
 //    
332
-//    /*
333
-//     *   - dailymotion.com
334
-//     */
335
-//    
336
-//      // 'http://www.dailymotion.com/video/xafj1q_black-bomb-a-tales-from-the-old-sch_music', 
337
-//    
338
-//      // TODO: l'url est pas toujours la même pour le thumb :/
339
-////    $this->assertEquals(array(
340
-////      'data_ref_id' => 'xafj1q',
341
-////      'data_thumb_url'  => 'http://static2.dmcdn.net/static/video/686/025/17520686:jpeg_preview_medium.jpg?20110820212502'
342
-////    ),$this->proceed_element_datas_api(
343
-////      $bux, 
344
-////      'http://www.dailymotion.com/video/xafj1q_black-bomb-a-tales-from-the-old-sch_music'
345
-////    ));
346
-//    
332
+    ///*
333
+    // *   - dailymotion.com
334
+    // */
335
+    //
336
+    //  // 'http://www.dailymotion.com/video/xafj1q_black-bomb-a-tales-from-the-old-sch_music'
337
+    //$this->assertEquals(array(
338
+    //  'data_ref_id' => 'xafj1q',
339
+    //  'data_thumb_url'  => 'http://s1.dmcdn.net/kcSK/160x120-si6.jpg',
340
+    //  'data_type' => 'other',
341
+    //  'data_title' => 'Black Bomb A - Tales From The Old School',
342
+    //  'data_tags' => array(
343
+    //    0 => 'Metal'
344
+    //  )
345
+    //),$this->proceed_element_datas_api(
346
+    //  $bux, 
347
+    //  'http://www.dailymotion.com/video/xafj1q_black-bomb-a-tales-from-the-old-sch_music'
348
+    //));
349
+    //
350
+    //// http://www.dailymotion.com/video/x4om5b_punish-yourself-gimme-cocaine-live_music?search_algo=2
351
+    //$this->assertEquals(array(
352
+    //  'data_ref_id' => 'x4om5b',
353
+    //  'data_thumb_url'  => 'http://s1.dmcdn.net/sRiY/160x120-BYy.jpg',
354
+    //  'data_type' => 'other',
355
+    //  'data_title' => 'Punish yourself - gimme cocaine (live à nancy, azimut854)',
356
+    //  'data_tags' => array(
357
+    //    0 => 'Metal',
358
+    //    1 => 'Electro'
359
+    //  )
360
+    //),$this->proceed_element_datas_api(
361
+    //  $bux, 
362
+    //  'http://www.dailymotion.com/video/x4om5b_punish-yourself-gimme-cocaine-live_music?search_algo=2'
363
+    //));
364
+    //    
347
     ///*
365
     ///*
348
     // * - soundcloud.com
366
     // * - soundcloud.com
349
     // */
367
     // */