]>
Commit | Line | Data |
---|---|---|
0fa9a1e2 THD |
1 | from .common import InfoExtractor |
2 | from ..utils import ( | |
3 | int_or_none, | |
4 | qualities, | |
c4f60dd7 | 5 | try_call, |
0fa9a1e2 THD |
6 | try_get, |
7 | ExtractorError, | |
8 | ) | |
9 | from ..compat import compat_str | |
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 | ||
c4f60dd7 | 27 | title = try_call( |
28 | lambda: live_data['share_info']['live_title'][1:-1], | |
29 | lambda: metadata['live']['title'], | |
30 | expected_type=str) | |
0fa9a1e2 THD |
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( | |
177877c5 | 48 | hls_url, video_id, ext='mp4', m3u8_id='hls-%s' % name, quality=quality) |
0fa9a1e2 THD |
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( | |
177877c5 | 71 | hls_url, video_id, ext='mp4', m3u8_id='hls')) |
0fa9a1e2 THD |
72 | self._remove_duplicate_formats(formats) |
73 | self._sort_formats(formats) | |
74 | ||
75 | uploader_url = try_get(metadata, lambda x: x['live']['user']['user_path'], compat_str) | |
76 | if uploader_url: | |
77 | uploader_url = 'https://whowatch.tv/profile/%s' % uploader_url | |
78 | uploader_id = compat_str(try_get(metadata, lambda x: x['live']['user']['id'], int)) | |
79 | uploader = try_get(metadata, lambda x: x['live']['user']['name'], compat_str) | |
80 | thumbnail = try_get(metadata, lambda x: x['live']['latest_thumbnail_url'], compat_str) | |
81 | timestamp = int_or_none(try_get(metadata, lambda x: x['live']['started_at'], int), scale=1000) | |
82 | view_count = try_get(metadata, lambda x: x['live']['total_view_count'], int) | |
83 | comment_count = try_get(metadata, lambda x: x['live']['comment_count'], int) | |
84 | ||
85 | return { | |
86 | 'id': video_id, | |
87 | 'title': title, | |
88 | 'uploader_id': uploader_id, | |
89 | 'uploader_url': uploader_url, | |
90 | 'uploader': uploader, | |
91 | 'formats': formats, | |
92 | 'thumbnail': thumbnail, | |
93 | 'timestamp': timestamp, | |
94 | 'view_count': view_count, | |
95 | 'comment_count': comment_count, | |
96 | 'is_live': True, | |
97 | } |