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