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.

119 lines
4.9 KiB

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