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.

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