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.

116 lines
4.5 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 (
  7. compat_str,
  8. compat_urllib_parse_unquote,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. try_get,
  13. )
  14. from ..compat import compat_HTTPError
  15. class DiscoveryIE(DiscoveryGoBaseIE):
  16. _VALID_URL = r'''(?x)https?://
  17. (?P<site>
  18. (?:www\.)?
  19. (?:
  20. discovery|
  21. investigationdiscovery|
  22. discoverylife|
  23. animalplanet|
  24. ahctv|
  25. destinationamerica|
  26. sciencechannel|
  27. tlc|
  28. velocity
  29. )|
  30. watch\.
  31. (?:
  32. hgtv|
  33. foodnetwork|
  34. travelchannel|
  35. diynetwork|
  36. cookingchanneltv|
  37. motortrend
  38. )
  39. )\.com(?P<path>/tv-shows/[^/]+/(?:video|full-episode)s/(?P<id>[^./?#]+))'''
  40. _TESTS = [{
  41. 'url': 'https://www.discovery.com/tv-shows/cash-cab/videos/dave-foley',
  42. 'info_dict': {
  43. 'id': '5a2d9b4d6b66d17a5026e1fd',
  44. 'ext': 'mp4',
  45. 'title': 'Dave Foley',
  46. 'description': 'md5:4b39bcafccf9167ca42810eb5f28b01f',
  47. 'duration': 608,
  48. },
  49. 'params': {
  50. 'skip_download': True, # requires ffmpeg
  51. }
  52. }, {
  53. 'url': 'https://www.investigationdiscovery.com/tv-shows/final-vision/full-episodes/final-vision',
  54. 'only_matching': True,
  55. }]
  56. _GEO_COUNTRIES = ['US']
  57. _GEO_BYPASS = False
  58. def _real_extract(self, url):
  59. site, path, display_id = re.match(self._VALID_URL, url).groups()
  60. webpage = self._download_webpage(url, display_id)
  61. react_data = self._parse_json(self._search_regex(
  62. r'window\.__reactTransmitPacket\s*=\s*({.+?});',
  63. webpage, 'react data'), display_id)
  64. content_blocks = react_data['layout'][path]['contentBlocks']
  65. video = next(cb for cb in content_blocks if cb.get('type') == 'video')['content']['items'][0]
  66. video_id = video['id']
  67. access_token = None
  68. cookies = self._get_cookies(url)
  69. # prefer Affiliate Auth Token over Anonymous Auth Token
  70. auth_storage_cookie = cookies.get('eosAf') or cookies.get('eosAn')
  71. if auth_storage_cookie and auth_storage_cookie.value:
  72. auth_storage = self._parse_json(compat_urllib_parse_unquote(
  73. compat_urllib_parse_unquote(auth_storage_cookie.value)),
  74. video_id, fatal=False) or {}
  75. access_token = auth_storage.get('a') or auth_storage.get('access_token')
  76. if not access_token:
  77. access_token = self._download_json(
  78. 'https://%s.com/anonymous' % site, display_id, query={
  79. 'authRel': 'authorization',
  80. 'client_id': try_get(
  81. react_data, lambda x: x['application']['apiClientId'],
  82. compat_str) or '3020a40c2356a645b4b4',
  83. 'nonce': ''.join([random.choice(string.ascii_letters) for _ in range(32)]),
  84. 'redirectUri': 'https://fusion.ddmcdn.com/app/mercury-sdk/180/redirectHandler.html?https://www.%s.com' % site,
  85. })['access_token']
  86. try:
  87. headers = self.geo_verification_headers()
  88. headers['Authorization'] = 'Bearer ' + access_token
  89. stream = self._download_json(
  90. 'https://api.discovery.com/v1/streaming/video/' + video_id,
  91. display_id, headers=headers)
  92. except ExtractorError as e:
  93. if isinstance(e.cause, compat_HTTPError) and e.cause.code in (401, 403):
  94. e_description = self._parse_json(
  95. e.cause.read().decode(), display_id)['description']
  96. if 'resource not available for country' in e_description:
  97. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  98. if 'Authorized Networks' in e_description:
  99. raise ExtractorError(
  100. 'This video is only available via cable service provider subscription that'
  101. ' is not currently supported. You may want to use --cookies.', expected=True)
  102. raise ExtractorError(e_description)
  103. raise
  104. return self._extract_video_info(video, stream, display_id)