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