fox.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_str,
  8. compat_urllib_parse_unquote,
  9. )
  10. from ..utils import (
  11. int_or_none,
  12. parse_age_limit,
  13. parse_duration,
  14. try_get,
  15. unified_timestamp,
  16. )
  17. class FOXIE(AdobePassIE):
  18. _VALID_URL = r'https?://(?:www\.)?fox\.com/watch/(?P<id>[\da-fA-F]+)'
  19. _TESTS = [{
  20. # clip
  21. 'url': 'https://www.fox.com/watch/4b765a60490325103ea69888fb2bd4e8/',
  22. 'md5': 'ebd296fcc41dd4b19f8115d8461a3165',
  23. 'info_dict': {
  24. 'id': '4b765a60490325103ea69888fb2bd4e8',
  25. 'ext': 'mp4',
  26. 'title': 'Aftermath: Bruce Wayne Develops Into The Dark Knight',
  27. 'description': 'md5:549cd9c70d413adb32ce2a779b53b486',
  28. 'duration': 102,
  29. 'timestamp': 1504291893,
  30. 'upload_date': '20170901',
  31. 'creator': 'FOX',
  32. 'series': 'Gotham',
  33. 'age_limit': 14,
  34. },
  35. 'params': {
  36. 'skip_download': True,
  37. },
  38. }, {
  39. # episode, geo-restricted
  40. 'url': 'https://www.fox.com/watch/087036ca7f33c8eb79b08152b4dd75c1/',
  41. 'only_matching': True,
  42. }, {
  43. # episode, geo-restricted, tv provided required
  44. 'url': 'https://www.fox.com/watch/30056b295fb57f7452aeeb4920bc3024/',
  45. 'only_matching': True,
  46. }]
  47. _HOME_PAGE_URL = 'https://www.fox.com/'
  48. _API_KEY = 'abdcbed02c124d393b39e818a4312055'
  49. _access_token = None
  50. def _call_api(self, path, video_id, data=None):
  51. headers = {
  52. 'X-Api-Key': self._API_KEY,
  53. }
  54. if self._access_token:
  55. headers['Authorization'] = 'Bearer ' + self._access_token
  56. return self._download_json(
  57. 'https://api2.fox.com/v2.0/' + path,
  58. video_id, data=data, headers=headers)
  59. def _real_initialize(self):
  60. if not self._access_token:
  61. mvpd_auth = self._get_cookies(self._HOME_PAGE_URL).get('mvpd-auth')
  62. if mvpd_auth:
  63. self._access_token = (self._parse_json(compat_urllib_parse_unquote(
  64. mvpd_auth.value), None, fatal=False) or {}).get('accessToken')
  65. if not self._access_token:
  66. self._access_token = self._call_api(
  67. 'login', None, json.dumps({
  68. 'deviceId': compat_str(uuid.uuid4()),
  69. }).encode())['accessToken']
  70. def _real_extract(self, url):
  71. video_id = self._match_id(url)
  72. video = self._call_api('vodplayer/' + video_id, video_id)
  73. title = video['name']
  74. release_url = video['url']
  75. m3u8_url = self._download_json(release_url, video_id)['playURL']
  76. formats = self._extract_m3u8_formats(
  77. m3u8_url, video_id, 'mp4',
  78. entry_protocol='m3u8_native', m3u8_id='hls')
  79. self._sort_formats(formats)
  80. data = try_get(
  81. video, lambda x: x['trackingData']['properties'], dict) or {}
  82. duration = int_or_none(video.get('durationInSeconds')) or int_or_none(
  83. video.get('duration')) or parse_duration(video.get('duration'))
  84. timestamp = unified_timestamp(video.get('datePublished'))
  85. creator = data.get('brand') or data.get('network') or video.get('network')
  86. series = video.get('seriesName') or data.get(
  87. 'seriesName') or data.get('show')
  88. subtitles = {}
  89. for doc_rel in video.get('documentReleases', []):
  90. rel_url = doc_rel.get('url')
  91. if not url or doc_rel.get('format') != 'SCC':
  92. continue
  93. subtitles['en'] = [{
  94. 'url': rel_url,
  95. 'ext': 'scc',
  96. }]
  97. break
  98. return {
  99. 'id': video_id,
  100. 'title': title,
  101. 'formats': formats,
  102. 'description': video.get('description'),
  103. 'duration': duration,
  104. 'timestamp': timestamp,
  105. 'age_limit': parse_age_limit(video.get('contentRating')),
  106. 'creator': creator,
  107. 'series': series,
  108. 'season_number': int_or_none(video.get('seasonNumber')),
  109. 'episode': video.get('name'),
  110. 'episode_number': int_or_none(video.get('episodeNumber')),
  111. 'release_year': int_or_none(video.get('releaseYear')),
  112. 'subtitles': subtitles,
  113. }