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