]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/mirrativ.py
[mirrativ] Cleanup extractor code (#2925)
[yt-dlp.git] / yt_dlp / extractor / mirrativ.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 dict_get,
7 traverse_obj,
8 try_get,
9 )
10
11
12 class MirrativBaseIE(InfoExtractor):
13 def assert_error(self, response):
14 error_message = traverse_obj(response, ('status', 'error'))
15 if error_message:
16 raise ExtractorError('Mirrativ says: %s' % error_message, expected=True)
17
18
19 class MirrativIE(MirrativBaseIE):
20 IE_NAME = 'mirrativ'
21 _VALID_URL = r'https?://(?:www\.)?mirrativ\.com/live/(?P<id>[^/?#&]+)'
22
23 TESTS = [{
24 'url': 'https://mirrativ.com/live/UQomuS7EMgHoxRHjEhNiHw',
25 'info_dict': {
26 'id': 'UQomuS7EMgHoxRHjEhNiHw',
27 'title': 'ねむいぃ、。『参加型』🔰jcが初めてやるCOD✨初見さん大歓迎💗',
28 'is_live': True,
29 'description': 'md5:bfcd8f77f2fab24c3c672e5620f3f16e',
30 'thumbnail': r're:https?://.+',
31 'uploader': '# あ ち ゅ 。💡',
32 'uploader_id': '118572165',
33 'duration': None,
34 'view_count': 1241,
35 'release_timestamp': 1646229192,
36 'timestamp': 1646229167,
37 'was_live': False,
38 },
39 'skip': 'livestream',
40 }, {
41 'url': 'https://mirrativ.com/live/POxyuG1KmW2982lqlDTuPw',
42 'only_matching': True,
43 }]
44
45 def _real_extract(self, url):
46 video_id = self._match_id(url)
47 webpage = self._download_webpage('https://www.mirrativ.com/live/%s' % video_id, video_id)
48 live_response = self._download_json(f'https://www.mirrativ.com/api/live/live?live_id={video_id}', video_id)
49 self.assert_error(live_response)
50
51 hls_url = dict_get(live_response, ('archive_url_hls', 'streaming_url_hls'))
52 is_live = bool(live_response.get('is_live'))
53 if not hls_url:
54 raise ExtractorError('Neither archive nor live is available.', expected=True)
55
56 formats = self._extract_m3u8_formats(
57 hls_url, video_id,
58 ext='mp4', entry_protocol='m3u8_native',
59 m3u8_id='hls', live=is_live)
60 self._sort_formats(formats)
61
62 return {
63 'id': video_id,
64 'title': self._og_search_title(webpage, default=None) or self._search_regex(
65 r'<title>\s*(.+?) - Mirrativ\s*</title>', webpage) or live_response.get('title'),
66 'is_live': is_live,
67 'description': live_response.get('description'),
68 'formats': formats,
69 'thumbnail': live_response.get('image_url'),
70 'uploader': traverse_obj(live_response, ('owner', 'name')),
71 'uploader_id': traverse_obj(live_response, ('owner', 'user_id')),
72 'duration': try_get(live_response, lambda x: x['ended_at'] - x['started_at']) if not is_live else None,
73 'view_count': live_response.get('total_viewer_num'),
74 'release_timestamp': live_response.get('started_at'),
75 'timestamp': live_response.get('created_at'),
76 'was_live': bool(live_response.get('is_archive')),
77 }
78
79
80 class MirrativUserIE(MirrativBaseIE):
81 IE_NAME = 'mirrativ:user'
82 _VALID_URL = r'https?://(?:www\.)?mirrativ\.com/user/(?P<id>\d+)'
83
84 _TESTS = [{
85 # Live archive is available up to 3 days
86 # see: https://helpfeel.com/mirrativ/%E9%8C%B2%E7%94%BB-5e26d3ad7b59ef0017fb49ac (Japanese)
87 'url': 'https://www.mirrativ.com/user/110943130',
88 'note': 'multiple archives available',
89 'only_matching': True,
90 }]
91
92 def _entries(self, user_id):
93 page = 1
94 while page is not None:
95 api_response = self._download_json(
96 f'https://www.mirrativ.com/api/live/live_history?user_id={user_id}&page={page}', user_id,
97 note=f'Downloading page {page}')
98 self.assert_error(api_response)
99 lives = api_response.get('lives')
100 if not lives:
101 break
102 for live in lives:
103 if not live.get('is_archive') and not live.get('is_live'):
104 # neither archive nor live is available, so skip it
105 # or the service will ban your IP address for a while
106 continue
107 live_id = live.get('live_id')
108 url = 'https://www.mirrativ.com/live/%s' % live_id
109 yield self.url_result(url, video_id=live_id, video_title=live.get('title'))
110 page = api_response.get('next_page')
111
112 def _real_extract(self, url):
113 user_id = self._match_id(url)
114 user_info = self._download_json(
115 f'https://www.mirrativ.com/api/user/profile?user_id={user_id}', user_id,
116 note='Downloading user info', fatal=False)
117 self.assert_error(user_info)
118
119 return self.playlist_result(
120 self._entries(user_id), user_id,
121 user_info.get('name'), user_info.get('description'))