4 from .common
import InfoExtractor
5 from ..downloader
.websocket
import has_websockets
23 class TwitCastingIE(InfoExtractor
):
24 _VALID_URL
= r
'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<uploader_id>[^/]+)/(?:movie|twplayer)/(?P<id>\d+)'
26 'Origin': 'https://twitcasting.tv',
27 'Referer': 'https://twitcasting.tv/',
30 'url': 'https://twitcasting.tv/ivetesangalo/movie/2357609',
31 'md5': '745243cad58c4681dc752490f7540d7f',
35 'title': 'Live #2357609',
36 'uploader_id': 'ivetesangalo',
37 'description': 'Twitter Oficial da cantora brasileira Ivete Sangalo.',
38 'thumbnail': r
're:^https?://.*\.jpg$',
39 'upload_date': '20110822',
40 'timestamp': 1314010824,
45 'skip_download': True,
48 'url': 'https://twitcasting.tv/mttbernardini/movie/3689740',
52 'title': 'Live playing something #3689740',
53 'uploader_id': 'mttbernardini',
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ù.',
55 'thumbnail': r
're:^https?://.*\.jpg$',
56 'upload_date': '20120212',
57 'timestamp': 1329028024,
62 'skip_download': True,
63 'videopassword': 'abc',
66 'note': 'archive is split in 2 parts',
67 'url': 'https://twitcasting.tv/loft_heaven/movie/685979292',
71 'title': '南波一海のhear_here “ナタリー望月哲さんに聞く編集と「渋谷系狂騒曲」”',
72 'duration': 6964.599334,
74 'playlist_mincount': 2,
77 def _real_extract(self
, url
):
78 uploader_id
, video_id
= self
._match
_valid
_url
(url
).groups()
80 video_password
= self
.get_param('videopassword')
83 request_data
= urlencode_postdata({
84 'password': video_password
,
86 webpage
, urlh
= self
._download
_webpage
_handle
(
87 url
, video_id
, data
=request_data
,
88 headers
={'Origin': 'https://twitcasting.tv'}
)
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')
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)
98 title
= (clean_html(get_element_by_id('movietitle', webpage
))
99 or self
._html
_search
_meta
(['og:title', 'twitter:title'], webpage
, fatal
=True))
101 video_js_data
= try_get(
103 lambda x
: self
._parse
_json
(self
._search
_regex
(
104 r
'data-movie-playlist=\'([^
\']+?
)\'',
105 x, 'movie playlist
', default=None), video_id)['2'], list)
107 thumbnail = traverse_obj(video_js_data, (0, 'thumbnailUrl
')) or self._og_search_thumbnail(webpage)
108 description = clean_html(get_element_by_id(
109 'authorcomment
', webpage)) or self._html_search_meta(
110 ['description
', 'og
:description
', 'twitter
:description
'], webpage)
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))))
113 view_count = str_to_int(self._search_regex(
114 (r'Total\s
*:\s
*([\d
,]+)\s
*Views
', r'総視聴者\s
*:\s
*([\d
,]+)\s
*</'), webpage, 'views
', None))
115 timestamp = unified_timestamp(self._search_regex(
116 r'data
-toggle
="true"[^
>]+datetime
="([^"]+)"',
117 webpage, 'datetime', None))
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)
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')
129 'description': description,
130 'thumbnail': thumbnail,
131 'timestamp': timestamp,
132 'uploader_id': uploader_id,
133 'duration': duration,
134 'view_count': view_count,
139 data_movie_url = self._search_regex(
140 r'data-movie-url=(["\'])(?P
<url
>(?
:(?
!\
1).)+)\
1',
141 x, 'm3u8 url
', group='url
', default=None)
143 return [data_movie_url]
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))
149 raise ExtractorError('Failed to get m3u8 playlist
')
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)
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))
165 qq = qualities(['base
', 'mobilesource
', 'main
'])
166 streams = traverse_obj(stream_server_data, ('llfmp4
', 'streams
')) or {}
167 for mode, ws_url in streams.items():
170 'format_id
': 'ws
-%s' % mode,
173 'source_preference
': -10,
174 # TwitCasting simply sends moof atom directly over WS
175 'protocol
': 'websocket_frag
',
178 self._sort_formats(formats, ('source
',))
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)
188 # No problem here since there's only one manifest
193 '_type': 'multi_video',
195 'id': f
'{video_id}-{num}',
198 # Requesting the manifests here will cause download to fail.
199 # So use ffmpeg instead. See: https://github.com/yt-dlp/yt-dlp/issues/382
201 'http_headers': self
._M
3U8_HEADERS
,
203 } for (num
, m3u8_url
) in enumerate(m3u8_urls
)],
213 class TwitCastingLiveIE(InfoExtractor
):
214 _VALID_URL
= r
'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<id>[^/]+)/?(?:[#?]|$)'
216 'url': 'https://twitcasting.tv/ivetesangalo',
217 'only_matching': True,
220 def _real_extract(self
, url
):
221 uploader_id
= self
._match
_id
(url
)
223 'Downloading live video of user {0}. '
224 'Pass "https://twitcasting.tv/{0}/show" to download the history'.format(uploader_id
))
226 webpage
= self
._download
_webpage
(url
, uploader_id
)
227 current_live
= self
._search
_regex
(
228 (r
'data-type="movie" data-id="(\d+)">',
229 r
'tw-sound-flag-open-link" data-id="(\d+)" style=',),
230 webpage
, 'current live ID', default
=None)
232 # fetch unfiltered /show to find running livestreams; we can't get ID of the password-protected livestream above
233 webpage
= self
._download
_webpage
(
234 f
'https://twitcasting.tv/{uploader_id}/show/', uploader_id
,
235 note
='Downloading live history')
236 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 # get the first live; running live is always at the first
239 current_live
= self
._search
_regex
(
240 r
'(?s)<a\s+class="tw-movie-thumbnail"\s*href="/[^/]+/movie/(?P<video_id>\d+)"\s*>.+?</a>',
241 webpage
, 'current live ID 2', default
=None, group
='video_id')
243 raise ExtractorError('The user is not currently live')
244 return self
.url_result('https://twitcasting.tv/%s/movie/%s' % (uploader_id
, current_live
))
247 class TwitCastingUserIE(InfoExtractor
):
248 _VALID_URL
= r
'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<id>[^/]+)/show/?(?:[#?]|$)'
250 'url': 'https://twitcasting.tv/noriyukicas/show',
251 'only_matching': True,
254 def _entries(self
, uploader_id
):
255 base_url
= next_url
= 'https://twitcasting.tv/%s/show' % uploader_id
256 for page_num
in itertools
.count(1):
257 webpage
= self
._download
_webpage
(
258 next_url
, uploader_id
, query
={'filter': 'watchable'}
, note
='Downloading page %d' % page_num
)
259 matches
= re
.finditer(
260 r
'''(?isx)<a\s+class="tw-movie-thumbnail"\s*href="(?P<url>/[^/]+/movie/\d+)"\s*>.+?</a>''',
263 yield self
.url_result(urljoin(base_url
, mobj
.group('url')))
265 next_url
= self
._search
_regex
(
266 r
'<a href="(/%s/show/%d-\d+)[?"]' % (re
.escape(uploader_id
), page_num
),
267 webpage
, 'next url', default
=None)
268 next_url
= urljoin(base_url
, next_url
)
272 def _real_extract(self
, url
):
273 uploader_id
= self
._match
_id
(url
)
274 return self
.playlist_result(
275 self
._entries
(uploader_id
), uploader_id
, '%s - Live History' % uploader_id
)