Make FoxBusiness work.
[youtube-dl] / youtube_dl / extractor / foxnews.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     parse_iso8601,
8     int_or_none,
9 )
10
11
12 class FoxNewsIE(InfoExtractor):
13     _VALID_URL = r'https?://video\.fox(?:news|business)\.com/v/(?:video-embed\.html\?video_id=)?(?P<id>\d+)'
14     _TESTS = [
15         {
16             'url': 'http://video.foxnews.com/v/3937480/frozen-in-time/#sp=show-clips',
17             'md5': '32aaded6ba3ef0d1c04e238d01031e5e',
18             'info_dict': {
19                 'id': '3937480',
20                 'ext': 'flv',
21                 'title': 'Frozen in Time',
22                 'description': 'Doctors baffled by 16-year-old girl that is the size of a toddler',
23                 'duration': 265,
24                 'timestamp': 1304411491,
25                 'upload_date': '20110503',
26                 'thumbnail': 're:^https?://.*\.jpg$',
27             },
28         },
29         {
30             'url': 'http://video.foxnews.com/v/3922535568001/rep-luis-gutierrez-on-if-obamas-immigration-plan-is-legal/#sp=show-clips',
31             'md5': '5846c64a1ea05ec78175421b8323e2df',
32             'info_dict': {
33                 'id': '3922535568001',
34                 'ext': 'mp4',
35                 'title': "Rep. Luis Gutierrez on if Obama's immigration plan is legal",
36                 'description': "Congressman discusses the president's executive action",
37                 'duration': 292,
38                 'timestamp': 1417662047,
39                 'upload_date': '20141204',
40                 'thumbnail': 're:^https?://.*\.jpg$',
41             },
42         },
43         {
44             'url': 'http://video.foxnews.com/v/video-embed.html?video_id=3937480&d=video.foxnews.com',
45             'only_matching': True,
46         },
47     ]
48
49     def _real_extract(self, url):
50         video_id = self._match_id(url)
51
52         m = re.match(r'^https?://video\.fox(news|business)', url)
53
54         video = self._download_json(
55             'http://video.fox' + m.group(1) + '.com/v/feed/video/%s.js?template=fox' % video_id, video_id)
56
57         item = video['channel']['item']
58         title = item['title']
59         description = item['description']
60         timestamp = parse_iso8601(item['dc-date'])
61
62         media_group = item['media-group']
63         duration = None
64         formats = []
65         for media in media_group['media-content']:
66             attributes = media['@attributes']
67             video_url = attributes['url']
68             if video_url.endswith('.f4m'):
69                 formats.extend(self._extract_f4m_formats(video_url + '?hdcore=3.4.0&plugin=aasp-3.4.0.132.124', video_id))
70             elif video_url.endswith('.m3u8'):
71                 formats.extend(self._extract_m3u8_formats(video_url, video_id, 'flv'))
72             elif not video_url.endswith('.smil'):
73                 duration = int_or_none(attributes.get('duration'))
74                 formats.append({
75                     'url': video_url,
76                     'format_id': media['media-category']['@attributes']['label'],
77                     'preference': 1,
78                     'vbr': int_or_none(attributes.get('bitrate')),
79                     'filesize': int_or_none(attributes.get('fileSize'))
80                 })
81         self._sort_formats(formats)
82
83         media_thumbnail = media_group['media-thumbnail']['@attributes']
84         thumbnails = [{
85             'url': media_thumbnail['url'],
86             'width': int_or_none(media_thumbnail.get('width')),
87             'height': int_or_none(media_thumbnail.get('height')),
88         }] if media_thumbnail else []
89
90         return {
91             'id': video_id,
92             'title': title,
93             'description': description,
94             'duration': duration,
95             'timestamp': timestamp,
96             'formats': formats,
97             'thumbnails': thumbnails,
98         }