]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/twitcasting.py
[twitcasting] Add fallback for finding running live (#2803)
[yt-dlp.git] / yt_dlp / extractor / twitcasting.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5 import re
6
7 from .common import InfoExtractor
8 from ..downloader.websocket import has_websockets
9 from ..utils import (
10 clean_html,
11 ExtractorError,
12 float_or_none,
13 get_element_by_class,
14 get_element_by_id,
15 parse_duration,
16 qualities,
17 str_to_int,
18 traverse_obj,
19 try_get,
20 unified_timestamp,
21 urlencode_postdata,
22 urljoin,
23 )
24
25
26 class TwitCastingIE(InfoExtractor):
27 _VALID_URL = r'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<uploader_id>[^/]+)/(?:movie|twplayer)/(?P<id>\d+)'
28 _M3U8_HEADERS = {
29 'Origin': 'https://twitcasting.tv',
30 'Referer': 'https://twitcasting.tv/',
31 }
32 _TESTS = [{
33 'url': 'https://twitcasting.tv/ivetesangalo/movie/2357609',
34 'md5': '745243cad58c4681dc752490f7540d7f',
35 'info_dict': {
36 'id': '2357609',
37 'ext': 'mp4',
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': 1314010824,
44 'duration': 32,
45 'view_count': int,
46 },
47 'params': {
48 'skip_download': True,
49 },
50 }, {
51 'url': 'https://twitcasting.tv/mttbernardini/movie/3689740',
52 'info_dict': {
53 'id': '3689740',
54 'ext': 'mp4',
55 'title': 'Live playing something #3689740',
56 'uploader_id': 'mttbernardini',
57 '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ù.',
58 'thumbnail': r're:^https?://.*\.jpg$',
59 'upload_date': '20120212',
60 'timestamp': 1329028024,
61 'duration': 681,
62 'view_count': int,
63 },
64 'params': {
65 'skip_download': True,
66 'videopassword': 'abc',
67 },
68 }, {
69 'note': 'archive is split in 2 parts',
70 'url': 'https://twitcasting.tv/loft_heaven/movie/685979292',
71 'info_dict': {
72 'id': '685979292',
73 'ext': 'mp4',
74 'title': '南波一海のhear_here “ナタリー望月哲さんに聞く編集と「渋谷系狂騒曲」”',
75 'duration': 6964.599334,
76 },
77 'playlist_mincount': 2,
78 }]
79
80 def _real_extract(self, url):
81 uploader_id, video_id = self._match_valid_url(url).groups()
82
83 video_password = self.get_param('videopassword')
84 request_data = None
85 if video_password:
86 request_data = urlencode_postdata({
87 'password': video_password,
88 }, encoding='utf-8')
89 webpage, urlh = self._download_webpage_handle(
90 url, video_id, data=request_data,
91 headers={'Origin': 'https://twitcasting.tv'})
92 if urlh.geturl() != url and request_data:
93 webpage = self._download_webpage(
94 urlh.geturl(), video_id, data=request_data,
95 headers={'Origin': 'https://twitcasting.tv'},
96 note='Retrying authentication')
97
98 title = (clean_html(get_element_by_id('movietitle', webpage))
99 or self._html_search_meta(['og:title', 'twitter:title'], webpage, fatal=True))
100
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)
106
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))
118
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)
122
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 = {
128 'title': title,
129 'description': description,
130 'thumbnail': thumbnail,
131 'timestamp': timestamp,
132 'uploader_id': uploader_id,
133 'duration': duration,
134 'view_count': view_count,
135 'is_live': is_live,
136 }
137
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
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))
163
164 if has_websockets:
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),
173 'source_preference': -10,
174 # TwitCasting simply sends moof atom directly over WS
175 'protocol': 'websocket_frag',
176 })
177
178 self._sort_formats(formats, ('source',))
179
180 infodict = {
181 'formats': formats
182 }
183 else:
184 infodict = {
185 '_type': 'multi_video',
186 'entries': [{
187 'id': f'{video_id}-{num}',
188 'url': m3u8_url,
189 'ext': 'mp4',
190 # Requesting the manifests here will cause download to fail.
191 # So use ffmpeg instead. See: https://github.com/yt-dlp/yt-dlp/issues/382
192 'protocol': 'm3u8',
193 'http_headers': self._M3U8_HEADERS,
194 **base_dict,
195 } for (num, m3u8_url) in enumerate(m3u8_urls)],
196 }
197
198 return {
199 'id': video_id,
200 **base_dict,
201 **infodict,
202 }
203
204
205 class TwitCastingLiveIE(InfoExtractor):
206 _VALID_URL = r'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<id>[^/]+)/?(?:[#?]|$)'
207 _TESTS = [{
208 'url': 'https://twitcasting.tv/ivetesangalo',
209 'only_matching': True,
210 }]
211
212 def _real_extract(self, url):
213 uploader_id = self._match_id(url)
214 self.to_screen(
215 'Downloading live video of user {0}. '
216 'Pass "https://twitcasting.tv/{0}/show" to download the history'.format(uploader_id))
217
218 webpage = self._download_webpage(url, uploader_id)
219 current_live = self._search_regex(
220 (r'data-type="movie" data-id="(\d+)">',
221 r'tw-sound-flag-open-link" data-id="(\d+)" style=',),
222 webpage, 'current live ID', default=None)
223 if not current_live:
224 # fetch unfiltered /show to find running livestreams; we can't get ID of the password-protected livestream above
225 webpage = self._download_webpage(
226 f'https://twitcasting.tv/{uploader_id}/show/', uploader_id,
227 note='Downloading live history')
228 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)
229 if is_live:
230 # get the first live; running live is always at the first
231 current_live = self._search_regex(
232 r'(?s)<a\s+class="tw-movie-thumbnail"\s*href="/[^/]+/movie/(?P<video_id>\d+)"\s*>.+?</a>',
233 webpage, 'current live ID 2', default=None, group='video_id')
234 if not current_live:
235 raise ExtractorError('The user is not currently live')
236 return self.url_result('https://twitcasting.tv/%s/movie/%s' % (uploader_id, current_live))
237
238
239 class TwitCastingUserIE(InfoExtractor):
240 _VALID_URL = r'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<id>[^/]+)/show/?(?:[#?]|$)'
241 _TESTS = [{
242 'url': 'https://twitcasting.tv/noriyukicas/show',
243 'only_matching': True,
244 }]
245
246 def _entries(self, uploader_id):
247 base_url = next_url = 'https://twitcasting.tv/%s/show' % uploader_id
248 for page_num in itertools.count(1):
249 webpage = self._download_webpage(
250 next_url, uploader_id, query={'filter': 'watchable'}, note='Downloading page %d' % page_num)
251 matches = re.finditer(
252 r'''(?isx)<a\s+class="tw-movie-thumbnail"\s*href="(?P<url>/[^/]+/movie/\d+)"\s*>.+?</a>''',
253 webpage)
254 for mobj in matches:
255 yield self.url_result(urljoin(base_url, mobj.group('url')))
256
257 next_url = self._search_regex(
258 r'<a href="(/%s/show/%d-\d+)[?"]' % (re.escape(uploader_id), page_num),
259 webpage, 'next url', default=None)
260 next_url = urljoin(base_url, next_url)
261 if not next_url:
262 return
263
264 def _real_extract(self, url):
265 uploader_id = self._match_id(url)
266 return self.playlist_result(
267 self._entries(uploader_id), uploader_id, '%s - Live History' % uploader_id)