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