[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / democracynow.py
index 973bb437b1f149a6ec53d4997ca6b78e048abff5..5c9c0ecdc0e20c1dbf9871edb65c43a24c714396 100644 (file)
@@ -2,21 +2,27 @@
 from __future__ import unicode_literals
 
 import re
+import os.path
+
 from .common import InfoExtractor
+from ..compat import compat_urlparse
+from ..utils import (
+    url_basename,
+    remove_start,
+)
 
 
 class DemocracynowIE(InfoExtractor):
-    _VALID_URL = r'https?://(?:www\.)?democracynow.org/?(?P<id>[^\?]*)'
+    _VALID_URL = r'https?://(?:www\.)?democracynow\.org/(?P<id>[^\?]*)'
     IE_NAME = 'democracynow'
     _TESTS = [{
         'url': 'http://www.democracynow.org/shows/2015/7/3',
+        'md5': '3757c182d3d84da68f5c8f506c18c196',
         'info_dict': {
             'id': '2015-0703-001',
             'ext': 'mp4',
-            'title': 'July 03, 2015 - Democracy Now!',
-            'description': 'A daily independent global news hour with Amy Goodman & Juan Gonz\xe1lez "What to the Slave is 4th of July?": James Earl Jones Reads Frederick Douglass\u2019 Historic Speech : "This Flag Comes Down Today": Bree Newsome Scales SC Capitol Flagpole, Takes Down Confederate Flag : "We Shall Overcome": Remembering Folk Icon, Activist Pete Seeger in His Own Words & Songs',
-            'uploader': 'Democracy Now',
-            'upload_date': None,
+            'title': 'Daily Show for July 03, 2015',
+            'description': 'md5:80eb927244d6749900de6072c7cc2c86',
         },
     }, {
         'url': 'http://www.democracynow.org/2015/7/3/this_flag_comes_down_today_bree',
@@ -25,61 +31,66 @@ class DemocracynowIE(InfoExtractor):
             'ext': 'mp4',
             'title': '"This Flag Comes Down Today": Bree Newsome Scales SC Capitol Flagpole, Takes Down Confederate Flag',
             'description': 'md5:4d2bc4f0d29f5553c2210a4bc7761a21',
-            'uploader': 'Democracy Now',
-            'upload_date': None,
+        },
+        'params': {
+            'skip_download': True,
         },
     }]
 
     def _real_extract(self, url):
         display_id = self._match_id(url)
-        base_host = re.search(r'^(.+?://[^/]+)', url).group(1)
-        if display_id == '':
-            display_id = 'home'
+
         webpage = self._download_webpage(url, display_id)
-        re_desc = re.search(r'<meta property=.og:description. content=(["\'])(.+?)\1', webpage, re.DOTALL)
-        description = re_desc.group(2) if re_desc else ''
 
-        jstr = self._search_regex(r'({.+?"related_video_xml".+?})', webpage, 'json', default=None)
-        js = self._parse_json(jstr, display_id)
-        video_id = None
+        json_data = self._parse_json(self._search_regex(
+            r'<script[^>]+type="text/json"[^>]*>\s*({[^>]+})', webpage, 'json'),
+            display_id)
+
+        title = json_data['title']
         formats = []
-        subtitles = {}
-        for key in ('caption_file', '.......'):
-            # ....... = pending vtt support that doesn't clobber srt 'chapter_file':
-            url = js.get(key, '')
-            if url == '' or url is None:
-                continue
-            if not re.match(r'^https?://', url):
-                url = base_host + url
-            ext = re.search(r'\.([^\.]+)$', url).group(1)
-            subtitles['eng'] = [{
-                'ext': ext,
-                'url': url,
-            }]
-        for key in ('file', 'audio'):
-            url = js.get(key, '')
-            if url == '' or url is None:
+
+        video_id = None
+
+        for key in ('file', 'audio', 'video', 'high_res_video'):
+            media_url = json_data.get(key, '')
+            if not media_url:
                 continue
-            if not re.match(r'^https?://', url):
-                url = base_host + url
-            purl = re.search(r'/(?P<dir>[^/]+)/(?:dn)?(?P<fn>[^/]+?)\.(?P<ext>[^\.\?]+)(?P<hasparams>\?|$)', url)
-            if video_id is None:
-                video_id = purl.group('fn')
-            if js.get('start') is not None:
-                url += '&' if purl.group('hasparams') == '?' else '?'
-                url = url + 'start=' + str(js.get('start'))
+            media_url = re.sub(r'\?.*', '', compat_urlparse.urljoin(url, media_url))
+            video_id = video_id or remove_start(os.path.splitext(url_basename(media_url))[0], 'dn')
             formats.append({
-                'format_id': purl.group('dir'),
-                'ext': purl.group('ext'),
-                'url': url,
+                'url': media_url,
+                'vcodec': 'none' if key == 'audio' else None,
             })
+
         self._sort_formats(formats)
-        ret = {
-            'id': video_id,
-            'title': js.get('title'),
+
+        default_lang = 'en'
+        subtitles = {}
+
+        def add_subtitle_item(lang, info_dict):
+            if lang not in subtitles:
+                subtitles[lang] = []
+            subtitles[lang].append(info_dict)
+
+        # chapter_file are not subtitles
+        if 'caption_file' in json_data:
+            add_subtitle_item(default_lang, {
+                'url': compat_urlparse.urljoin(url, json_data['caption_file']),
+            })
+
+        for subtitle_item in json_data.get('captions', []):
+            lang = subtitle_item.get('language', '').lower() or default_lang
+            add_subtitle_item(lang, {
+                'url': compat_urlparse.urljoin(url, subtitle_item['url']),
+            })
+
+        description = self._og_search_description(webpage, default=None)
+
+        return {
+            'id': video_id or display_id,
+            'title': title,
             'description': description,
-            'uploader': 'Democracy Now',
+            'thumbnail': json_data.get('image'),
             'subtitles': subtitles,
             'formats': formats,
         }
-        return ret