[youtube] Skip unsupported adaptive stream type (#18804)
[youtube-dl] / youtube_dl / extractor / go90.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_HTTPError
8 from ..utils import (
9     determine_ext,
10     ExtractorError,
11     int_or_none,
12     parse_age_limit,
13     parse_iso8601,
14 )
15
16
17 class Go90IE(InfoExtractor):
18     _VALID_URL = r'https?://(?:www\.)?go90\.com/(?:videos|embed)/(?P<id>[0-9a-zA-Z]+)'
19     _TESTS = [{
20         'url': 'https://www.go90.com/videos/84BUqjLpf9D',
21         'md5': 'efa7670dbbbf21a7b07b360652b24a32',
22         'info_dict': {
23             'id': '84BUqjLpf9D',
24             'ext': 'mp4',
25             'title': 'Daily VICE - Inside The Utah Coalition Against Pornography Convention',
26             'description': 'VICE\'s Karley Sciortino meets with activists who discuss the state\'s strong anti-porn stance. Then, VICE Sports explains NFL contracts.',
27             'timestamp': 1491868800,
28             'upload_date': '20170411',
29             'age_limit': 14,
30         }
31     }, {
32         'url': 'https://www.go90.com/embed/261MflWkD3N',
33         'only_matching': True,
34     }]
35     _GEO_BYPASS = False
36
37     def _real_extract(self, url):
38         video_id = self._match_id(url)
39
40         try:
41             headers = self.geo_verification_headers()
42             headers.update({
43                 'Content-Type': 'application/json; charset=utf-8',
44             })
45             video_data = self._download_json(
46                 'https://www.go90.com/api/view/items/' + video_id, video_id,
47                 headers=headers, data=b'{"client":"web","device_type":"pc"}')
48         except ExtractorError as e:
49             if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
50                 message = self._parse_json(e.cause.read().decode(), None)['error']['message']
51                 if 'region unavailable' in message:
52                     self.raise_geo_restricted(countries=['US'])
53                 raise ExtractorError(message, expected=True)
54             raise
55
56         if video_data.get('requires_drm'):
57             raise ExtractorError('This video is DRM protected.', expected=True)
58         main_video_asset = video_data['main_video_asset']
59
60         episode_number = int_or_none(video_data.get('episode_number'))
61         series = None
62         season = None
63         season_id = None
64         season_number = None
65         for metadata in video_data.get('__children', {}).get('Item', {}).values():
66             if metadata.get('type') == 'show':
67                 series = metadata.get('title')
68             elif metadata.get('type') == 'season':
69                 season = metadata.get('title')
70                 season_id = metadata.get('id')
71                 season_number = int_or_none(metadata.get('season_number'))
72
73         title = episode = video_data.get('title') or series
74         if series and series != title:
75             title = '%s - %s' % (series, title)
76
77         thumbnails = []
78         formats = []
79         subtitles = {}
80         for asset in video_data.get('assets'):
81             if asset.get('id') == main_video_asset:
82                 for source in asset.get('sources', []):
83                     source_location = source.get('location')
84                     if not source_location:
85                         continue
86                     source_type = source.get('type')
87                     if source_type == 'hls':
88                         m3u8_formats = self._extract_m3u8_formats(
89                             source_location, video_id, 'mp4',
90                             'm3u8_native', m3u8_id='hls', fatal=False)
91                         for f in m3u8_formats:
92                             mobj = re.search(r'/hls-(\d+)-(\d+)K', f['url'])
93                             if mobj:
94                                 height, tbr = mobj.groups()
95                                 height = int_or_none(height)
96                                 f.update({
97                                     'height': f.get('height') or height,
98                                     'width': f.get('width') or int_or_none(height / 9.0 * 16.0 if height else None),
99                                     'tbr': f.get('tbr') or int_or_none(tbr),
100                                 })
101                         formats.extend(m3u8_formats)
102                     elif source_type == 'dash':
103                         formats.extend(self._extract_mpd_formats(
104                             source_location, video_id, mpd_id='dash', fatal=False))
105                     else:
106                         formats.append({
107                             'format_id': source.get('name'),
108                             'url': source_location,
109                             'width': int_or_none(source.get('width')),
110                             'height': int_or_none(source.get('height')),
111                             'tbr': int_or_none(source.get('bitrate')),
112                         })
113
114                 for caption in asset.get('caption_metadata', []):
115                     caption_url = caption.get('source_url')
116                     if not caption_url:
117                         continue
118                     subtitles.setdefault(caption.get('language', 'en'), []).append({
119                         'url': caption_url,
120                         'ext': determine_ext(caption_url, 'vtt'),
121                     })
122             elif asset.get('type') == 'image':
123                 asset_location = asset.get('location')
124                 if not asset_location:
125                     continue
126                 thumbnails.append({
127                     'url': asset_location,
128                     'width': int_or_none(asset.get('width')),
129                     'height': int_or_none(asset.get('height')),
130                 })
131         self._sort_formats(formats)
132
133         return {
134             'id': video_id,
135             'title': title,
136             'formats': formats,
137             'thumbnails': thumbnails,
138             'description': video_data.get('short_description'),
139             'like_count': int_or_none(video_data.get('like_count')),
140             'timestamp': parse_iso8601(video_data.get('released_at')),
141             'series': series,
142             'episode': episode,
143             'season': season,
144             'season_id': season_id,
145             'season_number': season_number,
146             'episode_number': episode_number,
147             'subtitles': subtitles,
148             'age_limit': parse_age_limit(video_data.get('rating')),
149         }