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