]>
Commit | Line | Data |
---|---|---|
9787c5f4 | 1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
41c2c254 RA |
4 | import json |
5 | import uuid | |
96c186e1 | 6 | |
e33a7253 | 7 | from .adobepass import AdobePassIE |
6df196f3 | 8 | from ..compat import ( |
0d08bcdb | 9 | compat_HTTPError, |
6df196f3 RA |
10 | compat_str, |
11 | compat_urllib_parse_unquote, | |
12 | ) | |
e37b54b1 | 13 | from ..utils import ( |
0d08bcdb | 14 | ExtractorError, |
bf6ec2fe S |
15 | int_or_none, |
16 | parse_age_limit, | |
17 | parse_duration, | |
18 | try_get, | |
19 | unified_timestamp, | |
e37b54b1 | 20 | ) |
9787c5f4 | 21 | |
22 | ||
e33a7253 | 23 | class FOXIE(AdobePassIE): |
6df196f3 | 24 | _VALID_URL = r'https?://(?:www\.)?fox\.com/watch/(?P<id>[\da-fA-F]+)' |
bf6ec2fe S |
25 | _TESTS = [{ |
26 | # clip | |
27 | 'url': 'https://www.fox.com/watch/4b765a60490325103ea69888fb2bd4e8/', | |
5e3a6fec | 28 | 'md5': 'ebd296fcc41dd4b19f8115d8461a3165', |
9787c5f4 | 29 | 'info_dict': { |
bf6ec2fe | 30 | 'id': '4b765a60490325103ea69888fb2bd4e8', |
9787c5f4 | 31 | 'ext': 'mp4', |
bf6ec2fe S |
32 | 'title': 'Aftermath: Bruce Wayne Develops Into The Dark Knight', |
33 | 'description': 'md5:549cd9c70d413adb32ce2a779b53b486', | |
34 | 'duration': 102, | |
35 | 'timestamp': 1504291893, | |
36 | 'upload_date': '20170901', | |
37 | 'creator': 'FOX', | |
38 | 'series': 'Gotham', | |
6df196f3 | 39 | 'age_limit': 14, |
9787c5f4 | 40 | }, |
bf6ec2fe S |
41 | 'params': { |
42 | 'skip_download': True, | |
43 | }, | |
44 | }, { | |
45 | # episode, geo-restricted | |
46 | 'url': 'https://www.fox.com/watch/087036ca7f33c8eb79b08152b4dd75c1/', | |
47 | 'only_matching': True, | |
48 | }, { | |
49 | # episode, geo-restricted, tv provided required | |
50 | 'url': 'https://www.fox.com/watch/30056b295fb57f7452aeeb4920bc3024/', | |
51 | 'only_matching': True, | |
52 | }] | |
0d08bcdb | 53 | _GEO_BYPASS = False |
6df196f3 RA |
54 | _HOME_PAGE_URL = 'https://www.fox.com/' |
55 | _API_KEY = 'abdcbed02c124d393b39e818a4312055' | |
41c2c254 | 56 | _access_token = None |
96c186e1 | 57 | |
41c2c254 RA |
58 | def _call_api(self, path, video_id, data=None): |
59 | headers = { | |
6df196f3 | 60 | 'X-Api-Key': self._API_KEY, |
41c2c254 RA |
61 | } |
62 | if self._access_token: | |
63 | headers['Authorization'] = 'Bearer ' + self._access_token | |
0d08bcdb RA |
64 | try: |
65 | return self._download_json( | |
66 | 'https://api2.fox.com/v2.0/' + path, | |
67 | video_id, data=data, headers=headers) | |
68 | except ExtractorError as e: | |
62d10f0d | 69 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403: |
0d08bcdb RA |
70 | entitlement_issues = self._parse_json( |
71 | e.cause.read().decode(), video_id)['entitlementIssues'] | |
72 | for e in entitlement_issues: | |
73 | if e.get('errorCode') == 1005: | |
74 | raise ExtractorError( | |
75 | 'This video is only available via cable service provider ' | |
76 | 'subscription. You may want to use --cookies.', expected=True) | |
77 | messages = ', '.join([e['message'] for e in entitlement_issues]) | |
78 | raise ExtractorError(messages, expected=True) | |
79 | raise | |
96c186e1 | 80 | |
41c2c254 | 81 | def _real_initialize(self): |
6df196f3 RA |
82 | if not self._access_token: |
83 | mvpd_auth = self._get_cookies(self._HOME_PAGE_URL).get('mvpd-auth') | |
84 | if mvpd_auth: | |
85 | self._access_token = (self._parse_json(compat_urllib_parse_unquote( | |
86 | mvpd_auth.value), None, fatal=False) or {}).get('accessToken') | |
87 | if not self._access_token: | |
88 | self._access_token = self._call_api( | |
89 | 'login', None, json.dumps({ | |
90 | 'deviceId': compat_str(uuid.uuid4()), | |
91 | }).encode())['accessToken'] | |
9787c5f4 | 92 | |
93 | def _real_extract(self, url): | |
94 | video_id = self._match_id(url) | |
7aa0ee32 | 95 | |
41c2c254 | 96 | video = self._call_api('vodplayer/' + video_id, video_id) |
bf6ec2fe S |
97 | |
98 | title = video['name'] | |
41c2c254 | 99 | release_url = video['url'] |
0d08bcdb RA |
100 | try: |
101 | m3u8_url = self._download_json(release_url, video_id)['playURL'] | |
102 | except ExtractorError as e: | |
e0dde1d8 | 103 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403: |
0d08bcdb RA |
104 | error = self._parse_json(e.cause.read().decode(), video_id) |
105 | if error.get('exception') == 'GeoLocationBlocked': | |
106 | self.raise_geo_restricted(countries=['US']) | |
107 | raise ExtractorError(error['description'], expected=True) | |
108 | raise | |
96c186e1 RA |
109 | formats = self._extract_m3u8_formats( |
110 | m3u8_url, video_id, 'mp4', | |
111 | entry_protocol='m3u8_native', m3u8_id='hls') | |
112 | self._sort_formats(formats) | |
113 | ||
6df196f3 RA |
114 | data = try_get( |
115 | video, lambda x: x['trackingData']['properties'], dict) or {} | |
116 | ||
96c186e1 RA |
117 | duration = int_or_none(video.get('durationInSeconds')) or int_or_none( |
118 | video.get('duration')) or parse_duration(video.get('duration')) | |
119 | timestamp = unified_timestamp(video.get('datePublished')) | |
120 | creator = data.get('brand') or data.get('network') or video.get('network') | |
121 | series = video.get('seriesName') or data.get( | |
122 | 'seriesName') or data.get('show') | |
684ae102 RA |
123 | |
124 | subtitles = {} | |
125 | for doc_rel in video.get('documentReleases', []): | |
126 | rel_url = doc_rel.get('url') | |
127 | if not url or doc_rel.get('format') != 'SCC': | |
128 | continue | |
129 | subtitles['en'] = [{ | |
130 | 'url': rel_url, | |
131 | 'ext': 'scc', | |
132 | }] | |
133 | break | |
bf6ec2fe | 134 | |
96c186e1 | 135 | return { |
bf6ec2fe S |
136 | 'id': video_id, |
137 | 'title': title, | |
96c186e1 RA |
138 | 'formats': formats, |
139 | 'description': video.get('description'), | |
bf6ec2fe S |
140 | 'duration': duration, |
141 | 'timestamp': timestamp, | |
6df196f3 | 142 | 'age_limit': parse_age_limit(video.get('contentRating')), |
bf6ec2fe S |
143 | 'creator': creator, |
144 | 'series': series, | |
96c186e1 RA |
145 | 'season_number': int_or_none(video.get('seasonNumber')), |
146 | 'episode': video.get('name'), | |
147 | 'episode_number': int_or_none(video.get('episodeNumber')), | |
148 | 'release_year': int_or_none(video.get('releaseYear')), | |
684ae102 | 149 | 'subtitles': subtitles, |
bf6ec2fe | 150 | } |