]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/twitcasting.py
[murrtube] Add extractor (#2387)
[yt-dlp.git] / yt_dlp / extractor / twitcasting.py
CommitLineData
036f9051 1# coding: utf-8
2from __future__ import unicode_literals
3
e85a3971 4import itertools
036f9051 5import re
6
29f7c58a 7from .common import InfoExtractor
e6779b94 8from ..downloader.websocket import has_websockets
29f7c58a 9from ..utils import (
10 clean_html,
a4a42602 11 ExtractorError,
29f7c58a 12 float_or_none,
13 get_element_by_class,
14 get_element_by_id,
15 parse_duration,
e6779b94 16 qualities,
29f7c58a 17 str_to_int,
fabb27fc 18 traverse_obj,
e85a3971 19 try_get,
29f7c58a 20 unified_timestamp,
21 urlencode_postdata,
e85a3971 22 urljoin,
29f7c58a 23)
24
036f9051 25
cf0db4d9 26class TwitCastingIE(InfoExtractor):
e85a3971 27 _VALID_URL = r'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<uploader_id>[^/]+)/(?:movie|twplayer)/(?P<id>\d+)'
a4a42602
LTHD
28 _M3U8_HEADERS = {
29 'Origin': 'https://twitcasting.tv',
30 'Referer': 'https://twitcasting.tv/',
31 }
88b54749 32 _TESTS = [{
036f9051 33 'url': 'https://twitcasting.tv/ivetesangalo/movie/2357609',
34 'md5': '745243cad58c4681dc752490f7540d7f',
35 'info_dict': {
36 'id': '2357609',
37 'ext': 'mp4',
00a9a25c 38 'title': 'Live #2357609',
036f9051 39 'uploader_id': 'ivetesangalo',
29f7c58a 40 'description': 'Twitter Oficial da cantora brasileira Ivete Sangalo.',
036f9051 41 'thumbnail': r're:^https?://.*\.jpg$',
29f7c58a 42 'upload_date': '20110822',
43 'timestamp': 1314010824,
44 'duration': 32,
45 'view_count': int,
cf0db4d9
S
46 },
47 'params': {
48 'skip_download': True,
49 },
88b54749
MZ
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',
29f7c58a 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ù.',
88b54749 58 'thumbnail': r're:^https?://.*\.jpg$',
29f7c58a 59 'upload_date': '20120212',
60 'timestamp': 1329028024,
61 'duration': 681,
62 'view_count': int,
88b54749
MZ
63 },
64 'params': {
65 'skip_download': True,
66 'videopassword': 'abc',
67 },
a4a42602
LTHD
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,
88b54749 78 }]
036f9051 79
80 def _real_extract(self, url):
5ad28e7f 81 uploader_id, video_id = self._match_valid_url(url).groups()
036f9051 82
a06916d9 83 video_password = self.get_param('videopassword')
88b54749
MZ
84 request_data = None
85 if video_password:
86 request_data = urlencode_postdata({
87 'password': video_password,
5a13fdd2 88 }, encoding='utf-8')
d0491a1e 89 webpage = self._download_webpage(
90 url, video_id, data=request_data,
91 headers={'Origin': 'https://twitcasting.tv'})
036f9051 92
e85a3971 93 title = (clean_html(get_element_by_id('movietitle', webpage))
94 or self._html_search_meta(['og:title', 'twitter:title'], webpage, fatal=True))
cf0db4d9 95
a4a42602
LTHD
96 video_js_data = try_get(
97 webpage,
98 lambda x: self._parse_json(self._search_regex(
99 r'data-movie-playlist=\'([^\']+?)\'',
100 x, 'movie playlist', default=None), video_id)['2'], list)
fabb27fc 101
a4a42602 102 thumbnail = traverse_obj(video_js_data, (0, 'thumbnailUrl')) or self._og_search_thumbnail(webpage)
29f7c58a 103 description = clean_html(get_element_by_id(
104 'authorcomment', webpage)) or self._html_search_meta(
105 ['description', 'og:description', 'twitter:description'], webpage)
a4a42602
LTHD
106 duration = (try_get(video_js_data, lambda x: sum(float_or_none(y.get('duration')) for y in x) / 1000)
107 or parse_duration(clean_html(get_element_by_class('tw-player-duration-time', webpage))))
29f7c58a 108 view_count = str_to_int(self._search_regex(
a4a42602 109 (r'Total\s*:\s*([\d,]+)\s*Views', r'総視聴者\s*:\s*([\d,]+)\s*</'), webpage, 'views', None))
29f7c58a 110 timestamp = unified_timestamp(self._search_regex(
111 r'data-toggle="true"[^>]+datetime="([^"]+)"',
112 webpage, 'datetime', None))
cf0db4d9 113
a4a42602
LTHD
114 stream_server_data = self._download_json(
115 'https://twitcasting.tv/streamserver.php?target=%s&mode=client' % uploader_id, video_id,
116 'Downloading live info', fatal=False)
e85a3971 117
a4a42602
LTHD
118 is_live = 'data-status="online"' in webpage
119 if not traverse_obj(stream_server_data, 'llfmp4') and is_live:
120 self.raise_login_required(method='cookies')
121
122 base_dict = {
036f9051 123 'title': title,
124 'description': description,
125 'thumbnail': thumbnail,
29f7c58a 126 'timestamp': timestamp,
036f9051 127 'uploader_id': uploader_id,
29f7c58a 128 'duration': duration,
129 'view_count': view_count,
e85a3971 130 'is_live': is_live,
036f9051 131 }
e85a3971 132
a4a42602
LTHD
133 def find_dmu(x):
134 data_movie_url = self._search_regex(
135 r'data-movie-url=(["\'])(?P<url>(?:(?!\1).)+)\1',
136 x, 'm3u8 url', group='url', default=None)
137 if data_movie_url:
138 return [data_movie_url]
139
140 m3u8_urls = (try_get(webpage, find_dmu, list)
141 or traverse_obj(video_js_data, (..., 'source', 'url'))
142 or ([f'https://twitcasting.tv/{uploader_id}/metastream.m3u8'] if is_live else None))
143 if not m3u8_urls:
144 raise ExtractorError('Failed to get m3u8 playlist')
145
146 if is_live:
147 m3u8_url = m3u8_urls[0]
148 formats = self._extract_m3u8_formats(
149 m3u8_url, video_id, ext='mp4', m3u8_id='hls',
150 live=True, headers=self._M3U8_HEADERS)
151
152 formats.extend(self._extract_m3u8_formats(
153 m3u8_url, video_id, ext='mp4', m3u8_id='source',
154 live=True, query={'mode': 'source'},
155 note='Downloading source quality m3u8',
156 headers=self._M3U8_HEADERS, fatal=False))
157
158 if has_websockets:
159 qq = qualities(['base', 'mobilesource', 'main'])
160 streams = traverse_obj(stream_server_data, ('llfmp4', 'streams')) or {}
161 for mode, ws_url in streams.items():
162 formats.append({
163 'url': ws_url,
164 'format_id': 'ws-%s' % mode,
165 'ext': 'mp4',
166 'quality': qq(mode),
167 # TwitCasting simply sends moof atom directly over WS
168 'protocol': 'websocket_frag',
169 })
170
171 self._sort_formats(formats)
172
173 infodict = {
174 'formats': formats
175 }
176 else:
177 infodict = {
178 '_type': 'multi_video',
179 'entries': [{
180 'id': f'{video_id}-{num}',
181 'url': m3u8_url,
182 'ext': 'mp4',
183 # Requesting the manifests here will cause download to fail.
184 # So use ffmpeg instead. See: https://github.com/yt-dlp/yt-dlp/issues/382
185 'protocol': 'm3u8',
186 'http_headers': self._M3U8_HEADERS,
187 **base_dict,
188 } for (num, m3u8_url) in enumerate(m3u8_urls)],
189 }
190
191 return {
192 'id': video_id,
193 **base_dict,
194 **infodict,
195 }
196
e85a3971 197
198class TwitCastingLiveIE(InfoExtractor):
199 _VALID_URL = r'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<id>[^/]+)/?(?:[#?]|$)'
200 _TESTS = [{
201 'url': 'https://twitcasting.tv/ivetesangalo',
202 'only_matching': True,
203 }]
204
205 def _real_extract(self, url):
206 uploader_id = self._match_id(url)
207 self.to_screen(
208 'Downloading live video of user {0}. '
209 'Pass "https://twitcasting.tv/{0}/show" to download the history'.format(uploader_id))
210
211 webpage = self._download_webpage(url, uploader_id)
212 current_live = self._search_regex(
213 (r'data-type="movie" data-id="(\d+)">',
214 r'tw-sound-flag-open-link" data-id="(\d+)" style=',),
215 webpage, 'current live ID', default=None)
216 if not current_live:
217 raise ExtractorError('The user is not currently live')
218 return self.url_result('https://twitcasting.tv/%s/movie/%s' % (uploader_id, current_live))
219
220
221class TwitCastingUserIE(InfoExtractor):
222 _VALID_URL = r'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<id>[^/]+)/show/?(?:[#?]|$)'
223 _TESTS = [{
224 'url': 'https://twitcasting.tv/noriyukicas/show',
225 'only_matching': True,
226 }]
227
228 def _entries(self, uploader_id):
229 base_url = next_url = 'https://twitcasting.tv/%s/show' % uploader_id
230 for page_num in itertools.count(1):
231 webpage = self._download_webpage(
232 next_url, uploader_id, query={'filter': 'watchable'}, note='Downloading page %d' % page_num)
233 matches = re.finditer(
234 r'''(?isx)<a\s+class="tw-movie-thumbnail"\s*href="(?P<url>/[^/]+/movie/\d+)"\s*>.+?</a>''',
235 webpage)
236 for mobj in matches:
237 yield self.url_result(urljoin(base_url, mobj.group('url')))
238
239 next_url = self._search_regex(
240 r'<a href="(/%s/show/%d-\d+)[?"]' % (re.escape(uploader_id), page_num),
241 webpage, 'next url', default=None)
242 next_url = urljoin(base_url, next_url)
243 if not next_url:
244 return
245
246 def _real_extract(self, url):
247 uploader_id = self._match_id(url)
248 return self.playlist_result(
249 self._entries(uploader_id), uploader_id, '%s - Live History' % uploader_id)