]>
Commit | Line | Data |
---|---|---|
da9a60ca | 1 | import base64 |
e85a3971 | 2 | import itertools |
036f9051 | 3 | import re |
4 | ||
29f7c58a | 5 | from .common import InfoExtractor |
9b8ee23b | 6 | from ..dependencies import websockets |
29f7c58a | 7 | from ..utils import ( |
a4a42602 | 8 | ExtractorError, |
c1d71d0d AW |
9 | UserNotLive, |
10 | clean_html, | |
29f7c58a | 11 | float_or_none, |
12 | get_element_by_class, | |
13 | get_element_by_id, | |
2325d03a | 14 | int_or_none, |
29f7c58a | 15 | parse_duration, |
e6779b94 | 16 | qualities, |
29f7c58a | 17 | str_to_int, |
fabb27fc | 18 | traverse_obj, |
e85a3971 | 19 | try_get, |
29f7c58a | 20 | unified_timestamp, |
21 | urlencode_postdata, | |
e85a3971 | 22 | urljoin, |
29f7c58a | 23 | ) |
24 | ||
036f9051 | 25 | |
cf0db4d9 | 26 | class TwitCastingIE(InfoExtractor): |
cebbd33b | 27 | _VALID_URL = r'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<uploader_id>[^/?#]+)/(?:movie|twplayer)/(?P<id>\d+)' |
a4a42602 LTHD |
28 | _M3U8_HEADERS = { |
29 | 'Origin': 'https://twitcasting.tv', | |
30 | 'Referer': 'https://twitcasting.tv/', | |
31 | } | |
88b54749 | 32 | _TESTS = [{ |
036f9051 | 33 | 'url': 'https://twitcasting.tv/ivetesangalo/movie/2357609', |
34 | 'md5': '745243cad58c4681dc752490f7540d7f', | |
35 | 'info_dict': { | |
36 | 'id': '2357609', | |
37 | 'ext': 'mp4', | |
00a9a25c | 38 | 'title': 'Live #2357609', |
036f9051 | 39 | 'uploader_id': 'ivetesangalo', |
29f7c58a | 40 | 'description': 'Twitter Oficial da cantora brasileira Ivete Sangalo.', |
036f9051 | 41 | 'thumbnail': r're:^https?://.*\.jpg$', |
29f7c58a | 42 | 'upload_date': '20110822', |
9a9006ba | 43 | 'timestamp': 1313978424, |
29f7c58a | 44 | 'duration': 32, |
45 | 'view_count': int, | |
cf0db4d9 S |
46 | }, |
47 | 'params': { | |
48 | 'skip_download': True, | |
49 | }, | |
88b54749 MZ |
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', | |
9a9006ba | 57 | 'description': 'md5:1dc7efa2f1ab932fcd119265cebeec69', |
88b54749 | 58 | 'thumbnail': r're:^https?://.*\.jpg$', |
9a9006ba S |
59 | 'upload_date': '20120211', |
60 | 'timestamp': 1328995624, | |
29f7c58a | 61 | 'duration': 681, |
62 | 'view_count': int, | |
88b54749 MZ |
63 | }, |
64 | 'params': { | |
65 | 'skip_download': True, | |
66 | 'videopassword': 'abc', | |
67 | }, | |
a4a42602 | 68 | }, { |
a4a42602 LTHD |
69 | 'url': 'https://twitcasting.tv/loft_heaven/movie/685979292', |
70 | 'info_dict': { | |
71 | 'id': '685979292', | |
72 | 'ext': 'mp4', | |
9a9006ba S |
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, | |
a4a42602 | 84 | }, |
88b54749 | 85 | }] |
036f9051 | 86 | |
da9a60ca L |
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 | ||
036f9051 | 97 | def _real_extract(self, url): |
5ad28e7f | 98 | uploader_id, video_id = self._match_valid_url(url).groups() |
036f9051 | 99 | |
9a9006ba | 100 | webpage, urlh = self._download_webpage_handle(url, video_id) |
a06916d9 | 101 | video_password = self.get_param('videopassword') |
88b54749 MZ |
102 | request_data = None |
103 | if video_password: | |
104 | request_data = urlencode_postdata({ | |
105 | 'password': video_password, | |
9a9006ba | 106 | **self._hidden_inputs(webpage), |
5a13fdd2 | 107 | }, encoding='utf-8') |
9a9006ba S |
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') | |
3d2623a8 | 112 | if urlh.url != url and request_data: |
df635a09 | 113 | webpage = self._download_webpage( |
3d2623a8 | 114 | urlh.url, video_id, data=request_data, |
df635a09 LNO |
115 | headers={'Origin': 'https://twitcasting.tv'}, |
116 | note='Retrying authentication') | |
f099df14 LNO |
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) | |
036f9051 | 120 | |
e85a3971 | 121 | title = (clean_html(get_element_by_id('movietitle', webpage)) |
122 | or self._html_search_meta(['og:title', 'twitter:title'], webpage, fatal=True)) | |
cf0db4d9 | 123 | |
a4a42602 LTHD |
124 | video_js_data = try_get( |
125 | webpage, | |
da9a60ca | 126 | lambda x: self._parse_data_movie_playlist(self._search_regex( |
a4a42602 LTHD |
127 | r'data-movie-playlist=\'([^\']+?)\'', |
128 | x, 'movie playlist', default=None), video_id)['2'], list) | |
fabb27fc | 129 | |
a4a42602 | 130 | thumbnail = traverse_obj(video_js_data, (0, 'thumbnailUrl')) or self._og_search_thumbnail(webpage) |
29f7c58a | 131 | description = clean_html(get_element_by_id( |
132 | 'authorcomment', webpage)) or self._html_search_meta( | |
133 | ['description', 'og:description', 'twitter:description'], webpage) | |
a4a42602 LTHD |
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)))) | |
29f7c58a | 136 | view_count = str_to_int(self._search_regex( |
9a9006ba | 137 | (r'Total\s*:\s*Views\s*([\d,]+)', r'総視聴者\s*:\s*([\d,]+)\s*</'), webpage, 'views', None)) |
29f7c58a | 138 | timestamp = unified_timestamp(self._search_regex( |
139 | r'data-toggle="true"[^>]+datetime="([^"]+)"', | |
140 | webpage, 'datetime', None)) | |
cf0db4d9 | 141 | |
a4a42602 LTHD |
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) | |
e85a3971 | 145 | |
7b8b1cf5 | 146 | is_live = any(f'data-{x}' in webpage for x in ['is-onlive="true"', 'live-type="live"', 'status="online"']) |
a4a42602 LTHD |
147 | if not traverse_obj(stream_server_data, 'llfmp4') and is_live: |
148 | self.raise_login_required(method='cookies') | |
149 | ||
150 | base_dict = { | |
036f9051 | 151 | 'title': title, |
152 | 'description': description, | |
153 | 'thumbnail': thumbnail, | |
29f7c58a | 154 | 'timestamp': timestamp, |
036f9051 | 155 | 'uploader_id': uploader_id, |
29f7c58a | 156 | 'duration': duration, |
157 | 'view_count': view_count, | |
e85a3971 | 158 | 'is_live': is_live, |
036f9051 | 159 | } |
e85a3971 | 160 | |
a4a42602 LTHD |
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 | ||
df635a09 LNO |
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)) | |
a4a42602 | 186 | |
9b8ee23b | 187 | if websockets: |
a4a42602 LTHD |
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), | |
df635a09 | 196 | 'source_preference': -10, |
a4a42602 LTHD |
197 | # TwitCasting simply sends moof atom directly over WS |
198 | 'protocol': 'websocket_frag', | |
199 | }) | |
200 | ||
a4a42602 | 201 | infodict = { |
9f14daf2 | 202 | 'formats': formats, |
203 | '_format_sort_fields': ('source', ), | |
a4a42602 | 204 | } |
06b1628d LNO |
205 | elif len(m3u8_urls) == 1: |
206 | formats = self._extract_m3u8_formats( | |
207 | m3u8_urls[0], video_id, 'mp4', headers=self._M3U8_HEADERS) | |
06b1628d LNO |
208 | infodict = { |
209 | # No problem here since there's only one manifest | |
210 | 'formats': formats, | |
997378f9 | 211 | 'http_headers': self._M3U8_HEADERS, |
06b1628d | 212 | } |
a4a42602 LTHD |
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 | ||
e85a3971 | 234 | |
235 | class TwitCastingLiveIE(InfoExtractor): | |
cebbd33b | 236 | _VALID_URL = r'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<id>[^/?#]+)/?(?:[#?]|$)' |
e85a3971 | 237 | _TESTS = [{ |
238 | 'url': 'https://twitcasting.tv/ivetesangalo', | |
239 | 'only_matching': True, | |
c1d71d0d AW |
240 | }, { |
241 | 'url': 'https://twitcasting.tv/c:unusedlive', | |
242 | 'expected_exception': 'UserNotLive', | |
e85a3971 | 243 | }] |
244 | ||
2325d03a JC |
245 | _PROTECTED_LIVE_RE = r'(?s)(<span\s*class="tw-movie-thumbnail2-badge"\s*data-status="live">\s*LIVE)' |
246 | ||
e85a3971 | 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) | |
2325d03a JC |
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: | |
07ea0014 LNO |
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') | |
2325d03a | 266 | is_live = self._search_regex(self._PROTECTED_LIVE_RE, webpage, 'is live?', default=None) |
07ea0014 LNO |
267 | if is_live: |
268 | # get the first live; running live is always at the first | |
269 | current_live = self._search_regex( | |
2325d03a | 270 | r'(?s)<a\s+class="tw-movie-thumbnail2"\s*href="/[^/]+/movie/(?P<video_id>\d+)"\s*>.+?</a>', |
07ea0014 | 271 | webpage, 'current live ID 2', default=None, group='video_id') |
e85a3971 | 272 | if not current_live: |
c1d71d0d | 273 | raise UserNotLive(video_id=uploader_id) |
2325d03a | 274 | return self.url_result(f'https://twitcasting.tv/{uploader_id}/movie/{current_live}', TwitCastingIE) |
e85a3971 | 275 | |
276 | ||
277 | class TwitCastingUserIE(InfoExtractor): | |
cebbd33b | 278 | _VALID_URL = r'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<id>[^/?#]+)/(:?show|archive)/?(?:[#?]|$)' |
e85a3971 | 279 | _TESTS = [{ |
cebbd33b | 280 | 'url': 'https://twitcasting.tv/natsuiromatsuri/archive/', |
281 | 'info_dict': { | |
282 | 'id': 'natsuiromatsuri', | |
283 | 'title': 'natsuiromatsuri - Live History', | |
284 | }, | |
285 | 'playlist_mincount': 235, | |
286 | }, { | |
e85a3971 | 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) |