You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.6 KiB

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