Merge remote-tracking branch 'upstream/master' into bliptv
[youtube-dl] / youtube_dl / extractor / democracynow.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import os.path
6
7 from .common import InfoExtractor
8 from ..compat import compat_urlparse
9 from ..utils import (
10     url_basename,
11     remove_start,
12 )
13
14
15 class DemocracynowIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?democracynow.org/(?P<id>[^\?]*)'
17     IE_NAME = 'democracynow'
18     _TESTS = [{
19         'url': 'http://www.democracynow.org/shows/2015/7/3',
20         'md5': 'fbb8fe3d7a56a5e12431ce2f9b2fab0d',
21         'info_dict': {
22             'id': '2015-0703-001',
23             'ext': 'mp4',
24             'title': 'July 03, 2015 - Democracy Now!',
25             'description': 'A daily independent global news hour with Amy Goodman & Juan González "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',
26         },
27     }, {
28         'url': 'http://www.democracynow.org/2015/7/3/this_flag_comes_down_today_bree',
29         'md5': 'fbb8fe3d7a56a5e12431ce2f9b2fab0d',
30         'info_dict': {
31             'id': '2015-0703-001',
32             'ext': 'mp4',
33             'title': '"This Flag Comes Down Today": Bree Newsome Scales SC Capitol Flagpole, Takes Down Confederate Flag',
34             'description': 'md5:4d2bc4f0d29f5553c2210a4bc7761a21',
35         },
36     }]
37
38     def _real_extract(self, url):
39         display_id = self._match_id(url)
40         webpage = self._download_webpage(url, display_id)
41         description = self._og_search_description(webpage)
42
43         json_data = self._parse_json(self._search_regex(
44             r'<script[^>]+type="text/json"[^>]*>\s*({[^>]+})', webpage, 'json'),
45             display_id)
46         video_id = None
47         formats = []
48
49         default_lang = 'en'
50
51         subtitles = {}
52
53         def add_subtitle_item(lang, info_dict):
54             if lang not in subtitles:
55                 subtitles[lang] = []
56             subtitles[lang].append(info_dict)
57
58         # chapter_file are not subtitles
59         if 'caption_file' in json_data:
60             add_subtitle_item(default_lang, {
61                 'url': compat_urlparse.urljoin(url, json_data['caption_file']),
62             })
63
64         for subtitle_item in json_data.get('captions', []):
65             lang = subtitle_item.get('language', '').lower() or default_lang
66             add_subtitle_item(lang, {
67                 'url': compat_urlparse.urljoin(url, subtitle_item['url']),
68             })
69
70         for key in ('file', 'audio', 'video'):
71             media_url = json_data.get(key, '')
72             if not media_url:
73                 continue
74             media_url = re.sub(r'\?.*', '', compat_urlparse.urljoin(url, media_url))
75             video_id = video_id or remove_start(os.path.splitext(url_basename(media_url))[0], 'dn')
76             formats.append({
77                 'url': media_url,
78             })
79
80         self._sort_formats(formats)
81
82         return {
83             'id': video_id or display_id,
84             'title': json_data['title'],
85             'description': description,
86             'subtitles': subtitles,
87             'formats': formats,
88         }