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