5 from .common
import InfoExtractor
6 from ..dependencies
import websockets
25 class TwitCastingIE(InfoExtractor
):
26 _VALID_URL
= r
'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<uploader_id>[^/?#]+)/(?:movie|twplayer)/(?P<id>\d+)'
28 'Origin': 'https://twitcasting.tv',
29 'Referer': 'https://twitcasting.tv/',
32 'url': 'https://twitcasting.tv/ivetesangalo/movie/2357609',
33 'md5': '745243cad58c4681dc752490f7540d7f',
37 'title': 'Live #2357609',
38 'uploader_id': 'ivetesangalo',
39 'description': 'Twitter Oficial da cantora brasileira Ivete Sangalo.',
40 'thumbnail': r
're:^https?://.*\.jpg$',
41 'upload_date': '20110822',
42 'timestamp': 1313978424,
47 'skip_download': True,
50 'url': 'https://twitcasting.tv/mttbernardini/movie/3689740',
54 'title': 'Live playing something #3689740',
55 'uploader_id': 'mttbernardini',
56 'description': 'md5:1dc7efa2f1ab932fcd119265cebeec69',
57 'thumbnail': r
're:^https?://.*\.jpg$',
58 'upload_date': '20120211',
59 'timestamp': 1328995624,
64 'skip_download': True,
65 'videopassword': 'abc',
68 'url': 'https://twitcasting.tv/loft_heaven/movie/685979292',
72 'title': '【無料配信】南波一海のhear/here “ナタリー望月哲さんに聞く編集と「渋谷系狂騒曲」”',
73 'uploader_id': 'loft_heaven',
74 'description': 'md5:3a0c7b53019df987ce545c935538bacf',
75 'upload_date': '20210604',
76 'timestamp': 1622802114,
77 'thumbnail': r
're:^https?://.*\.jpg$',
82 'skip_download': True,
86 def _parse_data_movie_playlist(self
, dmp
, video_id
):
87 # attempt 1: parse as JSON directly
89 return self
._parse
_json
(dmp
, video_id
)
90 except ExtractorError
:
92 # attempt 2: decode reversed base64
93 decoded
= base64
.b64decode(dmp
[::-1])
94 return self
._parse
_json
(decoded
, video_id
)
96 def _real_extract(self
, url
):
97 uploader_id
, video_id
= self
._match
_valid
_url
(url
).groups()
99 webpage
, urlh
= self
._download
_webpage
_handle
(url
, video_id
)
100 video_password
= self
.get_param('videopassword')
103 request_data
= urlencode_postdata({
104 'password': video_password
,
105 **self
._hidden
_inputs
(webpage
),
107 webpage
, urlh
= self
._download
_webpage
_handle
(
108 url
, video_id
, data
=request_data
,
109 headers
={'Origin': 'https://twitcasting.tv'},
110 note
='Trying video password')
111 if urlh
.url
!= url
and request_data
:
112 webpage
= self
._download
_webpage
(
113 urlh
.url
, video_id
, data
=request_data
,
114 headers
={'Origin': 'https://twitcasting.tv'},
115 note
='Retrying authentication')
116 # has to check here as the first request can contain password input form even if the password is correct
117 if re
.search(r
'<form\s+method="POST">\s*<input\s+[^>]+?name="password"', webpage
):
118 raise ExtractorError('This video is protected by a password, use the --video-password option', expected
=True)
120 title
= (clean_html(get_element_by_id('movietitle', webpage
))
121 or self
._html
_search
_meta
(['og:title', 'twitter:title'], webpage
, fatal
=True))
123 video_js_data
= try_get(
125 lambda x
: self
._parse
_data
_movie
_playlist
(self
._search
_regex
(
126 r
'data-movie-playlist=\'([^
\']+?
)\'',
127 x, 'movie playlist
', default=None), video_id)['2'], list)
129 thumbnail = traverse_obj(video_js_data, (0, 'thumbnailUrl
')) or self._og_search_thumbnail(webpage)
130 description = clean_html(get_element_by_id(
131 'authorcomment
', webpage)) or self._html_search_meta(
132 ['description
', 'og
:description
', 'twitter
:description
'], webpage)
133 duration = (try_get(video_js_data, lambda x: sum(float_or_none(y.get('duration
')) for y in x) / 1000)
134 or parse_duration(clean_html(get_element_by_class('tw
-player
-duration
-time
', webpage))))
135 view_count = str_to_int(self._search_regex(
136 (r'Total\s
*:\s
*Views\s
*([\d
,]+)', r'総視聴者\s
*:\s
*([\d
,]+)\s
*</'), webpage, 'views
', None))
137 timestamp = unified_timestamp(self._search_regex(
138 r'data
-toggle
="true"[^
>]+datetime
="([^"]+)"',
139 webpage, 'datetime', None))
141 stream_server_data = self._download_json(
142 f'https://twitcasting.tv/streamserver.php?target={uploader_id}&mode=client', video_id,
143 'Downloading live info', fatal=False)
145 is_live = any(f'data-{x}' in webpage for x in ['is-onlive="true
"', 'live-type="live
"', 'status="online
"'])
146 if not traverse_obj(stream_server_data, 'llfmp4') and is_live:
147 self.raise_login_required(method='cookies')
151 'description': description,
152 'thumbnail': thumbnail,
153 'timestamp': timestamp,
154 'uploader_id': uploader_id,
155 'duration': duration,
156 'view_count': view_count,
161 data_movie_url = self._search_regex(
162 r'data-movie-url=(["\'])(?P
<url
>(?
:(?
!\
1).)+)\
1',
163 x, 'm3u8 url
', group='url
', default=None)
165 return [data_movie_url]
167 m3u8_urls = (try_get(webpage, find_dmu, list)
168 or traverse_obj(video_js_data, (..., 'source
', 'url
'))
169 or ([f'https
://twitcasting
.tv
/{uploader_id}
/metastream
.m3u8
'] if is_live else None))
171 raise ExtractorError('Failed to get m3u8 playlist
')
174 m3u8_url = m3u8_urls[0]
175 formats = self._extract_m3u8_formats(
176 m3u8_url, video_id, ext='mp4
', m3u8_id='hls
',
177 live=True, headers=self._M3U8_HEADERS)
179 if traverse_obj(stream_server_data, ('hls
', 'source
')):
180 formats.extend(self._extract_m3u8_formats(
181 m3u8_url, video_id, ext='mp4
', m3u8_id='source
',
182 live=True, query={'mode
': 'source
'},
183 note='Downloading source quality m3u8
',
184 headers=self._M3U8_HEADERS, fatal=False))
187 qq = qualities(['base
', 'mobilesource
', 'main
'])
188 streams = traverse_obj(stream_server_data, ('llfmp4
', 'streams
')) or {}
189 for mode, ws_url in streams.items():
192 'format_id
': f'ws
-{mode}
',
195 'source_preference
': -10,
196 # TwitCasting simply sends moof atom directly over WS
197 'protocol
': 'websocket_frag
',
202 '_format_sort_fields
': ('source
', ),
204 elif len(m3u8_urls) == 1:
205 formats = self._extract_m3u8_formats(
206 m3u8_urls[0], video_id, 'mp4
', headers=self._M3U8_HEADERS)
208 # No problem here since there's only one manifest
210 'http_headers': self
._M
3U8_HEADERS
,
214 '_type': 'multi_video',
216 'id': f
'{video_id}-{num}',
219 # Requesting the manifests here will cause download to fail.
220 # So use ffmpeg instead. See: https://github.com/yt-dlp/yt-dlp/issues/382
222 'http_headers': self
._M
3U8_HEADERS
,
224 } for (num
, m3u8_url
) in enumerate(m3u8_urls
)],
234 class TwitCastingLiveIE(InfoExtractor
):
235 _VALID_URL
= r
'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<id>[^/?#]+)/?(?:[#?]|$)'
237 'url': 'https://twitcasting.tv/ivetesangalo',
238 'only_matching': True,
240 'url': 'https://twitcasting.tv/c:unusedlive',
241 'expected_exception': 'UserNotLive',
244 def _real_extract(self
, url
):
245 uploader_id
= self
._match
_id
(url
)
247 f
'Downloading live video of user {uploader_id}. '
248 f
'Pass "https://twitcasting.tv/{uploader_id}/show" to download the history')
250 is_live
= traverse_obj(self
._download
_json
(
251 f
'https://frontendapi.twitcasting.tv/watch/user/{uploader_id}',
252 uploader_id
, 'Checking live status', data
=b
'', fatal
=False), ('is_live', {bool}
))
253 if is_live
is False: # only raise here if API response was as expected
254 raise UserNotLive(video_id
=uploader_id
)
256 # Use /show/ page so that password-protected and members-only livestreams can be found
257 webpage
= self
._download
_webpage
(
258 f
'https://twitcasting.tv/{uploader_id}/show/', uploader_id
, 'Downloading live history')
259 is_live
= is_live
or self
._search
_regex
(
260 r
'(?s)(<span\s*class="tw-movie-thumbnail2-badge"\s*data-status="live">\s*LIVE)',
261 webpage
, 'is live?', default
=False)
262 # Current live is always the first match
263 current_live
= self
._search
_regex
(
264 r
'(?s)<a\s+class="tw-movie-thumbnail2"\s+href="/[^/"]+/movie/(?P<video_id>\d+)"',
265 webpage
, 'current live ID', default
=None, group
='video_id')
266 if not is_live
or not current_live
:
267 raise UserNotLive(video_id
=uploader_id
)
269 return self
.url_result(f
'https://twitcasting.tv/{uploader_id}/movie/{current_live}', TwitCastingIE
)
272 class TwitCastingUserIE(InfoExtractor
):
273 _VALID_URL
= r
'https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<id>[^/?#]+)/(?:show|archive)/?(?:[#?]|$)'
275 'url': 'https://twitcasting.tv/natsuiromatsuri/archive/',
277 'id': 'natsuiromatsuri',
278 'title': 'natsuiromatsuri - Live History',
280 'playlist_mincount': 235,
282 'url': 'https://twitcasting.tv/noriyukicas/show',
283 'only_matching': True,
286 def _entries(self
, uploader_id
):
287 base_url
= next_url
= f
'https://twitcasting.tv/{uploader_id}/show'
288 for page_num
in itertools
.count(1):
289 webpage
= self
._download
_webpage
(
290 next_url
, uploader_id
, query
={'filter': 'watchable'}, note
=f
'Downloading page {page_num}')
291 matches
= re
.finditer(
292 r
'(?s)<a\s+class="tw-movie-thumbnail2"\s+href="(?P<url>/[^/"]+/movie/\d+)"', webpage
)
294 yield self
.url_result(urljoin(base_url
, mobj
.group('url')))
296 next_url
= self
._search
_regex
(
297 r
'<a href="(/%s/show/%d-\d+)[?"]' % (re
.escape(uploader_id
), page_num
),
298 webpage
, 'next url', default
=None)
299 next_url
= urljoin(base_url
, next_url
)
303 def _real_extract(self
, url
):
304 uploader_id
= self
._match
_id
(url
)
305 return self
.playlist_result(
306 self
._entries
(uploader_id
), uploader_id
, f
'{uploader_id} - Live History')