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.

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