]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/democracynow.py
[democracynow] Rename js to json_data
[yt-dlp.git] / youtube_dl / extractor / democracynow.py
CommitLineData
f8705443 1# coding: utf-8
2from __future__ import unicode_literals
3
f8705443 4import re
dde9fe97
YCH
5import os.path
6
f8705443 7from .common import InfoExtractor
dde9fe97
YCH
8from ..compat import compat_urlparse
9from ..utils import (
10 url_basename,
11 remove_start,
12)
f8705443 13
14
15class DemocracynowIE(InfoExtractor):
dde9fe97 16 _VALID_URL = r'https?://(?:www\.)?democracynow.org/(?P<id>[^\?]*)'
f8705443 17 IE_NAME = 'democracynow'
18 _TESTS = [{
19 'url': 'http://www.democracynow.org/shows/2015/7/3',
fc68d52b 20 'md5': 'fbb8fe3d7a56a5e12431ce2f9b2fab0d',
f8705443 21 'info_dict': {
22 'id': '2015-0703-001',
23 'ext': 'mp4',
24 'title': 'July 03, 2015 - Democracy Now!',
dde9fe97 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',
f8705443 26 },
eb080813 27 }, {
f8705443 28 'url': 'http://www.democracynow.org/2015/7/3/this_flag_comes_down_today_bree',
fc68d52b 29 'md5': 'fbb8fe3d7a56a5e12431ce2f9b2fab0d',
f8705443 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',
f8705443 35 },
f8705443 36 }]
37
38 def _real_extract(self, url):
39 display_id = self._match_id(url)
f8705443 40 webpage = self._download_webpage(url, display_id)
78d7ee19 41 description = self._og_search_description(webpage)
f8705443 42
fd810282 43 json_data = self._parse_json(self._search_regex(
dde9fe97
YCH
44 r'<script[^>]+type="text/json"[^>]*>\s*({[^>]+})', webpage, 'json'),
45 display_id)
f8705443 46 video_id = None
47 formats = []
dde9fe97
YCH
48
49 default_lang = 'en'
50
f8705443 51 subtitles = {}
dde9fe97
YCH
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
fd810282 59 if 'caption_file' in json_data:
dde9fe97 60 add_subtitle_item(default_lang, {
fd810282 61 'url': compat_urlparse.urljoin(url, json_data['caption_file']),
dde9fe97
YCH
62 })
63
fd810282 64 for subtitle_item in json_data.get('captions', []):
dde9fe97
YCH
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
78d7ee19 70 for key in ('file', 'audio', 'video'):
fd810282 71 media_url = json_data.get(key, '')
dde9fe97 72 if not media_url:
f8705443 73 continue
dde9fe97
YCH
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')
f8705443 76 formats.append({
dde9fe97 77 'url': media_url,
f8705443 78 })
dde9fe97 79
f8705443 80 self._sort_formats(formats)
dde9fe97
YCH
81
82 return {
f8705443 83 'id': video_id,
fd810282 84 'title': json_data.get('title'),
f8705443 85 'description': description,
f8705443 86 'subtitles': subtitles,
87 'formats': formats,
88 }