]>
Commit | Line | Data |
---|---|---|
1 | import base64 | |
2 | import itertools | |
3 | import re | |
4 | ||
5 | from .common import InfoExtractor | |
6 | from ..dependencies import websockets | |
7 | from ..utils import ( | |
8 | ExtractorError, | |
9 | UserNotLive, | |
10 | clean_html, | |
11 | float_or_none, | |
12 | get_element_by_class, | |
13 | get_element_by_id, | |
14 | int_or_none, | |
15 | parse_duration, | |
16 | qualities, | |
17 | str_to_int, | |
18 | traverse_obj, | |
19 | try_get, | |
20 | unified_timestamp, | |
21 | urlencode_postdata, | |
22 | urljoin, | |
23 | ) | |
24 | ||
25 | ||
26 | class TwitCastingIE(InfoExtractor): | |
27 | _VALID_URL = r'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<uploader_id>[^/?#]+)/(?:movie|twplayer)/(?P<id>\d+)' | |
28 | _M3U8_HEADERS = { | |
29 | 'Origin': 'https://twitcasting.tv', | |
30 | 'Referer': 'https://twitcasting.tv/', | |
31 | } | |
32 | _TESTS = [{ | |
33 | 'url': 'https://twitcasting.tv/ivetesangalo/movie/2357609', | |
34 | 'md5': '745243cad58c4681dc752490f7540d7f', | |
35 | 'info_dict': { | |
36 | 'id': '2357609', | |
37 | 'ext': 'mp4', | |
38 | 'title': 'Live #2357609', | |
39 | 'uploader_id': 'ivetesangalo', | |
40 | 'description': 'Twitter Oficial da cantora brasileira Ivete Sangalo.', | |
41 | 'thumbnail': r're:^https?://.*\.jpg$', | |
42 | 'upload_date': '20110822', | |
43 | 'timestamp': 1313978424, | |
44 | 'duration': 32, | |
45 | 'view_count': int, | |
46 | }, | |
47 | 'params': { | |
48 | 'skip_download': True, | |
49 | }, | |
50 | }, { | |
51 | 'url': 'https://twitcasting.tv/mttbernardini/movie/3689740', | |
52 | 'info_dict': { | |
53 | 'id': '3689740', | |
54 | 'ext': 'mp4', | |
55 | 'title': 'Live playing something #3689740', | |
56 | 'uploader_id': 'mttbernardini', | |
57 | 'description': 'md5:1dc7efa2f1ab932fcd119265cebeec69', | |
58 | 'thumbnail': r're:^https?://.*\.jpg$', | |
59 | 'upload_date': '20120211', | |
60 | 'timestamp': 1328995624, | |
61 | 'duration': 681, | |
62 | 'view_count': int, | |
63 | }, | |
64 | 'params': { | |
65 | 'skip_download': True, | |
66 | 'videopassword': 'abc', | |
67 | }, | |
68 | }, { | |
69 | 'url': 'https://twitcasting.tv/loft_heaven/movie/685979292', | |
70 | 'info_dict': { | |
71 | 'id': '685979292', | |
72 | 'ext': 'mp4', | |
73 | 'title': '【無料配信】南波一海のhear/here “ナタリー望月哲さんに聞く編集と「渋谷系狂騒曲」”', | |
74 | 'uploader_id': 'loft_heaven', | |
75 | 'description': 'md5:3a0c7b53019df987ce545c935538bacf', | |
76 | 'upload_date': '20210604', | |
77 | 'timestamp': 1622802114, | |
78 | 'thumbnail': r're:^https?://.*\.jpg$', | |
79 | 'duration': 6964, | |
80 | 'view_count': int, | |
81 | }, | |
82 | 'params': { | |
83 | 'skip_download': True, | |
84 | }, | |
85 | }] | |
86 | ||
87 | def _parse_data_movie_playlist(self, dmp, video_id): | |
88 | # attempt 1: parse as JSON directly | |
89 | try: | |
90 | return self._parse_json(dmp, video_id) | |
91 | except ExtractorError: | |
92 | pass | |
93 | # attempt 2: decode reversed base64 | |
94 | decoded = base64.b64decode(dmp[::-1]) | |
95 | return self._parse_json(decoded, video_id) | |
96 | ||
97 | def _real_extract(self, url): | |
98 | uploader_id, video_id = self._match_valid_url(url).groups() | |
99 | ||
100 | webpage, urlh = self._download_webpage_handle(url, video_id) | |
101 | video_password = self.get_param('videopassword') | |
102 | request_data = None | |
103 | if video_password: | |
104 | request_data = urlencode_postdata({ | |
105 | 'password': video_password, | |
106 | **self._hidden_inputs(webpage), | |
107 | }, encoding='utf-8') | |
108 | webpage, urlh = self._download_webpage_handle( | |
109 | url, video_id, data=request_data, | |
110 | headers={'Origin': 'https://twitcasting.tv'}, | |
111 | note='Trying video password') | |
112 | if urlh.url != url and request_data: | |
113 | webpage = self._download_webpage( | |
114 | urlh.url, video_id, data=request_data, | |
115 | headers={'Origin': 'https://twitcasting.tv'}, | |
116 | note='Retrying authentication') | |
117 | # has to check here as the first request can contain password input form even if the password is correct | |
118 | if re.search(r'<form\s+method="POST">\s*<input\s+[^>]+?name="password"', webpage): | |
119 | raise ExtractorError('This video is protected by a password, use the --video-password option', expected=True) | |
120 | ||
121 | title = (clean_html(get_element_by_id('movietitle', webpage)) | |
122 | or self._html_search_meta(['og:title', 'twitter:title'], webpage, fatal=True)) | |
123 | ||
124 | video_js_data = try_get( | |
125 | webpage, | |
126 | lambda x: self._parse_data_movie_playlist(self._search_regex( | |
127 | r'data-movie-playlist=\'([^\']+?)\'', | |
128 | x, 'movie playlist', default=None), video_id)['2'], list) | |
129 | ||
130 | thumbnail = traverse_obj(video_js_data, (0, 'thumbnailUrl')) or self._og_search_thumbnail(webpage) | |
131 | description = clean_html(get_element_by_id( | |
132 | 'authorcomment', webpage)) or self._html_search_meta( | |
133 | ['description', 'og:description', 'twitter:description'], webpage) | |
134 | duration = (try_get(video_js_data, lambda x: sum(float_or_none(y.get('duration')) for y in x) / 1000) | |
135 | or parse_duration(clean_html(get_element_by_class('tw-player-duration-time', webpage)))) | |
136 | view_count = str_to_int(self._search_regex( | |
137 | (r'Total\s*:\s*Views\s*([\d,]+)', r'総視聴者\s*:\s*([\d,]+)\s*</'), webpage, 'views', None)) | |
138 | timestamp = unified_timestamp(self._search_regex( | |
139 | r'data-toggle="true"[^>]+datetime="([^"]+)"', | |
140 | webpage, 'datetime', None)) | |
141 | ||
142 | stream_server_data = self._download_json( | |
143 | 'https://twitcasting.tv/streamserver.php?target=%s&mode=client' % uploader_id, video_id, | |
144 | 'Downloading live info', fatal=False) | |
145 | ||
146 | is_live = any(f'data-{x}' in webpage for x in ['is-onlive="true"', 'live-type="live"', 'status="online"']) | |
147 | if not traverse_obj(stream_server_data, 'llfmp4') and is_live: | |
148 | self.raise_login_required(method='cookies') | |
149 | ||
150 | base_dict = { | |
151 | 'title': title, | |
152 | 'description': description, | |
153 | 'thumbnail': thumbnail, | |
154 | 'timestamp': timestamp, | |
155 | 'uploader_id': uploader_id, | |
156 | 'duration': duration, | |
157 | 'view_count': view_count, | |
158 | 'is_live': is_live, | |
159 | } | |
160 | ||
161 | def find_dmu(x): | |
162 | data_movie_url = self._search_regex( | |
163 | r'data-movie-url=(["\'])(?P<url>(?:(?!\1).)+)\1', | |
164 | x, 'm3u8 url', group='url', default=None) | |
165 | if data_movie_url: | |
166 | return [data_movie_url] | |
167 | ||
168 | m3u8_urls = (try_get(webpage, find_dmu, list) | |
169 | or traverse_obj(video_js_data, (..., 'source', 'url')) | |
170 | or ([f'https://twitcasting.tv/{uploader_id}/metastream.m3u8'] if is_live else None)) | |
171 | if not m3u8_urls: | |
172 | raise ExtractorError('Failed to get m3u8 playlist') | |
173 | ||
174 | if is_live: | |
175 | m3u8_url = m3u8_urls[0] | |
176 | formats = self._extract_m3u8_formats( | |
177 | m3u8_url, video_id, ext='mp4', m3u8_id='hls', | |
178 | live=True, headers=self._M3U8_HEADERS) | |
179 | ||
180 | if traverse_obj(stream_server_data, ('hls', 'source')): | |
181 | formats.extend(self._extract_m3u8_formats( | |
182 | m3u8_url, video_id, ext='mp4', m3u8_id='source', | |
183 | live=True, query={'mode': 'source'}, | |
184 | note='Downloading source quality m3u8', | |
185 | headers=self._M3U8_HEADERS, fatal=False)) | |
186 | ||
187 | if websockets: | |
188 | qq = qualities(['base', 'mobilesource', 'main']) | |
189 | streams = traverse_obj(stream_server_data, ('llfmp4', 'streams')) or {} | |
190 | for mode, ws_url in streams.items(): | |
191 | formats.append({ | |
192 | 'url': ws_url, | |
193 | 'format_id': 'ws-%s' % mode, | |
194 | 'ext': 'mp4', | |
195 | 'quality': qq(mode), | |
196 | 'source_preference': -10, | |
197 | # TwitCasting simply sends moof atom directly over WS | |
198 | 'protocol': 'websocket_frag', | |
199 | }) | |
200 | ||
201 | infodict = { | |
202 | 'formats': formats, | |
203 | '_format_sort_fields': ('source', ), | |
204 | } | |
205 | elif len(m3u8_urls) == 1: | |
206 | formats = self._extract_m3u8_formats( | |
207 | m3u8_urls[0], video_id, 'mp4', headers=self._M3U8_HEADERS) | |
208 | infodict = { | |
209 | # No problem here since there's only one manifest | |
210 | 'formats': formats, | |
211 | 'http_headers': self._M3U8_HEADERS, | |
212 | } | |
213 | else: | |
214 | infodict = { | |
215 | '_type': 'multi_video', | |
216 | 'entries': [{ | |
217 | 'id': f'{video_id}-{num}', | |
218 | 'url': m3u8_url, | |
219 | 'ext': 'mp4', | |
220 | # Requesting the manifests here will cause download to fail. | |
221 | # So use ffmpeg instead. See: https://github.com/yt-dlp/yt-dlp/issues/382 | |
222 | 'protocol': 'm3u8', | |
223 | 'http_headers': self._M3U8_HEADERS, | |
224 | **base_dict, | |
225 | } for (num, m3u8_url) in enumerate(m3u8_urls)], | |
226 | } | |
227 | ||
228 | return { | |
229 | 'id': video_id, | |
230 | **base_dict, | |
231 | **infodict, | |
232 | } | |
233 | ||
234 | ||
235 | class TwitCastingLiveIE(InfoExtractor): | |
236 | _VALID_URL = r'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<id>[^/?#]+)/?(?:[#?]|$)' | |
237 | _TESTS = [{ | |
238 | 'url': 'https://twitcasting.tv/ivetesangalo', | |
239 | 'only_matching': True, | |
240 | }, { | |
241 | 'url': 'https://twitcasting.tv/c:unusedlive', | |
242 | 'expected_exception': 'UserNotLive', | |
243 | }] | |
244 | ||
245 | _PROTECTED_LIVE_RE = r'(?s)(<span\s*class="tw-movie-thumbnail2-badge"\s*data-status="live">\s*LIVE)' | |
246 | ||
247 | def _real_extract(self, url): | |
248 | uploader_id = self._match_id(url) | |
249 | self.to_screen( | |
250 | 'Downloading live video of user {0}. ' | |
251 | 'Pass "https://twitcasting.tv/{0}/show" to download the history'.format(uploader_id)) | |
252 | ||
253 | webpage = self._download_webpage(url, uploader_id) | |
254 | is_live = self._search_regex( # first pattern is for public live | |
255 | (r'(data-is-onlive="true")', self._PROTECTED_LIVE_RE), webpage, 'is live?', default=None) | |
256 | current_live = int_or_none(self._search_regex( | |
257 | (r'data-type="movie" data-id="(\d+)">', # not available? | |
258 | r'tw-sound-flag-open-link" data-id="(\d+)" style=', # not available? | |
259 | r'data-movie-id="(\d+)"'), # if not currently live, value may be 0 | |
260 | webpage, 'current live ID', default=None)) | |
261 | if is_live and not current_live: | |
262 | # fetch unfiltered /show to find running livestreams; we can't get ID of the password-protected livestream above | |
263 | webpage = self._download_webpage( | |
264 | f'https://twitcasting.tv/{uploader_id}/show/', uploader_id, | |
265 | note='Downloading live history') | |
266 | is_live = self._search_regex(self._PROTECTED_LIVE_RE, webpage, 'is live?', default=None) | |
267 | if is_live: | |
268 | # get the first live; running live is always at the first | |
269 | current_live = self._search_regex( | |
270 | r'(?s)<a\s+class="tw-movie-thumbnail2"\s*href="/[^/]+/movie/(?P<video_id>\d+)"\s*>.+?</a>', | |
271 | webpage, 'current live ID 2', default=None, group='video_id') | |
272 | if not current_live: | |
273 | raise UserNotLive(video_id=uploader_id) | |
274 | return self.url_result(f'https://twitcasting.tv/{uploader_id}/movie/{current_live}', TwitCastingIE) | |
275 | ||
276 | ||
277 | class TwitCastingUserIE(InfoExtractor): | |
278 | _VALID_URL = r'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<id>[^/?#]+)/(:?show|archive)/?(?:[#?]|$)' | |
279 | _TESTS = [{ | |
280 | 'url': 'https://twitcasting.tv/natsuiromatsuri/archive/', | |
281 | 'info_dict': { | |
282 | 'id': 'natsuiromatsuri', | |
283 | 'title': 'natsuiromatsuri - Live History', | |
284 | }, | |
285 | 'playlist_mincount': 235, | |
286 | }, { | |
287 | 'url': 'https://twitcasting.tv/noriyukicas/show', | |
288 | 'only_matching': True, | |
289 | }] | |
290 | ||
291 | def _entries(self, uploader_id): | |
292 | base_url = next_url = 'https://twitcasting.tv/%s/show' % uploader_id | |
293 | for page_num in itertools.count(1): | |
294 | webpage = self._download_webpage( | |
295 | next_url, uploader_id, query={'filter': 'watchable'}, note='Downloading page %d' % page_num) | |
296 | matches = re.finditer( | |
297 | r'''(?isx)<a\s+class="tw-movie-thumbnail"\s*href="(?P<url>/[^/]+/movie/\d+)"\s*>.+?</a>''', | |
298 | webpage) | |
299 | for mobj in matches: | |
300 | yield self.url_result(urljoin(base_url, mobj.group('url'))) | |
301 | ||
302 | next_url = self._search_regex( | |
303 | r'<a href="(/%s/show/%d-\d+)[?"]' % (re.escape(uploader_id), page_num), | |
304 | webpage, 'next url', default=None) | |
305 | next_url = urljoin(base_url, next_url) | |
306 | if not next_url: | |
307 | return | |
308 | ||
309 | def _real_extract(self, url): | |
310 | uploader_id = self._match_id(url) | |
311 | return self.playlist_result( | |
312 | self._entries(uploader_id), uploader_id, '%s - Live History' % uploader_id) |