]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/whowatch.py
[utils] Add `try_call`
[yt-dlp.git] / yt_dlp / extractor / whowatch.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6 int_or_none,
7 qualities,
8 try_call,
9 try_get,
10 ExtractorError,
11 )
12 from ..compat import compat_str
13
14
15 class WhoWatchIE(InfoExtractor):
16 IE_NAME = 'whowatch'
17 _VALID_URL = r'https?://whowatch\.tv/viewer/(?P<id>\d+)'
18
19 _TESTS = [{
20 'url': 'https://whowatch.tv/viewer/21450171',
21 'only_matching': True,
22 }]
23
24 def _real_extract(self, url):
25 video_id = self._match_id(url)
26 self._download_webpage(url, video_id)
27 metadata = self._download_json('https://api.whowatch.tv/lives/%s' % video_id, video_id)
28 live_data = self._download_json('https://api.whowatch.tv/lives/%s/play' % video_id, video_id)
29
30 title = try_call(
31 lambda: live_data['share_info']['live_title'][1:-1],
32 lambda: metadata['live']['title'],
33 expected_type=str)
34
35 hls_url = live_data.get('hls_url')
36 if not hls_url:
37 raise ExtractorError(live_data.get('error_message') or 'The user is offline.', expected=True)
38
39 QUALITIES = qualities(['low', 'medium', 'high', 'veryhigh'])
40 formats = []
41
42 for i, fmt in enumerate(live_data.get('streams') or []):
43 name = fmt.get('quality') or fmt.get('name') or compat_str(i)
44 hls_url = fmt.get('hls_url')
45 rtmp_url = fmt.get('rtmp_url')
46 audio_only = fmt.get('audio_only')
47 quality = QUALITIES(fmt.get('quality'))
48
49 if hls_url:
50 hls_fmts = self._extract_m3u8_formats(
51 hls_url, video_id, ext='mp4', 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', m3u8_id='hls'))
75 self._remove_duplicate_formats(formats)
76 self._sort_formats(formats)
77
78 uploader_url = try_get(metadata, lambda x: x['live']['user']['user_path'], compat_str)
79 if uploader_url:
80 uploader_url = 'https://whowatch.tv/profile/%s' % uploader_url
81 uploader_id = compat_str(try_get(metadata, lambda x: x['live']['user']['id'], int))
82 uploader = try_get(metadata, lambda x: x['live']['user']['name'], compat_str)
83 thumbnail = try_get(metadata, lambda x: x['live']['latest_thumbnail_url'], compat_str)
84 timestamp = int_or_none(try_get(metadata, lambda x: x['live']['started_at'], int), scale=1000)
85 view_count = try_get(metadata, lambda x: x['live']['total_view_count'], int)
86 comment_count = try_get(metadata, lambda x: x['live']['comment_count'], int)
87
88 return {
89 'id': video_id,
90 'title': title,
91 'uploader_id': uploader_id,
92 'uploader_url': uploader_url,
93 'uploader': uploader,
94 'formats': formats,
95 'thumbnail': thumbnail,
96 'timestamp': timestamp,
97 'view_count': view_count,
98 'comment_count': comment_count,
99 'is_live': True,
100 }