]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/whowatch.py
[youtube] Better message when login required
[yt-dlp.git] / yt_dlp / extractor / whowatch.py
CommitLineData
0fa9a1e2
THD
1# coding: utf-8
2from __future__ import unicode_literals
3
4from .common import InfoExtractor
5from ..utils import (
6 int_or_none,
7 qualities,
8 try_get,
9 ExtractorError,
10)
11from ..compat import compat_str
12
13
14class WhoWatchIE(InfoExtractor):
15 IE_NAME = 'whowatch'
16 _VALID_URL = r'https?://whowatch\.tv/viewer/(?P<id>\d+)'
17
18 _TESTS = [{
19 'url': 'https://whowatch.tv/viewer/21450171',
20 'only_matching': True,
21 }]
22
23 def _real_extract(self, url):
24 video_id = self._match_id(url)
25 self._download_webpage(url, video_id)
26 metadata = self._download_json('https://api.whowatch.tv/lives/%s' % video_id, video_id)
27 live_data = self._download_json('https://api.whowatch.tv/lives/%s/play' % video_id, video_id)
28
29 title = try_get(None, (
30 lambda x: live_data['share_info']['live_title'][1:-1],
31 lambda x: metadata['live']['title'],
32 ), compat_str)
33
34 hls_url = live_data.get('hls_url')
35 if not hls_url:
36 raise ExtractorError(live_data.get('error_message') or 'The user is offline.', expected=True)
37
38 QUALITIES = qualities(['low', 'medium', 'high', 'veryhigh'])
39 formats = []
40
41 for i, fmt in enumerate(live_data.get('streams') or []):
42 name = fmt.get('quality') or fmt.get('name') or compat_str(i)
43 hls_url = fmt.get('hls_url')
44 rtmp_url = fmt.get('rtmp_url')
45 audio_only = fmt.get('audio_only')
46 quality = QUALITIES(fmt.get('quality'))
47
48 if hls_url:
49 hls_fmts = self._extract_m3u8_formats(
50 hls_url, video_id, ext='mp4', entry_protocol='m3u8',
51 m3u8_id='hls-%s' % name, quality=quality)
52 formats.extend(hls_fmts)
53 else:
54 hls_fmts = []
55
56 # RTMP url for audio_only is same as high format, so skip it
57 if rtmp_url and not audio_only:
58 formats.append({
59 'url': rtmp_url,
60 'format_id': 'rtmp-%s' % name,
61 'ext': 'mp4',
62 'protocol': 'rtmp_ffmpeg', # ffmpeg can, while rtmpdump can't
63 'vcodec': 'h264',
64 'acodec': 'aac',
65 'quality': quality,
66 'format_note': fmt.get('label'),
67 # note: HLS and RTMP have same resolution for now, so it's acceptable
68 'width': try_get(hls_fmts, lambda x: x[0]['width'], int),
69 'height': try_get(hls_fmts, lambda x: x[0]['height'], int),
70 })
71
72 # This contains the same formats as the above manifests and is used only as a fallback
73 formats.extend(self._extract_m3u8_formats(
74 hls_url, video_id, ext='mp4', entry_protocol='m3u8',
75 m3u8_id='hls'))
76 self._remove_duplicate_formats(formats)
77 self._sort_formats(formats)
78
79 uploader_url = try_get(metadata, lambda x: x['live']['user']['user_path'], compat_str)
80 if uploader_url:
81 uploader_url = 'https://whowatch.tv/profile/%s' % uploader_url
82 uploader_id = compat_str(try_get(metadata, lambda x: x['live']['user']['id'], int))
83 uploader = try_get(metadata, lambda x: x['live']['user']['name'], compat_str)
84 thumbnail = try_get(metadata, lambda x: x['live']['latest_thumbnail_url'], compat_str)
85 timestamp = int_or_none(try_get(metadata, lambda x: x['live']['started_at'], int), scale=1000)
86 view_count = try_get(metadata, lambda x: x['live']['total_view_count'], int)
87 comment_count = try_get(metadata, lambda x: x['live']['comment_count'], int)
88
89 return {
90 'id': video_id,
91 'title': title,
92 'uploader_id': uploader_id,
93 'uploader_url': uploader_url,
94 'uploader': uploader,
95 'formats': formats,
96 'thumbnail': thumbnail,
97 'timestamp': timestamp,
98 'view_count': view_count,
99 'comment_count': comment_count,
100 'is_live': True,
101 }