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