[discoverygo] Add extractor (Closes #10245)
[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 )
11
12
13 class DiscoveryGoIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?discoverygo\.com/(?:[^/]+/)*(?P<id>[^/?#&]+)'
15     _TEST = {
16         'url': 'https://www.discoverygo.com/love-at-first-kiss/kiss-first-ask-questions-later/',
17         'info_dict': {
18             'id': '57a33c536b66d1cd0345eeb1',
19             'ext': 'mp4',
20             'title': 'Kiss First, Ask Questions Later!',
21             'description': 'md5:fe923ba34050eae468bffae10831cb22',
22             'duration': 2579,
23             'series': 'Love at First Kiss',
24             'season_number': 1,
25             'episode_number': 1,
26             'age_limit': 14,
27         },
28     }
29
30     def _real_extract(self, url):
31         display_id = self._match_id(url)
32
33         webpage = self._download_webpage(url, display_id)
34
35         container = extract_attributes(
36             self._search_regex(
37                 r'(<div[^>]+class=["\']video-player-container[^>]+>)',
38                 webpage, 'video container'))
39
40         video = self._parse_json(
41             unescapeHTML(container.get('data-video') or container.get('data-json')),
42             display_id)
43
44         title = video['name']
45
46         stream = video['stream']
47         STREAM_URL_SUFFIX = 'streamUrl'
48         formats = []
49         for stream_kind in ('', 'hds'):
50             suffix = STREAM_URL_SUFFIX.capitalize() if stream_kind else STREAM_URL_SUFFIX
51             stream_url = stream.get('%s%s' % (stream_kind, suffix))
52             if not stream_url:
53                 continue
54             if stream_kind == '':
55                 formats.extend(self._extract_m3u8_formats(
56                     stream_url, display_id, 'mp4', entry_protocol='m3u8_native',
57                     m3u8_id='hls', fatal=False))
58             elif stream_kind == 'hds':
59                 formats.extend(self._extract_f4m_formats(
60                     stream_url, display_id, f4m_id=stream_kind, fatal=False))
61         self._sort_formats(formats)
62
63         video_id = video.get('id') or display_id
64         description = video.get('description', {}).get('detailed')
65         duration = int_or_none(video.get('duration'))
66
67         series = video.get('show', {}).get('name')
68         season_number = int_or_none(video.get('season', {}).get('number'))
69         episode_number = int_or_none(video.get('episodeNumber'))
70
71         tags = video.get('tags')
72         age_limit = parse_age_limit(video.get('parental', {}).get('rating'))
73
74         subtitles = {}
75         captions = stream.get('captions')
76         if isinstance(captions, list):
77             for caption in captions:
78                 subtitle_url = caption.get('fileUrl')
79                 if (not subtitle_url or not isinstance(subtitle_url, compat_str) or
80                         not subtitle_url.startswith('http')):
81                     continue
82                 lang = caption.get('fileLang', 'en')
83                 subtitles.setdefault(lang, []).append({'url': subtitle_url})
84
85         return {
86             'id': video_id,
87             'display_id': display_id,
88             'title': title,
89             'description': description,
90             'duration': duration,
91             'series': series,
92             'season_number': season_number,
93             'episode_number': episode_number,
94             'tags': tags,
95             'age_limit': age_limit,
96             'formats': formats,
97             'subtitles': subtitles,
98         }