Merge branch 'master' of https://github.com/DarkstaIkers/youtube-dl into DarkstaIkers...
[youtube-dl] / youtube_dl / extractor / discoverygo.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..compat import compat_str
5 from ..utils import (
6     extract_attributes,
7     int_or_none,
8     parse_age_limit,
9     unescapeHTML,
10     ExtractorError,
11 )
12
13
14 class DiscoveryGoIE(InfoExtractor):
15     _VALID_URL = r'''(?x)https?://(?:www\.)?(?:
16             discovery|
17             investigationdiscovery|
18             discoverylife|
19             animalplanet|
20             ahctv|
21             destinationamerica|
22             sciencechannel|
23             tlc|
24             velocitychannel
25         )go\.com/(?:[^/]+/)*(?P<id>[^/?#&]+)'''
26     _TEST = {
27         'url': 'https://www.discoverygo.com/love-at-first-kiss/kiss-first-ask-questions-later/',
28         'info_dict': {
29             'id': '57a33c536b66d1cd0345eeb1',
30             'ext': 'mp4',
31             'title': 'Kiss First, Ask Questions Later!',
32             'description': 'md5:fe923ba34050eae468bffae10831cb22',
33             'duration': 2579,
34             'series': 'Love at First Kiss',
35             'season_number': 1,
36             'episode_number': 1,
37             'age_limit': 14,
38         },
39     }
40
41     def _real_extract(self, url):
42         display_id = self._match_id(url)
43
44         webpage = self._download_webpage(url, display_id)
45
46         container = extract_attributes(
47             self._search_regex(
48                 r'(<div[^>]+class=["\']video-player-container[^>]+>)',
49                 webpage, 'video container'))
50
51         video = self._parse_json(
52             unescapeHTML(container.get('data-video') or container.get('data-json')),
53             display_id)
54
55         title = video['name']
56
57         stream = video.get('stream')
58         if not stream:
59             if video.get('authenticated') is True:
60                 raise ExtractorError(
61                     'This video is only available via cable service provider subscription that'
62                     ' is not currently supported. You may want to use --cookies.', expected=True)
63             else:
64                 raise ExtractorError('Unable to find stream')
65         STREAM_URL_SUFFIX = 'streamUrl'
66         formats = []
67         for stream_kind in ('', 'hds'):
68             suffix = STREAM_URL_SUFFIX.capitalize() if stream_kind else STREAM_URL_SUFFIX
69             stream_url = stream.get('%s%s' % (stream_kind, suffix))
70             if not stream_url:
71                 continue
72             if stream_kind == '':
73                 formats.extend(self._extract_m3u8_formats(
74                     stream_url, display_id, 'mp4', entry_protocol='m3u8_native',
75                     m3u8_id='hls', fatal=False))
76             elif stream_kind == 'hds':
77                 formats.extend(self._extract_f4m_formats(
78                     stream_url, display_id, f4m_id=stream_kind, fatal=False))
79         self._sort_formats(formats)
80
81         video_id = video.get('id') or display_id
82         description = video.get('description', {}).get('detailed')
83         duration = int_or_none(video.get('duration'))
84
85         series = video.get('show', {}).get('name')
86         season_number = int_or_none(video.get('season', {}).get('number'))
87         episode_number = int_or_none(video.get('episodeNumber'))
88
89         tags = video.get('tags')
90         age_limit = parse_age_limit(video.get('parental', {}).get('rating'))
91
92         subtitles = {}
93         captions = stream.get('captions')
94         if isinstance(captions, list):
95             for caption in captions:
96                 subtitle_url = caption.get('fileUrl')
97                 if (not subtitle_url or not isinstance(subtitle_url, compat_str) or
98                         not subtitle_url.startswith('http')):
99                     continue
100                 lang = caption.get('fileLang', 'en')
101                 subtitles.setdefault(lang, []).append({'url': subtitle_url})
102
103         return {
104             'id': video_id,
105             'display_id': display_id,
106             'title': title,
107             'description': description,
108             'duration': duration,
109             'series': series,
110             'season_number': season_number,
111             'episode_number': episode_number,
112             'tags': tags,
113             'age_limit': age_limit,
114             'formats': formats,
115             'subtitles': subtitles,
116         }