[youtube] fix hd720 format position
[youtube-dl] / youtube_dl / extractor / fox.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .adobepass import AdobePassIE
5 from .uplynk import UplynkPreplayIE
6 from ..compat import compat_str
7 from ..utils import (
8     HEADRequest,
9     int_or_none,
10     parse_age_limit,
11     parse_duration,
12     try_get,
13     unified_timestamp,
14     update_url_query,
15 )
16
17
18 class FOXIE(AdobePassIE):
19     _VALID_URL = r'https?://(?:www\.)?fox\.com/watch/(?P<id>[\da-fA-F]+)'
20     _TESTS = [{
21         # clip
22         'url': 'https://www.fox.com/watch/4b765a60490325103ea69888fb2bd4e8/',
23         'md5': 'ebd296fcc41dd4b19f8115d8461a3165',
24         'info_dict': {
25             'id': '4b765a60490325103ea69888fb2bd4e8',
26             'ext': 'mp4',
27             'title': 'Aftermath: Bruce Wayne Develops Into The Dark Knight',
28             'description': 'md5:549cd9c70d413adb32ce2a779b53b486',
29             'duration': 102,
30             'timestamp': 1504291893,
31             'upload_date': '20170901',
32             'creator': 'FOX',
33             'series': 'Gotham',
34         },
35         'params': {
36             'skip_download': True,
37         },
38     }, {
39         # episode, geo-restricted
40         'url': 'https://www.fox.com/watch/087036ca7f33c8eb79b08152b4dd75c1/',
41         'only_matching': True,
42     }, {
43         # episode, geo-restricted, tv provided required
44         'url': 'https://www.fox.com/watch/30056b295fb57f7452aeeb4920bc3024/',
45         'only_matching': True,
46     }]
47
48     def _real_extract(self, url):
49         video_id = self._match_id(url)
50
51         video = self._download_json(
52             'https://api.fox.com/fbc-content/v1_4/video/%s' % video_id,
53             video_id, headers={
54                 'apikey': 'abdcbed02c124d393b39e818a4312055',
55                 'Content-Type': 'application/json',
56                 'Referer': url,
57             })
58
59         title = video['name']
60         release_url = video['videoRelease']['url']
61
62         description = video.get('description')
63         duration = int_or_none(video.get('durationInSeconds')) or int_or_none(
64             video.get('duration')) or parse_duration(video.get('duration'))
65         timestamp = unified_timestamp(video.get('datePublished'))
66         rating = video.get('contentRating')
67         age_limit = parse_age_limit(rating)
68
69         data = try_get(
70             video, lambda x: x['trackingData']['properties'], dict) or {}
71
72         creator = data.get('brand') or data.get('network') or video.get('network')
73
74         series = video.get('seriesName') or data.get(
75             'seriesName') or data.get('show')
76         season_number = int_or_none(video.get('seasonNumber'))
77         episode = video.get('name')
78         episode_number = int_or_none(video.get('episodeNumber'))
79         release_year = int_or_none(video.get('releaseYear'))
80
81         if data.get('authRequired'):
82             resource = self._get_mvpd_resource(
83                 'fbc-fox', title, video.get('guid'), rating)
84             release_url = update_url_query(
85                 release_url, {
86                     'auth': self._extract_mvpd_auth(
87                         url, video_id, 'fbc-fox', resource)
88                 })
89
90         subtitles = {}
91         for doc_rel in video.get('documentReleases', []):
92             rel_url = doc_rel.get('url')
93             if not url or doc_rel.get('format') != 'SCC':
94                 continue
95             subtitles['en'] = [{
96                 'url': rel_url,
97                 'ext': 'scc',
98             }]
99             break
100
101         info = {
102             'id': video_id,
103             'title': title,
104             'description': description,
105             'duration': duration,
106             'timestamp': timestamp,
107             'age_limit': age_limit,
108             'creator': creator,
109             'series': series,
110             'season_number': season_number,
111             'episode': episode,
112             'episode_number': episode_number,
113             'release_year': release_year,
114             'subtitles': subtitles,
115         }
116
117         urlh = self._request_webpage(HEADRequest(release_url), video_id)
118         video_url = compat_str(urlh.geturl())
119
120         if UplynkPreplayIE.suitable(video_url):
121             info.update({
122                 '_type': 'url_transparent',
123                 'url': video_url,
124                 'ie_key': UplynkPreplayIE.ie_key(),
125             })
126         else:
127             m3u8_url = self._download_json(release_url, video_id)['playURL']
128             formats = self._extract_m3u8_formats(
129                 m3u8_url, video_id, 'mp4',
130                 entry_protocol='m3u8_native', m3u8_id='hls')
131             self._sort_formats(formats)
132             info['formats'] = formats
133         return info