[discoverygo:playlist] Add extractor (closes #12424)
[youtube-dl] / youtube_dl / extractor / discoverygo.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_str
7 from ..utils import (
8     extract_attributes,
9     ExtractorError,
10     int_or_none,
11     parse_age_limit,
12     remove_end,
13     unescapeHTML,
14 )
15
16
17 class DiscoveryGoBaseIE(InfoExtractor):
18     _VALID_URL_TEMPLATE = r'''(?x)https?://(?:www\.)?(?:
19             discovery|
20             investigationdiscovery|
21             discoverylife|
22             animalplanet|
23             ahctv|
24             destinationamerica|
25             sciencechannel|
26             tlc|
27             velocitychannel
28         )go\.com/%s(?P<id>[^/?#&]+)'''
29
30
31 class DiscoveryGoIE(DiscoveryGoBaseIE):
32     _VALID_URL = DiscoveryGoBaseIE._VALID_URL_TEMPLATE % r'(?:[^/]+/)+'
33     _TEST = {
34         'url': 'https://www.discoverygo.com/love-at-first-kiss/kiss-first-ask-questions-later/',
35         'info_dict': {
36             'id': '57a33c536b66d1cd0345eeb1',
37             'ext': 'mp4',
38             'title': 'Kiss First, Ask Questions Later!',
39             'description': 'md5:fe923ba34050eae468bffae10831cb22',
40             'duration': 2579,
41             'series': 'Love at First Kiss',
42             'season_number': 1,
43             'episode_number': 1,
44             'age_limit': 14,
45         },
46     }
47
48     def _real_extract(self, url):
49         display_id = self._match_id(url)
50
51         webpage = self._download_webpage(url, display_id)
52
53         container = extract_attributes(
54             self._search_regex(
55                 r'(<div[^>]+class=["\']video-player-container[^>]+>)',
56                 webpage, 'video container'))
57
58         video = self._parse_json(
59             container.get('data-video') or container.get('data-json'),
60             display_id)
61
62         title = video['name']
63
64         stream = video.get('stream')
65         if not stream:
66             if video.get('authenticated') is True:
67                 raise ExtractorError(
68                     'This video is only available via cable service provider subscription that'
69                     ' is not currently supported. You may want to use --cookies.', expected=True)
70             else:
71                 raise ExtractorError('Unable to find stream')
72         STREAM_URL_SUFFIX = 'streamUrl'
73         formats = []
74         for stream_kind in ('', 'hds'):
75             suffix = STREAM_URL_SUFFIX.capitalize() if stream_kind else STREAM_URL_SUFFIX
76             stream_url = stream.get('%s%s' % (stream_kind, suffix))
77             if not stream_url:
78                 continue
79             if stream_kind == '':
80                 formats.extend(self._extract_m3u8_formats(
81                     stream_url, display_id, 'mp4', entry_protocol='m3u8_native',
82                     m3u8_id='hls', fatal=False))
83             elif stream_kind == 'hds':
84                 formats.extend(self._extract_f4m_formats(
85                     stream_url, display_id, f4m_id=stream_kind, fatal=False))
86         self._sort_formats(formats)
87
88         video_id = video.get('id') or display_id
89         description = video.get('description', {}).get('detailed')
90         duration = int_or_none(video.get('duration'))
91
92         series = video.get('show', {}).get('name')
93         season_number = int_or_none(video.get('season', {}).get('number'))
94         episode_number = int_or_none(video.get('episodeNumber'))
95
96         tags = video.get('tags')
97         age_limit = parse_age_limit(video.get('parental', {}).get('rating'))
98
99         subtitles = {}
100         captions = stream.get('captions')
101         if isinstance(captions, list):
102             for caption in captions:
103                 subtitle_url = caption.get('fileUrl')
104                 if (not subtitle_url or not isinstance(subtitle_url, compat_str) or
105                         not subtitle_url.startswith('http')):
106                     continue
107                 lang = caption.get('fileLang', 'en')
108                 subtitles.setdefault(lang, []).append({'url': subtitle_url})
109
110         return {
111             'id': video_id,
112             'display_id': display_id,
113             'title': title,
114             'description': description,
115             'duration': duration,
116             'series': series,
117             'season_number': season_number,
118             'episode_number': episode_number,
119             'tags': tags,
120             'age_limit': age_limit,
121             'formats': formats,
122             'subtitles': subtitles,
123         }
124
125
126 class DiscoveryGoPlaylistIE(DiscoveryGoBaseIE):
127     _VALID_URL = DiscoveryGoBaseIE._VALID_URL_TEMPLATE % ''
128     _TEST = {
129         'url': 'https://www.discoverygo.com/bering-sea-gold/',
130         'info_dict': {
131             'id': 'bering-sea-gold',
132             'title': 'Bering Sea Gold',
133             'description': 'md5:cc5c6489835949043c0cc3ad66c2fa0e',
134         },
135         'playlist_mincount': 6,
136     }
137
138     @classmethod
139     def suitable(cls, url):
140         return False if DiscoveryGoIE.suitable(url) else super(
141             DiscoveryGoPlaylistIE, cls).suitable(url)
142
143     def _real_extract(self, url):
144         display_id = self._match_id(url)
145
146         webpage = self._download_webpage(url, display_id)
147
148         entries = []
149         for mobj in re.finditer(r'data-json=(["\'])(?P<json>{.+?})\1', webpage):
150             data = self._parse_json(
151                 mobj.group('json'), display_id,
152                 transform_source=unescapeHTML, fatal=False)
153             if not isinstance(data, dict) or data.get('type') != 'episode':
154                 continue
155             episode_url = data.get('socialUrl')
156             if not episode_url:
157                 continue
158             entries.append(self.url_result(
159                 episode_url, ie=DiscoveryGoIE.ie_key(),
160                 video_id=data.get('id')))
161
162         return self.playlist_result(
163             entries, display_id,
164             remove_end(self._og_search_title(
165                 webpage, fatal=False), ' | Discovery GO'),
166             self._og_search_description(webpage))