]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/reuters.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / reuters.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 js_to_json,
6 int_or_none,
7 unescapeHTML,
8 )
9
10
11 class ReutersIE(InfoExtractor):
12 _VALID_URL = r'https?://(?:www\.)?reuters\.com/.*?\?.*?videoId=(?P<id>[0-9]+)'
13 _TEST = {
14 'url': 'http://www.reuters.com/video/2016/05/20/san-francisco-police-chief-resigns?videoId=368575562',
15 'md5': '8015113643a0b12838f160b0b81cc2ee',
16 'info_dict': {
17 'id': '368575562',
18 'ext': 'mp4',
19 'title': 'San Francisco police chief resigns',
20 }
21 }
22
23 def _real_extract(self, url):
24 video_id = self._match_id(url)
25 webpage = self._download_webpage(
26 'http://www.reuters.com/assets/iframe/yovideo?videoId=%s' % video_id, video_id)
27 video_data = js_to_json(self._search_regex(
28 r'(?s)Reuters\.yovideo\.drawPlayer\(({.*?})\);',
29 webpage, 'video data'))
30
31 def get_json_value(key, fatal=False):
32 return self._search_regex(r'"%s"\s*:\s*"([^"]+)"' % key, video_data, key, fatal=fatal)
33
34 title = unescapeHTML(get_json_value('title', fatal=True))
35 mmid, fid = re.search(r',/(\d+)\?f=(\d+)', get_json_value('flv', fatal=True)).groups()
36
37 mas_data = self._download_json(
38 'http://mas-e.cds1.yospace.com/mas/%s/%s?trans=json' % (mmid, fid),
39 video_id, transform_source=js_to_json)
40 formats = []
41 for f in mas_data:
42 f_url = f.get('url')
43 if not f_url:
44 continue
45 method = f.get('method')
46 if method == 'hls':
47 formats.extend(self._extract_m3u8_formats(
48 f_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
49 else:
50 container = f.get('container')
51 ext = '3gp' if method == 'mobile' else container
52 formats.append({
53 'format_id': ext,
54 'url': f_url,
55 'ext': ext,
56 'container': container if method != 'mobile' else None,
57 })
58
59 return {
60 'id': video_id,
61 'title': title,
62 'thumbnail': get_json_value('thumb'),
63 'duration': int_or_none(get_json_value('seconds')),
64 'formats': formats,
65 }