]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/fox.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / fox.py
1 import json
2 import uuid
3
4 from .common import InfoExtractor
5 from ..compat import (
6 compat_HTTPError,
7 compat_str,
8 compat_urllib_parse_unquote,
9 )
10 from ..utils import (
11 ExtractorError,
12 int_or_none,
13 parse_age_limit,
14 parse_duration,
15 traverse_obj,
16 try_get,
17 unified_timestamp,
18 url_or_none,
19 )
20
21
22 class FOXIE(InfoExtractor):
23 _VALID_URL = r'https?://(?:www\.)?fox\.com/watch/(?P<id>[\da-fA-F]+)'
24 _TESTS = [{
25 # clip
26 'url': 'https://www.fox.com/watch/4b765a60490325103ea69888fb2bd4e8/',
27 'md5': 'ebd296fcc41dd4b19f8115d8461a3165',
28 'info_dict': {
29 'id': '4b765a60490325103ea69888fb2bd4e8',
30 'ext': 'mp4',
31 'title': 'Aftermath: Bruce Wayne Develops Into The Dark Knight',
32 'description': 'md5:549cd9c70d413adb32ce2a779b53b486',
33 'duration': 102,
34 'timestamp': 1504291893,
35 'upload_date': '20170901',
36 'creator': 'FOX',
37 'series': 'Gotham',
38 'age_limit': 14,
39 'episode': 'Aftermath: Bruce Wayne Develops Into The Dark Knight',
40 'thumbnail': r're:^https?://.*\.jpg$',
41 },
42 'params': {
43 'skip_download': True,
44 },
45 }, {
46 # episode, geo-restricted
47 'url': 'https://www.fox.com/watch/087036ca7f33c8eb79b08152b4dd75c1/',
48 'only_matching': True,
49 }, {
50 # sports event, geo-restricted
51 'url': 'https://www.fox.com/watch/b057484dade738d1f373b3e46216fa2c/',
52 'only_matching': True,
53 }]
54 _GEO_BYPASS = False
55 _HOME_PAGE_URL = 'https://www.fox.com/'
56 _API_KEY = '6E9S4bmcoNnZwVLOHywOv8PJEdu76cM9'
57 _access_token = None
58 _device_id = compat_str(uuid.uuid4())
59
60 def _call_api(self, path, video_id, data=None):
61 headers = {
62 'X-Api-Key': self._API_KEY,
63 }
64 if self._access_token:
65 headers['Authorization'] = 'Bearer ' + self._access_token
66 try:
67 return self._download_json(
68 'https://api3.fox.com/v2.0/' + path,
69 video_id, data=data, headers=headers)
70 except ExtractorError as e:
71 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
72 entitlement_issues = self._parse_json(
73 e.cause.read().decode(), video_id)['entitlementIssues']
74 for e in entitlement_issues:
75 if e.get('errorCode') == 1005:
76 raise ExtractorError(
77 'This video is only available via cable service provider '
78 'subscription. You may want to use --cookies.', expected=True)
79 messages = ', '.join([e['message'] for e in entitlement_issues])
80 raise ExtractorError(messages, expected=True)
81 raise
82
83 def _real_initialize(self):
84 if not self._access_token:
85 mvpd_auth = self._get_cookies(self._HOME_PAGE_URL).get('mvpd-auth')
86 if mvpd_auth:
87 self._access_token = (self._parse_json(compat_urllib_parse_unquote(
88 mvpd_auth.value), None, fatal=False) or {}).get('accessToken')
89 if not self._access_token:
90 self._access_token = self._call_api(
91 'login', None, json.dumps({
92 'deviceId': self._device_id,
93 }).encode())['accessToken']
94
95 def _real_extract(self, url):
96 video_id = self._match_id(url)
97
98 self._access_token = self._call_api(
99 'previewpassmvpd?device_id=%s&mvpd_id=TempPass_fbcfox_60min' % self._device_id,
100 video_id)['accessToken']
101
102 video = self._call_api('watch', video_id, data=json.dumps({
103 'capabilities': ['drm/widevine', 'fsdk/yo'],
104 'deviceWidth': 1280,
105 'deviceHeight': 720,
106 'maxRes': '720p',
107 'os': 'macos',
108 'osv': '',
109 'provider': {
110 'freewheel': {'did': self._device_id},
111 'vdms': {'rays': ''},
112 'dmp': {'kuid': '', 'seg': ''}
113 },
114 'playlist': '',
115 'privacy': {'us': '1---'},
116 'siteSection': '',
117 'streamType': 'vod',
118 'streamId': video_id}).encode('utf-8'))
119
120 title = video['name']
121 release_url = video['url']
122
123 try:
124 m3u8_url = self._download_json(release_url, video_id)['playURL']
125 except ExtractorError as e:
126 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
127 error = self._parse_json(e.cause.read().decode(), video_id)
128 if error.get('exception') == 'GeoLocationBlocked':
129 self.raise_geo_restricted(countries=['US'])
130 raise ExtractorError(error['description'], expected=True)
131 raise
132 formats = self._extract_m3u8_formats(
133 m3u8_url, video_id, 'mp4',
134 entry_protocol='m3u8_native', m3u8_id='hls')
135
136 data = try_get(
137 video, lambda x: x['trackingData']['properties'], dict) or {}
138
139 duration = int_or_none(video.get('durationInSeconds')) or int_or_none(
140 video.get('duration')) or parse_duration(video.get('duration'))
141 timestamp = unified_timestamp(video.get('datePublished'))
142 creator = data.get('brand') or data.get('network') or video.get('network')
143 series = video.get('seriesName') or data.get(
144 'seriesName') or data.get('show')
145
146 subtitles = {}
147 for doc_rel in video.get('documentReleases', []):
148 rel_url = doc_rel.get('url')
149 if not url or doc_rel.get('format') != 'SCC':
150 continue
151 subtitles['en'] = [{
152 'url': rel_url,
153 'ext': 'scc',
154 }]
155 break
156
157 return {
158 'id': video_id,
159 'title': title,
160 'formats': formats,
161 'description': video.get('description'),
162 'duration': duration,
163 'timestamp': timestamp,
164 'age_limit': parse_age_limit(video.get('contentRating')),
165 'creator': creator,
166 'series': series,
167 'season_number': int_or_none(video.get('seasonNumber')),
168 'episode': video.get('name'),
169 'episode_number': int_or_none(video.get('episodeNumber')),
170 'thumbnail': traverse_obj(video, ('images', 'still', 'raw'), expected_type=url_or_none),
171 'release_year': int_or_none(video.get('releaseYear')),
172 'subtitles': subtitles,
173 }