1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-11-09 12:42:28 +01:00
yt-dlp/yt_dlp/extractor/democracynow.py

97 lines
3.0 KiB
Python
Raw Normal View History

# coding: utf-8
from __future__ import unicode_literals
import re
2015-11-03 11:36:54 +01:00
import os.path
from .common import InfoExtractor
2015-11-03 11:36:54 +01:00
from ..compat import compat_urlparse
from ..utils import (
url_basename,
remove_start,
)
class DemocracynowIE(InfoExtractor):
2016-09-08 13:29:05 +02:00
_VALID_URL = r'https?://(?:www\.)?democracynow\.org/(?P<id>[^\?]*)'
IE_NAME = 'democracynow'
_TESTS = [{
'url': 'http://www.democracynow.org/shows/2015/7/3',
2016-04-08 18:19:16 +02:00
'md5': '3757c182d3d84da68f5c8f506c18c196',
'info_dict': {
'id': '2015-0703-001',
'ext': 'mp4',
2017-05-01 20:25:09 +02:00
'title': 'Daily Show for July 03, 2015',
'description': 'md5:80eb927244d6749900de6072c7cc2c86',
},
2015-07-17 09:57:08 +02:00
}, {
'url': 'http://www.democracynow.org/2015/7/3/this_flag_comes_down_today_bree',
'info_dict': {
'id': '2015-0703-001',
'ext': 'mp4',
'title': '"This Flag Comes Down Today": Bree Newsome Scales SC Capitol Flagpole, Takes Down Confederate Flag',
'description': 'md5:4d2bc4f0d29f5553c2210a4bc7761a21',
},
2016-04-08 18:19:16 +02:00
'params': {
'skip_download': True,
},
}]
def _real_extract(self, url):
display_id = self._match_id(url)
2016-04-08 18:27:27 +02:00
webpage = self._download_webpage(url, display_id)
2015-11-03 17:09:55 +01:00
json_data = self._parse_json(self._search_regex(
2015-11-03 11:36:54 +01:00
r'<script[^>]+type="text/json"[^>]*>\s*({[^>]+})', webpage, 'json'),
display_id)
2016-04-08 18:27:27 +02:00
title = json_data['title']
formats = []
2015-11-03 11:36:54 +01:00
2016-04-08 18:27:27 +02:00
video_id = None
for key in ('file', 'audio', 'video', 'high_res_video'):
media_url = json_data.get(key, '')
if not media_url:
continue
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({
'url': media_url,
'vcodec': 'none' if key == 'audio' else None,
})
2015-11-03 11:36:54 +01:00
2016-04-08 18:27:27 +02:00
self._sort_formats(formats)
default_lang = 'en'
subtitles = {}
2015-11-03 11:36:54 +01:00
def add_subtitle_item(lang, info_dict):
if lang not in subtitles:
subtitles[lang] = []
subtitles[lang].append(info_dict)
# chapter_file are not subtitles
2015-11-03 17:09:55 +01:00
if 'caption_file' in json_data:
2015-11-03 11:36:54 +01:00
add_subtitle_item(default_lang, {
2015-11-03 17:09:55 +01:00
'url': compat_urlparse.urljoin(url, json_data['caption_file']),
2015-11-03 11:36:54 +01:00
})
2015-11-03 17:09:55 +01:00
for subtitle_item in json_data.get('captions', []):
2015-11-03 11:36:54 +01:00
lang = subtitle_item.get('language', '').lower() or default_lang
add_subtitle_item(lang, {
'url': compat_urlparse.urljoin(url, subtitle_item['url']),
})
2016-04-08 18:27:27 +02:00
description = self._og_search_description(webpage, default=None)
2015-11-03 11:36:54 +01:00
return {
'id': video_id or display_id,
2016-04-08 18:27:27 +02:00
'title': title,
'description': description,
2016-04-08 18:27:27 +02:00
'thumbnail': json_data.get('image'),
'subtitles': subtitles,
'formats': formats,
}