logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

discovery.py (4912B)


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