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