[joj] Rewrite and add support for generic embeds (closes #13268)
authorSergey M․ <dstftw@gmail.com>
Sun, 9 Jul 2017 12:05:18 +0000 (19:05 +0700)
committerSergey M․ <dstftw@gmail.com>
Sun, 9 Jul 2017 12:17:54 +0000 (19:17 +0700)
youtube_dl/extractor/generic.py
youtube_dl/extractor/joj.py

index 919f4f987eb6f5af3963656329ca9fc1c540d9a8..f2c577f98c07f30f76d832d1bcaecaa5c0f0bc7a 100644 (file)
@@ -91,6 +91,7 @@ from .anvato import AnvatoIE
 from .washingtonpost import WashingtonPostIE
 from .wistia import WistiaIE
 from .mediaset import MediasetIE
+from .joj import JojIE
 
 
 class GenericIE(InfoExtractor):
@@ -1770,6 +1771,16 @@ class GenericIE(InfoExtractor):
             },
             'add_ie': [MediasetIE.ie_key()],
         },
+        {
+            # JOJ.sk embeds
+            'url': 'https://www.noviny.sk/slovensko/238543-slovenskom-sa-prehnala-vlna-silnych-burok',
+            'info_dict': {
+                'id': '238543-slovenskom-sa-prehnala-vlna-silnych-burok',
+                'title': 'Slovenskom sa prehnala vlna silných búrok',
+            },
+            'playlist_mincount': 5,
+            'add_ie': [JojIE.ie_key()],
+        },
         {
             # AMP embed (see https://www.ampproject.org/docs/reference/components/amp-video)
             'url': 'https://tvrain.ru/amp/418921/',
@@ -2722,6 +2733,12 @@ class GenericIE(InfoExtractor):
             return self.playlist_from_matches(
                 mediaset_urls, video_id, video_title, ie=MediasetIE.ie_key())
 
+        # Look for JOJ.sk embeds
+        joj_urls = JojIE._extract_urls(webpage)
+        if joj_urls:
+            return self.playlist_from_matches(
+                joj_urls, video_id, video_title, ie=JojIE.ie_key())
+
         def merge_dicts(dict1, dict2):
             merged = {}
             for k, v in dict1.items():
index 2ebfec902940f2df449ec24ec2129fafefc4ee37..a764023e9f82a07bc96f6eaab293daf224a1c91a 100755 (executable)
 # coding: utf-8\r
 from __future__ import unicode_literals\r
 \r
-from .common import InfoExtractor\r
 import re\r
 \r
+from .common import InfoExtractor\r
+from ..compat import compat_str\r
+from ..utils import (\r
+    int_or_none,\r
+    js_to_json,\r
+    try_get,\r
+)\r
+\r
 \r
 class JojIE(InfoExtractor):\r
-    _VALID_URL = r'https?://[a-z0-9]+\.joj\.sk/([^/]+/)*(?P<title_query>(?P<release_date>[0-9]{4}(-[0-9]{2}){2}).*)' # noqa\r
+    _VALID_URL = r'''(?x)\r
+                    (?:\r
+                        joj:|\r
+                        https?://media\.joj\.sk/embed/\r
+                    )\r
+                    (?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})\r
+                '''\r
     _TESTS = [{\r
-        'url': 'https://www.joj.sk/nove-byvanie/archiv/2017-05-28-nove-byvanie', # noqa\r
+        'url': 'https://media.joj.sk/embed/a388ec4c-6019-4a4a-9312-b1bee194e932',\r
         'info_dict': {\r
             'id': 'a388ec4c-6019-4a4a-9312-b1bee194e932',\r
             'ext': 'mp4',\r
-            'title': 'Nové Bývanie',\r
-            'release_date': '20170528'\r
+            'title': 'NOVÉ BÝVANIE',\r
+            'thumbnail': r're:^https?://.*\.jpg$',\r
+            'duration': 3118,\r
         }\r
     }, {\r
-        'url': 'http://nasi.joj.sk/epizody/2016-09-06-stari-rodicia',\r
-        'info_dict': {\r
-            'id': 'f18b2c5f-9ea8-4941-a164-a814c53306ad',\r
-            'ext': 'mp4',\r
-            'title': 'Starí Rodičia',\r
-            'release_date': '20160906'\r
-        }\r
+        'url': 'joj:a388ec4c-6019-4a4a-9312-b1bee194e932',\r
+        'only_matching': True,\r
     }]\r
 \r
-    media_src_url = 'http://n16.joj.sk/storage/'\r
-    xml_source_url = 'https://media.joj.sk/services/Video.php?clip='\r
+    @staticmethod\r
+    def _extract_urls(webpage):\r
+        return re.findall(\r
+            r'<iframe\b[^>]+\bsrc=["\'](?P<url>(?:https?:)?//media\.joj\.sk/embed/[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})',\r
+            webpage)\r
 \r
     def _real_extract(self, url):\r
-        mobj = re.match(self._VALID_URL, url)\r
-        release_date = mobj.group('release_date').replace('-', '')\r
-        webpage = self._download_webpage(url, 'id')\r
-        video_id = self._html_search_regex(\r
-            r'https?://([a-z0-9]+\.)joj\.sk/embed/(?P<video_id>[a-f0-9\-]+)',\r
-            webpage, 'id', group='video_id')\r
-        xml_playlist_url = self.xml_source_url + video_id\r
-        xml_playlist_et = self._download_xml(xml_playlist_url, 'XML playlist')\r
+        video_id = self._match_id(url)\r
+\r
+        webpage = self._download_webpage(\r
+            'https://media.joj.sk/embed/%s' % video_id, video_id)\r
+\r
+        title = self._search_regex(\r
+            (r'videoTitle\s*:\s*(["\'])(?P<title>(?:(?!\1).)+)\1',\r
+             r'<title>(?P<title>[^<]+)'), webpage, 'title',\r
+            default=None, group='title') or self._og_search_title(webpage)\r
+\r
+        bitrates = self._parse_json(\r
+            self._search_regex(\r
+                r'(?s)bitrates\s*=\s*({.+?});', webpage, 'bitrates',\r
+                default='{}'),\r
+            video_id, transform_source=js_to_json, fatal=False)\r
+\r
         formats = []\r
-        for file_el in xml_playlist_et.findall('files/file'):\r
-            try:\r
-                height = int(file_el.attrib['id'].replace('p', ''))\r
-            except ValueError:\r
-                height = 0\r
-            formats.append({'height': height,\r
-                            'url': self.media_src_url + file_el.attrib['path'].replace(  # noqa\r
-                                'dat/', '', 1)})\r
+        for format_url in try_get(bitrates, lambda x: x['mp4'], list) or []:\r
+            if isinstance(format_url, compat_str):\r
+                height = self._search_regex(\r
+                    r'(\d+)[pP]\.', format_url, 'height', default=None)\r
+                formats.append({\r
+                    'url': format_url,\r
+                    'format_id': '%sp' % height if height else None,\r
+                    'height': int(height),\r
+                })\r
+        if not formats:\r
+            playlist = self._download_xml(\r
+                'https://media.joj.sk/services/Video.php?clip=%s' % video_id,\r
+                video_id)\r
+            for file_el in playlist.findall('./files/file'):\r
+                path = file_el.get('path')\r
+                if not path:\r
+                    continue\r
+                format_id = file_el.get('id') or file_el.get('label')\r
+                formats.append({\r
+                    'url': 'http://n16.joj.sk/storage/%s' % path.replace(\r
+                        'dat/', '', 1),\r
+                    'format_id': format_id,\r
+                    'height': int_or_none(self._search_regex(\r
+                        r'(\d+)[pP]', format_id or path, 'height',\r
+                        default=None)),\r
+                })\r
         self._sort_formats(formats)\r
 \r
+        thumbnail = self._og_search_thumbnail(webpage)\r
+\r
+        duration = int_or_none(self._search_regex(\r
+            r'videoDuration\s*:\s*(\d+)', webpage, 'duration', fatal=False))\r
+\r
         return {\r
             'id': video_id,\r
-            'title': self._og_search_title(webpage).title(),\r
+            'title': title,\r
+            'thumbnail': thumbnail,\r
+            'duration': duration,\r
             'formats': formats,\r
-            'release_date': release_date\r
         }\r