2
0

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