fox.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. _HOME_PAGE_URL = 'https://www.fox.com/'
  50. _API_KEY = 'abdcbed02c124d393b39e818a4312055'
  51. _access_token = None
  52. def _call_api(self, path, video_id, data=None):
  53. headers = {
  54. 'X-Api-Key': self._API_KEY,
  55. }
  56. if self._access_token:
  57. headers['Authorization'] = 'Bearer ' + self._access_token
  58. return self._download_json(
  59. 'https://api2.fox.com/v2.0/' + path,
  60. video_id, data=data, headers=headers)
  61. def _real_initialize(self):
  62. if not self._access_token:
  63. mvpd_auth = self._get_cookies(self._HOME_PAGE_URL).get('mvpd-auth')
  64. if mvpd_auth:
  65. self._access_token = (self._parse_json(compat_urllib_parse_unquote(
  66. mvpd_auth.value), None, fatal=False) or {}).get('accessToken')
  67. if not self._access_token:
  68. self._access_token = self._call_api(
  69. 'login', None, json.dumps({
  70. 'deviceId': compat_str(uuid.uuid4()),
  71. }).encode())['accessToken']
  72. def _real_extract(self, url):
  73. video_id = self._match_id(url)
  74. video = self._call_api('vodplayer/' + video_id, video_id)
  75. title = video['name']
  76. release_url = video['url']
  77. m3u8_url = self._download_json(release_url, video_id)['playURL']
  78. formats = self._extract_m3u8_formats(
  79. m3u8_url, video_id, 'mp4',
  80. entry_protocol='m3u8_native', m3u8_id='hls')
  81. self._sort_formats(formats)
  82. data = try_get(
  83. video, lambda x: x['trackingData']['properties'], dict) or {}
  84. duration = int_or_none(video.get('durationInSeconds')) or int_or_none(
  85. video.get('duration')) or parse_duration(video.get('duration'))
  86. timestamp = unified_timestamp(video.get('datePublished'))
  87. creator = data.get('brand') or data.get('network') or video.get('network')
  88. series = video.get('seriesName') or data.get(
  89. 'seriesName') or data.get('show')
  90. subtitles = {}
  91. for doc_rel in video.get('documentReleases', []):
  92. rel_url = doc_rel.get('url')
  93. if not url or doc_rel.get('format') != 'SCC':
  94. continue
  95. subtitles['en'] = [{
  96. 'url': rel_url,
  97. 'ext': 'scc',
  98. }]
  99. break
  100. return {
  101. 'id': video_id,
  102. 'title': title,
  103. 'formats': formats,
  104. 'description': video.get('description'),
  105. 'duration': duration,
  106. 'timestamp': timestamp,
  107. 'age_limit': parse_age_limit(video.get('contentRating')),
  108. 'creator': creator,
  109. 'series': series,
  110. 'season_number': int_or_none(video.get('seasonNumber')),
  111. 'episode': video.get('name'),
  112. 'episode_number': int_or_none(video.get('episodeNumber')),
  113. 'release_year': int_or_none(video.get('releaseYear')),
  114. 'subtitles': subtitles,
  115. }