]>
Commit | Line | Data |
---|---|---|
0fa9a1e2 THD |
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_get, | |
9 | ExtractorError, | |
10 | ) | |
11 | from ..compat import compat_str | |
12 | ||
13 | ||
14 | class 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( | |
177877c5 | 50 | hls_url, video_id, ext='mp4', m3u8_id='hls-%s' % name, quality=quality) |
0fa9a1e2 THD |
51 | formats.extend(hls_fmts) |
52 | else: | |
53 | hls_fmts = [] | |
54 | ||
55 | # RTMP url for audio_only is same as high format, so skip it | |
56 | if rtmp_url and not audio_only: | |
57 | formats.append({ | |
58 | 'url': rtmp_url, | |
59 | 'format_id': 'rtmp-%s' % name, | |
60 | 'ext': 'mp4', | |
61 | 'protocol': 'rtmp_ffmpeg', # ffmpeg can, while rtmpdump can't | |
62 | 'vcodec': 'h264', | |
63 | 'acodec': 'aac', | |
64 | 'quality': quality, | |
65 | 'format_note': fmt.get('label'), | |
66 | # note: HLS and RTMP have same resolution for now, so it's acceptable | |
67 | 'width': try_get(hls_fmts, lambda x: x[0]['width'], int), | |
68 | 'height': try_get(hls_fmts, lambda x: x[0]['height'], int), | |
69 | }) | |
70 | ||
71 | # This contains the same formats as the above manifests and is used only as a fallback | |
72 | formats.extend(self._extract_m3u8_formats( | |
177877c5 | 73 | hls_url, video_id, ext='mp4', m3u8_id='hls')) |
0fa9a1e2 THD |
74 | self._remove_duplicate_formats(formats) |
75 | self._sort_formats(formats) | |
76 | ||
77 | uploader_url = try_get(metadata, lambda x: x['live']['user']['user_path'], compat_str) | |
78 | if uploader_url: | |
79 | uploader_url = 'https://whowatch.tv/profile/%s' % uploader_url | |
80 | uploader_id = compat_str(try_get(metadata, lambda x: x['live']['user']['id'], int)) | |
81 | uploader = try_get(metadata, lambda x: x['live']['user']['name'], compat_str) | |
82 | thumbnail = try_get(metadata, lambda x: x['live']['latest_thumbnail_url'], compat_str) | |
83 | timestamp = int_or_none(try_get(metadata, lambda x: x['live']['started_at'], int), scale=1000) | |
84 | view_count = try_get(metadata, lambda x: x['live']['total_view_count'], int) | |
85 | comment_count = try_get(metadata, lambda x: x['live']['comment_count'], int) | |
86 | ||
87 | return { | |
88 | 'id': video_id, | |
89 | 'title': title, | |
90 | 'uploader_id': uploader_id, | |
91 | 'uploader_url': uploader_url, | |
92 | 'uploader': uploader, | |
93 | 'formats': formats, | |
94 | 'thumbnail': thumbnail, | |
95 | 'timestamp': timestamp, | |
96 | 'view_count': view_count, | |
97 | 'comment_count': comment_count, | |
98 | 'is_live': True, | |
99 | } |