5 from .common
import InfoExtractor
6 from ..dependencies
import websockets
26 class TwitCastingIE(InfoExtractor
):
27 _VALID_URL
= r
'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<uploader_id>[^/?#]+)/(?:movie|twplayer)/(?P<id>\d+)'
29 'Origin': 'https://twitcasting.tv',
30 'Referer': 'https://twitcasting.tv/',
33 'url': 'https://twitcasting.tv/ivetesangalo/movie/2357609',
34 'md5': '745243cad58c4681dc752490f7540d7f',
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,
48 'skip_download': True,
51 'url': 'https://twitcasting.tv/mttbernardini/movie/3689740',
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,
65 'skip_download': True,
66 'videopassword': 'abc',
69 'url': 'https://twitcasting.tv/loft_heaven/movie/685979292',
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$',
83 'skip_download': True,
87 def _parse_data_movie_playlist(self
, dmp
, video_id
):
88 # attempt 1: parse as JSON directly
90 return self
._parse
_json
(dmp
, video_id
)
91 except ExtractorError
:
93 # attempt 2: decode reversed base64
94 decoded
= base64
.b64decode(dmp
[::-1])
95 return self
._parse
_json
(decoded
, video_id
)
97 def _real_extract(self
, url
):
98 uploader_id
, video_id
= self
._match
_valid
_url
(url
).groups()
100 webpage
, urlh
= self
._download
_webpage
_handle
(url
, video_id
)
101 video_password
= self
.get_param('videopassword')
104 request_data
= urlencode_postdata({
105 'password': video_password
,
106 **self
._hidden
_inputs
(webpage
),
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)
121 title
= (clean_html(get_element_by_id('movietitle', webpage
))
122 or self
._html
_search
_meta
(['og:title', 'twitter:title'], webpage
, fatal
=True))
124 video_js_data
= try_get(
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)
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))
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)
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')
152 'description': description,
153 'thumbnail': thumbnail,
154 'timestamp': timestamp,
155 'uploader_id': uploader_id,
156 'duration': duration,
157 'view_count': view_count,
162 data_movie_url = self._search_regex(
163 r'data-movie-url=(["\'])(?P
<url
>(?
:(?
!\
1).)+)\
1',
164 x, 'm3u8 url
', group='url
', default=None)
166 return [data_movie_url]
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))
172 raise ExtractorError('Failed to get m3u8 playlist
')
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)
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))
188 qq = qualities(['base
', 'mobilesource
', 'main
'])
189 streams = traverse_obj(stream_server_data, ('llfmp4
', 'streams
')) or {}
190 for mode, ws_url in streams.items():
193 'format_id
': 'ws
-%s' % mode,
196 'source_preference
': -10,
197 # TwitCasting simply sends moof atom directly over WS
198 'protocol
': 'websocket_frag
',
203 '_format_sort_fields
': ('source
', ),
205 elif len(m3u8_urls) == 1:
206 formats = self._extract_m3u8_formats(
207 m3u8_urls[0], video_id, 'mp4
', headers=self._M3U8_HEADERS)
209 # No problem here since there's only one manifest
211 'http_headers': self
._M
3U8_HEADERS
,
215 '_type': 'multi_video',
217 'id': f
'{video_id}-{num}',
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
223 'http_headers': self
._M
3U8_HEADERS
,
225 } for (num
, m3u8_url
) in enumerate(m3u8_urls
)],
235 class TwitCastingLiveIE(InfoExtractor
):
236 _VALID_URL
= r
'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<id>[^/?#]+)/?(?:[#?]|$)'
238 'url': 'https://twitcasting.tv/ivetesangalo',
239 'only_matching': True,
241 'url': 'https://twitcasting.tv/c:unusedlive',
242 'expected_exception': 'UserNotLive',
245 _PROTECTED_LIVE_RE
= r
'(?s)(<span\s*class="tw-movie-thumbnail2-badge"\s*data-status="live">\s*LIVE)'
247 def _real_extract(self
, url
):
248 uploader_id
= self
._match
_id
(url
)
250 'Downloading live video of user {0}. '
251 'Pass "https://twitcasting.tv/{0}/show" to download the history'.format(uploader_id
))
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)
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')
273 raise UserNotLive(video_id
=uploader_id
)
274 return self
.url_result(f
'https://twitcasting.tv/{uploader_id}/movie/{current_live}', TwitCastingIE
)
277 class TwitCastingUserIE(InfoExtractor
):
278 _VALID_URL
= r
'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<id>[^/?#]+)/(:?show|archive)/?(?:[#?]|$)'
280 'url': 'https://twitcasting.tv/natsuiromatsuri/archive/',
282 'id': 'natsuiromatsuri',
283 'title': 'natsuiromatsuri - Live History',
285 'playlist_mincount': 235,
287 'url': 'https://twitcasting.tv/noriyukicas/show',
288 'only_matching': True,
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>''',
300 yield self
.url_result(urljoin(base_url
, mobj
.group('url')))
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
)
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
)