| 
									
										
										
										
											2015-12-31 12:02:33 +01:00
										 |  |  | # coding: utf-8 | 
					
						
							|  |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-28 22:39:08 +01:00
										 |  |  | import json | 
					
						
							|  |  |  | import uuid | 
					
						
							| 
									
										
										
										
											2019-01-10 09:05:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 15:52:23 +01:00
										 |  |  | from .adobepass import AdobePassIE | 
					
						
							| 
									
										
										
										
											2019-01-29 00:31:49 +01:00
										 |  |  | from ..compat import ( | 
					
						
							| 
									
										
										
										
											2019-03-10 09:37:28 +01:00
										 |  |  |     compat_HTTPError, | 
					
						
							| 
									
										
										
										
											2019-01-29 00:31:49 +01:00
										 |  |  |     compat_str, | 
					
						
							|  |  |  |     compat_urllib_parse_unquote, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-08-06 20:53:13 +01:00
										 |  |  | from ..utils import ( | 
					
						
							| 
									
										
										
										
											2019-03-10 09:37:28 +01:00
										 |  |  |     ExtractorError, | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  |     int_or_none, | 
					
						
							|  |  |  |     parse_age_limit, | 
					
						
							|  |  |  |     parse_duration, | 
					
						
							|  |  |  |     try_get, | 
					
						
							|  |  |  |     unified_timestamp, | 
					
						
							| 
									
										
										
										
											2016-08-06 20:53:13 +01:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-12-31 12:02:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 15:52:23 +01:00
										 |  |  | class FOXIE(AdobePassIE): | 
					
						
							| 
									
										
										
										
											2019-01-29 00:31:49 +01:00
										 |  |  |     _VALID_URL = r'https?://(?:www\.)?fox\.com/watch/(?P<id>[\da-fA-F]+)' | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  |     _TESTS = [{ | 
					
						
							|  |  |  |         # clip | 
					
						
							|  |  |  |         'url': 'https://www.fox.com/watch/4b765a60490325103ea69888fb2bd4e8/', | 
					
						
							| 
									
										
										
										
											2016-02-09 17:30:42 +01:00
										 |  |  |         'md5': 'ebd296fcc41dd4b19f8115d8461a3165', | 
					
						
							| 
									
										
										
										
											2015-12-31 12:02:33 +01:00
										 |  |  |         'info_dict': { | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  |             'id': '4b765a60490325103ea69888fb2bd4e8', | 
					
						
							| 
									
										
										
										
											2015-12-31 12:02:33 +01:00
										 |  |  |             'ext': 'mp4', | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  |             'title': 'Aftermath: Bruce Wayne Develops Into The Dark Knight', | 
					
						
							|  |  |  |             'description': 'md5:549cd9c70d413adb32ce2a779b53b486', | 
					
						
							|  |  |  |             'duration': 102, | 
					
						
							|  |  |  |             'timestamp': 1504291893, | 
					
						
							|  |  |  |             'upload_date': '20170901', | 
					
						
							|  |  |  |             'creator': 'FOX', | 
					
						
							|  |  |  |             'series': 'Gotham', | 
					
						
							| 
									
										
										
										
											2019-01-29 00:31:49 +01:00
										 |  |  |             'age_limit': 14, | 
					
						
							| 
									
										
										
										
											2015-12-31 12:02:33 +01:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  |         'params': { | 
					
						
							|  |  |  |             'skip_download': True, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |         # episode, geo-restricted | 
					
						
							|  |  |  |         'url': 'https://www.fox.com/watch/087036ca7f33c8eb79b08152b4dd75c1/', | 
					
						
							|  |  |  |         'only_matching': True, | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |         # episode, geo-restricted, tv provided required | 
					
						
							|  |  |  |         'url': 'https://www.fox.com/watch/30056b295fb57f7452aeeb4920bc3024/', | 
					
						
							|  |  |  |         'only_matching': True, | 
					
						
							|  |  |  |     }] | 
					
						
							| 
									
										
										
										
											2019-03-10 09:37:28 +01:00
										 |  |  |     _GEO_BYPASS = False | 
					
						
							| 
									
										
										
										
											2019-01-29 00:31:49 +01:00
										 |  |  |     _HOME_PAGE_URL = 'https://www.fox.com/' | 
					
						
							|  |  |  |     _API_KEY = 'abdcbed02c124d393b39e818a4312055' | 
					
						
							| 
									
										
										
										
											2019-01-28 22:39:08 +01:00
										 |  |  |     _access_token = None | 
					
						
							| 
									
										
										
										
											2019-01-10 09:05:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-28 22:39:08 +01:00
										 |  |  |     def _call_api(self, path, video_id, data=None): | 
					
						
							|  |  |  |         headers = { | 
					
						
							| 
									
										
										
										
											2019-01-29 00:31:49 +01:00
										 |  |  |             'X-Api-Key': self._API_KEY, | 
					
						
							| 
									
										
										
										
											2019-01-28 22:39:08 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         if self._access_token: | 
					
						
							|  |  |  |             headers['Authorization'] = 'Bearer ' + self._access_token | 
					
						
							| 
									
										
										
										
											2019-03-10 09:37:28 +01:00
										 |  |  |         try: | 
					
						
							|  |  |  |             return self._download_json( | 
					
						
							|  |  |  |                 'https://api2.fox.com/v2.0/' + path, | 
					
						
							|  |  |  |                 video_id, data=data, headers=headers) | 
					
						
							|  |  |  |         except ExtractorError as e: | 
					
						
							|  |  |  |             if isinstance(e.cause, compat_HTTPError) and e.cause.status == 403: | 
					
						
							|  |  |  |                 entitlement_issues = self._parse_json( | 
					
						
							|  |  |  |                     e.cause.read().decode(), video_id)['entitlementIssues'] | 
					
						
							|  |  |  |                 for e in entitlement_issues: | 
					
						
							|  |  |  |                     if e.get('errorCode') == 1005: | 
					
						
							|  |  |  |                         raise ExtractorError( | 
					
						
							|  |  |  |                             'This video is only available via cable service provider ' | 
					
						
							|  |  |  |                             'subscription. You may want to use --cookies.', expected=True) | 
					
						
							|  |  |  |                 messages = ', '.join([e['message'] for e in entitlement_issues]) | 
					
						
							|  |  |  |                 raise ExtractorError(messages, expected=True) | 
					
						
							|  |  |  |             raise | 
					
						
							| 
									
										
										
										
											2019-01-10 09:05:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-28 22:39:08 +01:00
										 |  |  |     def _real_initialize(self): | 
					
						
							| 
									
										
										
										
											2019-01-29 00:31:49 +01:00
										 |  |  |         if not self._access_token: | 
					
						
							|  |  |  |             mvpd_auth = self._get_cookies(self._HOME_PAGE_URL).get('mvpd-auth') | 
					
						
							|  |  |  |             if mvpd_auth: | 
					
						
							|  |  |  |                 self._access_token = (self._parse_json(compat_urllib_parse_unquote( | 
					
						
							|  |  |  |                     mvpd_auth.value), None, fatal=False) or {}).get('accessToken') | 
					
						
							|  |  |  |             if not self._access_token: | 
					
						
							|  |  |  |                 self._access_token = self._call_api( | 
					
						
							|  |  |  |                     'login', None, json.dumps({ | 
					
						
							|  |  |  |                         'deviceId': compat_str(uuid.uuid4()), | 
					
						
							|  |  |  |                     }).encode())['accessToken'] | 
					
						
							| 
									
										
										
										
											2015-12-31 12:02:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         video_id = self._match_id(url) | 
					
						
							| 
									
										
										
										
											2017-03-25 08:12:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-28 22:39:08 +01:00
										 |  |  |         video = self._call_api('vodplayer/' + video_id, video_id) | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         title = video['name'] | 
					
						
							| 
									
										
										
										
											2019-01-28 22:39:08 +01:00
										 |  |  |         release_url = video['url'] | 
					
						
							| 
									
										
										
										
											2019-03-10 09:37:28 +01:00
										 |  |  |         try: | 
					
						
							|  |  |  |             m3u8_url = self._download_json(release_url, video_id)['playURL'] | 
					
						
							|  |  |  |         except ExtractorError as e: | 
					
						
							|  |  |  |             if isinstance(e.cause, compat_HTTPError) and e.cause.status == 403: | 
					
						
							|  |  |  |                 error = self._parse_json(e.cause.read().decode(), video_id) | 
					
						
							|  |  |  |                 if error.get('exception') == 'GeoLocationBlocked': | 
					
						
							|  |  |  |                     self.raise_geo_restricted(countries=['US']) | 
					
						
							|  |  |  |                 raise ExtractorError(error['description'], expected=True) | 
					
						
							|  |  |  |             raise | 
					
						
							| 
									
										
										
										
											2019-01-10 09:05:00 +01:00
										 |  |  |         formats = self._extract_m3u8_formats( | 
					
						
							|  |  |  |             m3u8_url, video_id, 'mp4', | 
					
						
							|  |  |  |             entry_protocol='m3u8_native', m3u8_id='hls') | 
					
						
							|  |  |  |         self._sort_formats(formats) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 00:31:49 +01:00
										 |  |  |         data = try_get( | 
					
						
							|  |  |  |             video, lambda x: x['trackingData']['properties'], dict) or {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-10 09:05:00 +01:00
										 |  |  |         duration = int_or_none(video.get('durationInSeconds')) or int_or_none( | 
					
						
							|  |  |  |             video.get('duration')) or parse_duration(video.get('duration')) | 
					
						
							|  |  |  |         timestamp = unified_timestamp(video.get('datePublished')) | 
					
						
							|  |  |  |         creator = data.get('brand') or data.get('network') or video.get('network') | 
					
						
							|  |  |  |         series = video.get('seriesName') or data.get( | 
					
						
							|  |  |  |             'seriesName') or data.get('show') | 
					
						
							| 
									
										
										
										
											2017-12-06 22:56:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         subtitles = {} | 
					
						
							|  |  |  |         for doc_rel in video.get('documentReleases', []): | 
					
						
							|  |  |  |             rel_url = doc_rel.get('url') | 
					
						
							|  |  |  |             if not url or doc_rel.get('format') != 'SCC': | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             subtitles['en'] = [{ | 
					
						
							|  |  |  |                 'url': rel_url, | 
					
						
							|  |  |  |                 'ext': 'scc', | 
					
						
							|  |  |  |             }] | 
					
						
							|  |  |  |             break | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-10 09:05:00 +01:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  |             'id': video_id, | 
					
						
							|  |  |  |             'title': title, | 
					
						
							| 
									
										
										
										
											2019-01-10 09:05:00 +01:00
										 |  |  |             'formats': formats, | 
					
						
							|  |  |  |             'description': video.get('description'), | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  |             'duration': duration, | 
					
						
							|  |  |  |             'timestamp': timestamp, | 
					
						
							| 
									
										
										
										
											2019-01-29 00:31:49 +01:00
										 |  |  |             'age_limit': parse_age_limit(video.get('contentRating')), | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  |             'creator': creator, | 
					
						
							|  |  |  |             'series': series, | 
					
						
							| 
									
										
										
										
											2019-01-10 09:05:00 +01:00
										 |  |  |             'season_number': int_or_none(video.get('seasonNumber')), | 
					
						
							|  |  |  |             'episode': video.get('name'), | 
					
						
							|  |  |  |             'episode_number': int_or_none(video.get('episodeNumber')), | 
					
						
							|  |  |  |             'release_year': int_or_none(video.get('releaseYear')), | 
					
						
							| 
									
										
										
										
											2017-12-06 22:56:14 +01:00
										 |  |  |             'subtitles': subtitles, | 
					
						
							| 
									
										
										
										
											2017-09-10 22:08:32 +07:00
										 |  |  |         } |