]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/discovery.py
f9cec1d23db9e0ff389a9fa5ffd24ff9c8558d4b
[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 ..utils import (
9 ExtractorError,
10 update_url_query,
11 )
12 from ..compat import compat_HTTPError
13
14
15 class DiscoveryIE(DiscoveryGoBaseIE):
16 _VALID_URL = r'''(?x)https?://(?:www\.)?(?:
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
46 def _real_extract(self, url):
47 path, display_id = re.match(self._VALID_URL, url).groups()
48 webpage = self._download_webpage(url, display_id)
49
50 react_data = self._parse_json(self._search_regex(
51 r'window\.__reactTransmitPacket\s*=\s*({.+?});',
52 webpage, 'react data'), display_id)
53 content_blocks = react_data['layout'][path]['contentBlocks']
54 video = next(cb for cb in content_blocks if cb.get('type') == 'video')['content']['items'][0]
55 video_id = video['id']
56
57 access_token = self._download_json(
58 'https://www.discovery.com/anonymous', display_id, query={
59 'authLink': update_url_query(
60 'https://login.discovery.com/v1/oauth2/authorize', {
61 'client_id': react_data['application']['apiClientId'],
62 'redirect_uri': 'https://fusion.ddmcdn.com/app/mercury-sdk/180/redirectHandler.html',
63 'response_type': 'anonymous',
64 'state': 'nonce,' + ''.join([random.choice(string.ascii_letters) for _ in range(32)]),
65 })
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)