Merge branch 'pr-democracynow' of https://github.com/atomicdryad/youtube-dl into...
[youtube-dl] / youtube_dl / extractor / democracynow.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 from .common import InfoExtractor
6
7
8 class DemocracynowIE(InfoExtractor):
9     _VALID_URL = r'https?://(?:www\.)?democracynow.org/?(?P<id>[^\?]*)'
10     IE_NAME = 'democracynow'
11     _TESTS = [{
12         'url': 'http://www.democracynow.org/shows/2015/7/3',
13         'info_dict': {
14             'id': '2015-0703-001',
15             'ext': 'mp4',
16             'title': 'July 03, 2015 - Democracy Now!',
17             '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',
18             'uploader': 'Democracy Now',
19             'upload_date': None,
20         },
21     }, {
22         'url': 'http://www.democracynow.org/2015/7/3/this_flag_comes_down_today_bree',
23         'info_dict': {
24             'id': '2015-0703-001',
25             'ext': 'mp4',
26             'title': '"This Flag Comes Down Today": Bree Newsome Scales SC Capitol Flagpole, Takes Down Confederate Flag',
27             'description': 'md5:4d2bc4f0d29f5553c2210a4bc7761a21',
28             'uploader': 'Democracy Now',
29             'upload_date': None,
30         },
31     }]
32
33     def _real_extract(self, url):
34         display_id = self._match_id(url)
35         base_host = re.search(r'^(.+?://[^/]+)', url).group(1)
36         if display_id == '':
37             display_id = 'home'
38         webpage = self._download_webpage(url, display_id)
39         re_desc = re.search(r'<meta property=.og:description. content=(["\'])(.+?)\1', webpage, re.DOTALL)
40         description = re_desc.group(2) if re_desc else ''
41
42         jstr = self._search_regex(r'({.+?"related_video_xml".+?})', webpage, 'json', default=None)
43         js = self._parse_json(jstr, display_id)
44         video_id = None
45         formats = []
46         subtitles = {}
47         for key in ('caption_file', '.......'):
48             # ....... = pending vtt support that doesn't clobber srt 'chapter_file':
49             url = js.get(key, '')
50             if url == '' or url is None:
51                 continue
52             if not re.match(r'^https?://', url):
53                 url = base_host + url
54             ext = re.search(r'\.([^\.]+)$', url).group(1)
55             subtitles['eng'] = [{
56                 'ext': ext,
57                 'url': url,
58             }]
59         for key in ('file', 'audio'):
60             url = js.get(key, '')
61             if url == '' or url is None:
62                 continue
63             if not re.match(r'^https?://', url):
64                 url = base_host + url
65             purl = re.search(r'/(?P<dir>[^/]+)/(?:dn)?(?P<fn>[^/]+?)\.(?P<ext>[^\.\?]+)(?P<hasparams>\?|$)', url)
66             if video_id is None:
67                 video_id = purl.group('fn')
68             if js.get('start') is not None:
69                 url += '&' if purl.group('hasparams') == '?' else '?'
70                 url = url + 'start=' + str(js.get('start'))
71             formats.append({
72                 'format_id': purl.group('dir'),
73                 'ext': purl.group('ext'),
74                 'url': url,
75             })
76         self._sort_formats(formats)
77         ret = {
78             'id': video_id,
79             'title': js.get('title'),
80             'description': description,
81             'uploader': 'Democracy Now',
82             'subtitles': subtitles,
83             'formats': formats,
84         }
85         return ret