[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / pornovoisines.py
index efbb6a8187a9bf4f1c9c24a74df6c8a6cb4ed2bd..b6b71069d31070644afd46e2d92a939e4b33f744 100644 (file)
 from __future__ import unicode_literals
 
 import re
-import datetime
-import random
 
-from ..compat import compat_urllib_parse
 from .common import InfoExtractor
+from ..utils import (
+    int_or_none,
+    float_or_none,
+    unified_strdate,
+)
 
-class PornoVoisinesIE(InfoExtractor):
-    _VALID_URL = r'^((?:http://)?(?:www\.)?pornovoisines.com)/showvideo/(\d+)/([^/]+)'
-
-    VIDEO_URL_TEMPLATE = 'http://stream%d.pornovoisines.com' \
-        '/static/media/video/transcoded/%s-640x360-1000-trscded.mp4'
 
-    SERVER_NUMBERS = (1, 2)
+class PornoVoisinesIE(InfoExtractor):
+    _VALID_URL = r'https?://(?:www\.)?pornovoisines\.com/videos/show/(?P<id>\d+)/(?P<display_id>[^/.]+)'
 
     _TEST = {
-        'url': 'http://www.pornovoisines.com/showvideo/1285/recherche-appartement/',
-        'md5': '5ac670803bc12e9e7f9f662ce64cf1d1',
+        'url': 'http://www.pornovoisines.com/videos/show/919/recherche-appartement.html',
+        'md5': '6f8aca6a058592ab49fe701c8ba8317b',
         'info_dict': {
-            'id': '1285',
+            'id': '919',
             'display_id': 'recherche-appartement',
             'ext': 'mp4',
-            'title': "Recherche appartement",
+            'title': 'Recherche appartement',
+            'description': 'md5:fe10cb92ae2dd3ed94bb4080d11ff493',
+            'thumbnail': r're:^https?://.*\.jpg$',
             'upload_date': '20140925',
-            'view_count': int,
             'duration': 120,
-            'categories': ["Débutante", "Scénario", "Sodomie"],
-            'description': 're:^Pour la .+ original...$',
-            'thumbnail': 're:^http://',
-            'uploader': "JMTV",
+            'view_count': int,
             'average_rating': float,
-            'comment_count': int,
+            'categories': ['Débutante', 'Débutantes', 'Scénario', 'Sodomie'],
             'age_limit': 18,
+            'subtitles': {
+                'fr': [{
+                    'ext': 'vtt',
+                }]
+            },
         }
     }
 
-    @classmethod
-    def build_video_url(cls, id):
-        server_nr = random.choice(cls.SERVER_NUMBERS)
-        return cls.VIDEO_URL_TEMPLATE % (server_nr, id)
+    def _real_extract(self, url):
+        mobj = re.match(self._VALID_URL, url)
+        video_id = mobj.group('id')
+        display_id = mobj.group('display_id')
 
-    @staticmethod
-    def parse_upload_date(str):
-        return datetime.datetime.strptime(str, "%d-%m-%Y").strftime("%Y%m%d")
+        settings_url = self._download_json(
+            'http://www.pornovoisines.com/api/video/%s/getsettingsurl/' % video_id,
+            video_id, note='Getting settings URL')['video_settings_url']
+        settings = self._download_json(settings_url, video_id)['data']
 
-    @staticmethod
-    def parse_categories(str):
-        return map(lambda s: s.strip(), str.split(','))
+        formats = []
+        for kind, data in settings['variants'].items():
+            if kind == 'HLS':
+                formats.extend(self._extract_m3u8_formats(
+                    data, video_id, ext='mp4', entry_protocol='m3u8_native', m3u8_id='hls'))
+            elif kind == 'MP4':
+                for item in data:
+                    formats.append({
+                        'url': item['url'],
+                        'height': item.get('height'),
+                        'bitrate': item.get('bitrate'),
+                    })
+        self._sort_formats(formats)
 
-    def _real_extract(self, url):
-        mobj = re.match(self._VALID_URL, url)
-        url_prefix = mobj.group(1)
-        id = mobj.group(2)
-        display_id = mobj.group(3)
+        webpage = self._download_webpage(url, video_id)
+
+        title = self._og_search_title(webpage)
+        description = self._og_search_description(webpage)
 
-        webpage = self._download_webpage(url, id)
+        # The webpage has a bug - there's no space between "thumb" and src=
+        thumbnail = self._html_search_regex(
+            r'<img[^>]+class=([\'"])thumb\1[^>]*src=([\'"])(?P<url>[^"]+)\2',
+            webpage, 'thumbnail', fatal=False, group='url')
 
-        title = self._html_search_regex(r'<h1>(.+?)</h1>', webpage, 'title',
-            flags=re.DOTALL)
-        url = self.build_video_url(id)
-        upload_date = self.parse_upload_date(
-            self._search_regex(r'Publié le (\d\d-\d\d-\d{4})', webpage,
-            'upload date'))
-        view_count = int(self._search_regex(r'(\d+) vues', webpage, 'view count'))
-        duration = int(self._search_regex('Durée (\d+)', webpage, 'duration'))
-        categories = self.parse_categories(self._html_search_regex(
-            r'<li class="categorie">(.+?)</li>', webpage, "categories",
-            flags=re.DOTALL))
-        description = self._html_search_regex(
-            r'<article id="descriptif">(.+?)</article>', webpage, "description",
-            flags=re.DOTALL)
-        thumbnail = url_prefix + self._html_search_regex(re.compile(
-            '<div id="mediaspace' + id + '">.*?<img src="(.+?)"', re.DOTALL),
-            webpage, "thumbnail")
-        uploader = re.sub(r' *\| *$', '',
-            self._html_search_regex(r'<li class="auteur">(.+?)</li>', webpage,
-            "uploader", flags=re.DOTALL))
-        average_rating = float(self._search_regex(r'Note : (\d+,\d+)',
-            webpage, "average rating").replace(',', '.'))
-        comment_count = int(self._search_regex(r'\((\d+)\)', webpage,
-            "comment count"))
+        upload_date = unified_strdate(self._search_regex(
+            r'Le\s*<b>([\d/]+)', webpage, 'upload date', fatal=False))
+        duration = settings.get('main', {}).get('duration')
+        view_count = int_or_none(self._search_regex(
+            r'(\d+) vues', webpage, 'view count', fatal=False))
+        average_rating = self._search_regex(
+            r'Note\s*:\s*(\d+(?:,\d+)?)', webpage, 'average rating', fatal=False)
+        if average_rating:
+            average_rating = float_or_none(average_rating.replace(',', '.'))
+
+        categories = self._html_search_regex(
+            r'(?s)Catégories\s*:\s*<b>(.+?)</b>', webpage, 'categories', fatal=False)
+        if categories:
+            categories = [category.strip() for category in categories.split(',')]
+
+        subtitles = {'fr': [{
+            'url': subtitle,
+        } for subtitle in settings.get('main', {}).get('vtt_tracks', {}).values()]}
 
         return {
-            'id': id,
+            'id': video_id,
             'display_id': display_id,
-            'url': url,
+            'formats': formats,
             'title': title,
-            'upload_date': upload_date,
-            'view_count': view_count,
-            'duration': duration,
-            'categories': categories,
             'description': description,
             'thumbnail': thumbnail,
-            'uploader': uploader,
+            'upload_date': upload_date,
+            'duration': duration,
+            'view_count': view_count,
             'average_rating': average_rating,
-            'comment_count': comment_count,
+            'categories': categories,
             'age_limit': 18,
+            'subtitles': subtitles,
         }