discovery.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import random
  2. import string
  3. import urllib.parse
  4. from .discoverygo import DiscoveryGoBaseIE
  5. from ..networking.exceptions import HTTPError
  6. from ..utils import ExtractorError
  7. class DiscoveryIE(DiscoveryGoBaseIE):
  8. _VALID_URL = r'''(?x)https?://
  9. (?P<site>
  10. go\.discovery|
  11. www\.
  12. (?:
  13. investigationdiscovery|
  14. discoverylife|
  15. animalplanet|
  16. ahctv|
  17. destinationamerica|
  18. sciencechannel|
  19. tlc
  20. )|
  21. watch\.
  22. (?:
  23. hgtv|
  24. foodnetwork|
  25. travelchannel|
  26. diynetwork|
  27. cookingchanneltv|
  28. motortrend
  29. )
  30. )\.com/tv-shows/(?P<show_slug>[^/]+)/(?:video|full-episode)s/(?P<id>[^./?#]+)'''
  31. _TESTS = [{
  32. 'url': 'https://go.discovery.com/tv-shows/cash-cab/videos/riding-with-matthew-perry',
  33. 'info_dict': {
  34. 'id': '5a2f35ce6b66d17a5026e29e',
  35. 'ext': 'mp4',
  36. 'title': 'Riding with Matthew Perry',
  37. 'description': 'md5:a34333153e79bc4526019a5129e7f878',
  38. 'duration': 84,
  39. },
  40. 'params': {
  41. 'skip_download': True, # requires ffmpeg
  42. },
  43. }, {
  44. 'url': 'https://www.investigationdiscovery.com/tv-shows/final-vision/full-episodes/final-vision',
  45. 'only_matching': True,
  46. }, {
  47. 'url': 'https://go.discovery.com/tv-shows/alaskan-bush-people/videos/follow-your-own-road',
  48. 'only_matching': True,
  49. }, {
  50. # using `show_slug` is important to get the correct video data
  51. 'url': 'https://www.sciencechannel.com/tv-shows/mythbusters-on-science/full-episodes/christmas-special',
  52. 'only_matching': True,
  53. }]
  54. _GEO_COUNTRIES = ['US']
  55. _GEO_BYPASS = False
  56. _API_BASE_URL = 'https://api.discovery.com/v1/'
  57. def _real_extract(self, url):
  58. site, show_slug, display_id = self._match_valid_url(url).groups()
  59. access_token = None
  60. cookies = self._get_cookies(url)
  61. # prefer Affiliate Auth Token over Anonymous Auth Token
  62. auth_storage_cookie = cookies.get('eosAf') or cookies.get('eosAn')
  63. if auth_storage_cookie and auth_storage_cookie.value:
  64. auth_storage = self._parse_json(urllib.parse.unquote(
  65. urllib.parse.unquote(auth_storage_cookie.value)),
  66. display_id, fatal=False) or {}
  67. access_token = auth_storage.get('a') or auth_storage.get('access_token')
  68. if not access_token:
  69. access_token = self._download_json(
  70. f'https://{site}.com/anonymous', display_id,
  71. 'Downloading token JSON metadata', query={
  72. 'authRel': 'authorization',
  73. 'client_id': '3020a40c2356a645b4b4',
  74. 'nonce': ''.join(random.choices(string.ascii_letters, k=32)),
  75. 'redirectUri': 'https://www.discovery.com/',
  76. })['access_token']
  77. headers = self.geo_verification_headers()
  78. headers['Authorization'] = 'Bearer ' + access_token
  79. try:
  80. video = self._download_json(
  81. self._API_BASE_URL + 'content/videos',
  82. display_id, 'Downloading content JSON metadata',
  83. headers=headers, query={
  84. 'embed': 'show.name',
  85. 'fields': 'authenticated,description.detailed,duration,episodeNumber,id,name,parental.rating,season.number,show,tags',
  86. 'slug': display_id,
  87. 'show_slug': show_slug,
  88. })[0]
  89. video_id = video['id']
  90. stream = self._download_json(
  91. self._API_BASE_URL + 'streaming/video/' + video_id,
  92. display_id, 'Downloading streaming JSON metadata', headers=headers)
  93. except ExtractorError as e:
  94. if isinstance(e.cause, HTTPError) and e.cause.status in (401, 403):
  95. e_description = self._parse_json(
  96. e.cause.response.read().decode(), display_id)['description']
  97. if 'resource not available for country' in e_description:
  98. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  99. if 'Authorized Networks' in e_description:
  100. raise ExtractorError(
  101. 'This video is only available via cable service provider subscription that'
  102. ' is not currently supported. You may want to use --cookies.', expected=True)
  103. raise ExtractorError(e_description)
  104. raise
  105. return self._extract_video_info(video, stream, display_id)