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