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