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