]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/twitcasting.py
[cleanup, docs] Minor fixes
[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,
11 float_or_none,
12 get_element_by_class,
13 get_element_by_id,
14 parse_duration,
e6779b94 15 qualities,
29f7c58a 16 str_to_int,
fabb27fc 17 traverse_obj,
e85a3971 18 try_get,
29f7c58a 19 unified_timestamp,
20 urlencode_postdata,
e85a3971 21 urljoin,
22 ExtractorError,
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+)'
88b54749 28 _TESTS = [{
036f9051 29 'url': 'https://twitcasting.tv/ivetesangalo/movie/2357609',
30 'md5': '745243cad58c4681dc752490f7540d7f',
31 'info_dict': {
32 'id': '2357609',
33 'ext': 'mp4',
00a9a25c 34 'title': 'Live #2357609',
036f9051 35 'uploader_id': 'ivetesangalo',
29f7c58a 36 'description': 'Twitter Oficial da cantora brasileira Ivete Sangalo.',
036f9051 37 'thumbnail': r're:^https?://.*\.jpg$',
29f7c58a 38 'upload_date': '20110822',
39 'timestamp': 1314010824,
40 'duration': 32,
41 'view_count': int,
cf0db4d9
S
42 },
43 'params': {
44 'skip_download': True,
45 },
88b54749
MZ
46 }, {
47 'url': 'https://twitcasting.tv/mttbernardini/movie/3689740',
48 'info_dict': {
49 'id': '3689740',
50 'ext': 'mp4',
51 'title': 'Live playing something #3689740',
52 'uploader_id': 'mttbernardini',
29f7c58a 53 '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 54 'thumbnail': r're:^https?://.*\.jpg$',
29f7c58a 55 'upload_date': '20120212',
56 'timestamp': 1329028024,
57 'duration': 681,
58 'view_count': int,
88b54749
MZ
59 },
60 'params': {
61 'skip_download': True,
62 'videopassword': 'abc',
63 },
64 }]
036f9051 65
66 def _real_extract(self, url):
5ad28e7f 67 uploader_id, video_id = self._match_valid_url(url).groups()
036f9051 68
a06916d9 69 video_password = self.get_param('videopassword')
88b54749
MZ
70 request_data = None
71 if video_password:
72 request_data = urlencode_postdata({
73 'password': video_password,
74 })
d0491a1e 75 webpage = self._download_webpage(
76 url, video_id, data=request_data,
77 headers={'Origin': 'https://twitcasting.tv'})
036f9051 78
e85a3971 79 title = (clean_html(get_element_by_id('movietitle', webpage))
80 or self._html_search_meta(['og:title', 'twitter:title'], webpage, fatal=True))
cf0db4d9 81
29f7c58a 82 video_js_data = {}
cf0db4d9 83 m3u8_url = self._search_regex(
29f7c58a 84 r'data-movie-url=(["\'])(?P<url>(?:(?!\1).)+)\1',
85 webpage, 'm3u8 url', group='url', default=None)
86 if not m3u8_url:
87 video_js_data = self._parse_json(self._search_regex(
d0491a1e 88 r'data-movie-playlist=(["\'])(?P<url>(?:(?!\1).)+)',
e85a3971 89 webpage, 'movie playlist', group='url', default='[{}]'), video_id)
d0491a1e 90 if isinstance(video_js_data, dict):
91 video_js_data = list(video_js_data.values())[0]
92 video_js_data = video_js_data[0]
e85a3971 93 m3u8_url = try_get(video_js_data, lambda x: x['source']['url'])
94
e6779b94 95 stream_server_data = self._download_json(
96 'https://twitcasting.tv/streamserver.php?target=%s&mode=client' % uploader_id, video_id,
97 'Downloading live info', fatal=False)
98
e85a3971 99 is_live = 'data-status="online"' in webpage
fabb27fc
LTHD
100
101 if not traverse_obj(stream_server_data, 'llfmp4') and is_live:
ed8d87f9 102 self.raise_login_required(method='cookies')
fabb27fc 103
e6779b94 104 formats = []
e85a3971 105 if is_live and not m3u8_url:
106 m3u8_url = 'https://twitcasting.tv/%s/metastream.m3u8' % uploader_id
e6779b94 107 if is_live and has_websockets and stream_server_data:
108 qq = qualities(['base', 'mobilesource', 'main'])
fabb27fc
LTHD
109 streams = traverse_obj(stream_server_data, ('llfmp4', 'streams')) or {}
110 for mode, ws_url in streams.items():
e6779b94 111 formats.append({
112 'url': ws_url,
113 'format_id': 'ws-%s' % mode,
114 'ext': 'mp4',
115 'quality': qq(mode),
116 'protocol': 'websocket_frag', # TwitCasting simply sends moof atom directly over WS
117 })
cf0db4d9 118
29f7c58a 119 thumbnail = video_js_data.get('thumbnailUrl') or self._og_search_thumbnail(webpage)
120 description = clean_html(get_element_by_id(
121 'authorcomment', webpage)) or self._html_search_meta(
122 ['description', 'og:description', 'twitter:description'], webpage)
123 duration = float_or_none(video_js_data.get(
124 'duration'), 1000) or parse_duration(clean_html(
125 get_element_by_class('tw-player-duration-time', webpage)))
126 view_count = str_to_int(self._search_regex(
127 r'Total\s*:\s*([\d,]+)\s*Views', webpage, 'views', None))
128 timestamp = unified_timestamp(self._search_regex(
129 r'data-toggle="true"[^>]+datetime="([^"]+)"',
130 webpage, 'datetime', None))
cf0db4d9 131
e85a3971 132 if m3u8_url:
e6779b94 133 formats.extend(self._extract_m3u8_formats(
134 m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', live=is_live))
e85a3971 135 self._sort_formats(formats)
136
cf0db4d9 137 return {
036f9051 138 'id': video_id,
036f9051 139 'title': title,
140 'description': description,
141 'thumbnail': thumbnail,
29f7c58a 142 'timestamp': timestamp,
036f9051 143 'uploader_id': uploader_id,
29f7c58a 144 'duration': duration,
145 'view_count': view_count,
036f9051 146 'formats': formats,
e85a3971 147 'is_live': is_live,
036f9051 148 }
e85a3971 149
150
151class TwitCastingLiveIE(InfoExtractor):
152 _VALID_URL = r'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<id>[^/]+)/?(?:[#?]|$)'
153 _TESTS = [{
154 'url': 'https://twitcasting.tv/ivetesangalo',
155 'only_matching': True,
156 }]
157
158 def _real_extract(self, url):
159 uploader_id = self._match_id(url)
160 self.to_screen(
161 'Downloading live video of user {0}. '
162 'Pass "https://twitcasting.tv/{0}/show" to download the history'.format(uploader_id))
163
164 webpage = self._download_webpage(url, uploader_id)
165 current_live = self._search_regex(
166 (r'data-type="movie" data-id="(\d+)">',
167 r'tw-sound-flag-open-link" data-id="(\d+)" style=',),
168 webpage, 'current live ID', default=None)
169 if not current_live:
170 raise ExtractorError('The user is not currently live')
171 return self.url_result('https://twitcasting.tv/%s/movie/%s' % (uploader_id, current_live))
172
173
174class TwitCastingUserIE(InfoExtractor):
175 _VALID_URL = r'https?://(?:[^/]+\.)?twitcasting\.tv/(?P<id>[^/]+)/show/?(?:[#?]|$)'
176 _TESTS = [{
177 'url': 'https://twitcasting.tv/noriyukicas/show',
178 'only_matching': True,
179 }]
180
181 def _entries(self, uploader_id):
182 base_url = next_url = 'https://twitcasting.tv/%s/show' % uploader_id
183 for page_num in itertools.count(1):
184 webpage = self._download_webpage(
185 next_url, uploader_id, query={'filter': 'watchable'}, note='Downloading page %d' % page_num)
186 matches = re.finditer(
187 r'''(?isx)<a\s+class="tw-movie-thumbnail"\s*href="(?P<url>/[^/]+/movie/\d+)"\s*>.+?</a>''',
188 webpage)
189 for mobj in matches:
190 yield self.url_result(urljoin(base_url, mobj.group('url')))
191
192 next_url = self._search_regex(
193 r'<a href="(/%s/show/%d-\d+)[?"]' % (re.escape(uploader_id), page_num),
194 webpage, 'next url', default=None)
195 next_url = urljoin(base_url, next_url)
196 if not next_url:
197 return
198
199 def _real_extract(self, url):
200 uploader_id = self._match_id(url)
201 return self.playlist_result(
202 self._entries(uploader_id), uploader_id, '%s - Live History' % uploader_id)