[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / fusion.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5     determine_ext,
6     int_or_none,
7     mimetype2ext,
8     parse_iso8601,
9 )
10
11
12 class FusionIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?fusion\.(?:net|tv)/(?:video/|show/.+?\bvideo=)(?P<id>\d+)'
14     _TESTS = [{
15         'url': 'http://fusion.tv/video/201781/u-s-and-panamanian-forces-work-together-to-stop-a-vessel-smuggling-drugs/',
16         'info_dict': {
17             'id': '3145868',
18             'ext': 'mp4',
19             'title': 'U.S. and Panamanian forces work together to stop a vessel smuggling drugs',
20             'description': 'md5:0cc84a9943c064c0f46b128b41b1b0d7',
21             'duration': 140.0,
22             'timestamp': 1442589635,
23             'uploader': 'UNIVISON',
24             'upload_date': '20150918',
25         },
26         'params': {
27             'skip_download': True,
28         },
29         'add_ie': ['Anvato'],
30     }, {
31         'url': 'http://fusion.tv/video/201781',
32         'only_matching': True,
33     }, {
34         'url': 'https://fusion.tv/show/food-exposed-with-nelufar-hedayat/?ancla=full-episodes&video=588644',
35         'only_matching': True,
36     }]
37
38     def _real_extract(self, url):
39         video_id = self._match_id(url)
40         video = self._download_json(
41             'https://platform.fusion.net/wp-json/fusiondotnet/v1/video/' + video_id, video_id)
42
43         info = {
44             'id': video_id,
45             'title': video['title'],
46             'description': video.get('excerpt'),
47             'timestamp': parse_iso8601(video.get('published')),
48             'series': video.get('show'),
49         }
50
51         formats = []
52         src = video.get('src') or {}
53         for f_id, f in src.items():
54             for q_id, q in f.items():
55                 q_url = q.get('url')
56                 if not q_url:
57                     continue
58                 ext = determine_ext(q_url, mimetype2ext(q.get('type')))
59                 if ext == 'smil':
60                     formats.extend(self._extract_smil_formats(q_url, video_id, fatal=False))
61                 elif f_id == 'm3u8-variant' or (ext == 'm3u8' and q_id == 'Variant'):
62                     formats.extend(self._extract_m3u8_formats(
63                         q_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
64                 else:
65                     formats.append({
66                         'format_id': '-'.join([f_id, q_id]),
67                         'url': q_url,
68                         'width': int_or_none(q.get('width')),
69                         'height': int_or_none(q.get('height')),
70                         'tbr': int_or_none(self._search_regex(r'_(\d+)\.m(?:p4|3u8)', q_url, 'bitrate')),
71                         'ext': 'mp4' if ext == 'm3u8' else ext,
72                         'protocol': 'm3u8_native' if ext == 'm3u8' else 'https',
73                     })
74         if formats:
75             self._sort_formats(formats)
76             info['formats'] = formats
77         else:
78             info.update({
79                 '_type': 'url',
80                 'url': 'anvato:uni:' + video['video_ids']['anvato'],
81                 'ie_key': 'Anvato',
82             })
83
84         return info