]>
Commit | Line | Data |
---|---|---|
c14af7a7 | 1 | import functools |
c14af7a7 | 2 | import hashlib |
21b9413c | 3 | import json |
0a5d7c39 | 4 | import time |
e897bd82 | 5 | import urllib.parse |
001a5fd3 YCH |
6 | |
7 | from .common import InfoExtractor | |
caf0f5f8 | 8 | from ..utils import ( |
95a383be | 9 | ExtractorError, |
c14af7a7 | 10 | OnDemandPagedList, |
caf0f5f8 | 11 | int_or_none, |
0a5d7c39 | 12 | jwt_decode_hs256, |
caf0f5f8 | 13 | mimetype2ext, |
56793f74 | 14 | qualities, |
c14af7a7 | 15 | traverse_obj, |
0a5d7c39 | 16 | try_call, |
c14af7a7 | 17 | unified_timestamp, |
caf0f5f8 | 18 | ) |
001a5fd3 YCH |
19 | |
20 | ||
21b9413c | 21 | class IwaraBaseIE(InfoExtractor): |
0a5d7c39 | 22 | _NETRC_MACHINE = 'iwara' |
21b9413c | 23 | _USERTOKEN = None |
24 | _MEDIATOKEN = None | |
21b9413c | 25 | |
0a5d7c39 | 26 | def _is_token_expired(self, token, token_type): |
27 | # User token TTL == ~3 weeks, Media token TTL == ~1 hour | |
28 | if (try_call(lambda: jwt_decode_hs256(token)['exp']) or 0) <= int(time.time() - 120): | |
29 | self.to_screen(f'{token_type} token has expired') | |
30 | return True | |
21b9413c | 31 | |
0a5d7c39 | 32 | def _get_user_token(self): |
21b9413c | 33 | username, password = self._get_login_info() |
0a5d7c39 | 34 | if not username or not password: |
35 | return | |
36 | ||
37 | user_token = IwaraBaseIE._USERTOKEN or self.cache.load(self._NETRC_MACHINE, username) | |
38 | if not user_token or self._is_token_expired(user_token, 'User'): | |
39 | response = self._download_json( | |
21b9413c | 40 | 'https://api.iwara.tv/user/login', None, note='Logging in', |
0a5d7c39 | 41 | headers={'Content-Type': 'application/json'}, data=json.dumps({ |
21b9413c | 42 | 'email': username, |
add96eb9 | 43 | 'password': password, |
0a5d7c39 | 44 | }).encode(), expected_status=lambda x: True) |
45 | user_token = traverse_obj(response, ('token', {str})) | |
46 | if not user_token: | |
47 | error = traverse_obj(response, ('message', {str})) | |
48 | if 'invalidLogin' in error: | |
49 | raise ExtractorError('Invalid login credentials', expected=True) | |
50 | else: | |
51 | raise ExtractorError(f'Iwara API said: {error or "nothing"}') | |
52 | ||
53 | self.cache.store(self._NETRC_MACHINE, username, user_token) | |
54 | ||
55 | IwaraBaseIE._USERTOKEN = user_token | |
56 | ||
57 | def _get_media_token(self): | |
58 | self._get_user_token() | |
59 | if not IwaraBaseIE._USERTOKEN: | |
60 | return # user has not passed credentials | |
61 | ||
62 | if not IwaraBaseIE._MEDIATOKEN or self._is_token_expired(IwaraBaseIE._MEDIATOKEN, 'Media'): | |
63 | IwaraBaseIE._MEDIATOKEN = self._download_json( | |
64 | 'https://api.iwara.tv/user/token', None, note='Fetching media token', | |
65 | data=b'', headers={ | |
66 | 'Authorization': f'Bearer {IwaraBaseIE._USERTOKEN}', | |
add96eb9 | 67 | 'Content-Type': 'application/json', |
0a5d7c39 | 68 | })['accessToken'] |
21b9413c | 69 | |
0a5d7c39 | 70 | return {'Authorization': f'Bearer {IwaraBaseIE._MEDIATOKEN}'} |
21b9413c | 71 | |
0a5d7c39 | 72 | def _perform_login(self, username, password): |
73 | self._get_media_token() | |
21b9413c | 74 | |
75 | ||
76 | class IwaraIE(IwaraBaseIE): | |
c14af7a7 | 77 | IE_NAME = 'iwara' |
ab92d865 | 78 | _VALID_URL = r'https?://(?:www\.|ecchi\.)?iwara\.tv/videos?/(?P<id>[a-zA-Z0-9]+)' |
001a5fd3 | 79 | _TESTS = [{ |
c14af7a7 | 80 | 'url': 'https://www.iwara.tv/video/k2ayoueezfkx6gvq', |
001a5fd3 | 81 | 'info_dict': { |
c14af7a7 | 82 | 'id': 'k2ayoueezfkx6gvq', |
001a5fd3 | 83 | 'ext': 'mp4', |
001a5fd3 | 84 | 'age_limit': 18, |
c14af7a7 L |
85 | 'title': 'Defeat of Irybelda - アイリベルダの敗北', |
86 | 'description': 'md5:70278abebe706647a8b4cb04cf23e0d3', | |
87 | 'uploader': 'Inwerwm', | |
88 | 'uploader_id': 'inwerwm', | |
89 | 'tags': 'count:1', | |
90 | 'like_count': 6133, | |
91 | 'view_count': 1050343, | |
92 | 'comment_count': 1, | |
93 | 'timestamp': 1677843869, | |
94 | 'modified_timestamp': 1679056362, | |
001a5fd3 | 95 | }, |
0a5d7c39 | 96 | 'skip': 'this video cannot be played because of migration', |
001a5fd3 | 97 | }, { |
c14af7a7 | 98 | 'url': 'https://iwara.tv/video/1ywe1sbkqwumpdxz5/', |
0a5d7c39 | 99 | 'md5': '7645f966f069b8ec9210efd9130c9aad', |
001a5fd3 | 100 | 'info_dict': { |
c14af7a7 | 101 | 'id': '1ywe1sbkqwumpdxz5', |
001a5fd3 | 102 | 'ext': 'mp4', |
001a5fd3 | 103 | 'age_limit': 18, |
0a5d7c39 | 104 | 'title': 'Aponia アポニア SEX Party Tonight 手の脱衣 巨乳 ', |
105 | 'description': 'md5:3f60016fff22060eef1ef26d430b1f67', | |
106 | 'uploader': 'Lyu ya', | |
c14af7a7 L |
107 | 'uploader_id': 'user792540', |
108 | 'tags': [ | |
add96eb9 | 109 | 'uncategorized', |
c14af7a7 | 110 | ], |
0a5d7c39 | 111 | 'like_count': int, |
112 | 'view_count': int, | |
113 | 'comment_count': int, | |
c14af7a7 | 114 | 'timestamp': 1678732213, |
0a5d7c39 | 115 | 'modified_timestamp': int, |
116 | 'thumbnail': 'https://files.iwara.tv/image/thumbnail/581d12b5-46f4-4f15-beb2-cfe2cde5d13d/thumbnail-00.jpg', | |
117 | 'modified_date': '20230614', | |
118 | 'upload_date': '20230313', | |
001a5fd3 | 119 | }, |
21b9413c | 120 | }, { |
121 | 'url': 'https://iwara.tv/video/blggmfno8ghl725bg', | |
122 | 'info_dict': { | |
123 | 'id': 'blggmfno8ghl725bg', | |
124 | 'ext': 'mp4', | |
125 | 'age_limit': 18, | |
126 | 'title': 'お外でおしっこしちゃう猫耳ロリメイド', | |
127 | 'description': 'md5:0342ba9bf6db09edbbb28729657c3611', | |
128 | 'uploader': 'Fe_Kurosabi', | |
129 | 'uploader_id': 'fekurosabi', | |
130 | 'tags': [ | |
add96eb9 | 131 | 'pee', |
21b9413c | 132 | ], |
0a5d7c39 | 133 | 'like_count': int, |
134 | 'view_count': int, | |
135 | 'comment_count': int, | |
21b9413c | 136 | 'timestamp': 1598880567, |
0a5d7c39 | 137 | 'modified_timestamp': int, |
138 | 'upload_date': '20200831', | |
139 | 'modified_date': '20230605', | |
140 | 'thumbnail': 'https://files.iwara.tv/image/thumbnail/7693e881-d302-42a4-a780-f16d66b5dadd/thumbnail-00.jpg', | |
141 | # 'availability': 'needs_auth', | |
21b9413c | 142 | }, |
001a5fd3 YCH |
143 | }] |
144 | ||
c14af7a7 L |
145 | def _extract_formats(self, video_id, fileurl): |
146 | up = urllib.parse.urlparse(fileurl) | |
147 | q = urllib.parse.parse_qs(up.query) | |
148 | paths = up.path.rstrip('/').split('/') | |
149 | # https://github.com/yt-dlp/yt-dlp/issues/6549#issuecomment-1473771047 | |
150 | x_version = hashlib.sha1('_'.join((paths[-1], q['expires'][0], '5nFp9kmbNnHdAFhaqMvt')).encode()).hexdigest() | |
151 | ||
56793f74 | 152 | preference = qualities(['preview', '360', '540', 'Source']) |
153 | ||
c14af7a7 L |
154 | files = self._download_json(fileurl, video_id, headers={'X-Version': x_version}) |
155 | for fmt in files: | |
156 | yield traverse_obj(fmt, { | |
157 | 'format_id': 'name', | |
158 | 'url': ('src', ('view', 'download'), {self._proto_relative_url}), | |
159 | 'ext': ('type', {mimetype2ext}), | |
56793f74 | 160 | 'quality': ('name', {preference}), |
c14af7a7 L |
161 | 'height': ('name', {int_or_none}), |
162 | }, get_all=False) | |
163 | ||
001a5fd3 YCH |
164 | def _real_extract(self, url): |
165 | video_id = self._match_id(url) | |
0a5d7c39 | 166 | username, _ = self._get_login_info() |
167 | video_data = self._download_json( | |
168 | f'https://api.iwara.tv/video/{video_id}', video_id, | |
169 | expected_status=lambda x: True, headers=self._get_media_token()) | |
95a383be L |
170 | errmsg = video_data.get('message') |
171 | # at this point we can actually get uploaded user info, but do we need it? | |
172 | if errmsg == 'errors.privateVideo': | |
0a5d7c39 | 173 | self.raise_login_required('Private video. Login if you have permissions to watch', method='password') |
21b9413c | 174 | elif errmsg == 'errors.notFound' and not username: |
0a5d7c39 | 175 | self.raise_login_required('Video may need login to view', method='password') |
21b9413c | 176 | elif errmsg: # None if success |
95a383be | 177 | raise ExtractorError(f'Iwara says: {errmsg}') |
caf0f5f8 | 178 | |
56793f74 | 179 | if not video_data.get('fileUrl'): |
180 | if video_data.get('embedUrl'): | |
181 | return self.url_result(video_data.get('embedUrl')) | |
182 | raise ExtractorError('This video is unplayable', expected=True) | |
183 | ||
caf0f5f8 | 184 | return { |
001a5fd3 | 185 | 'id': video_id, |
c14af7a7 L |
186 | 'age_limit': 18 if video_data.get('rating') == 'ecchi' else 0, # ecchi is 'sexy' in Japanese |
187 | **traverse_obj(video_data, { | |
188 | 'title': 'title', | |
189 | 'description': 'body', | |
190 | 'uploader': ('user', 'name'), | |
191 | 'uploader_id': ('user', 'username'), | |
192 | 'tags': ('tags', ..., 'id'), | |
193 | 'like_count': 'numLikes', | |
194 | 'view_count': 'numViews', | |
195 | 'comment_count': 'numComments', | |
196 | 'timestamp': ('createdAt', {unified_timestamp}), | |
197 | 'modified_timestamp': ('updatedAt', {unified_timestamp}), | |
198 | 'thumbnail': ('file', 'id', {str}, { | |
199 | lambda x: f'https://files.iwara.tv/image/thumbnail/{x}/thumbnail-00.jpg'}), | |
200 | }), | |
201 | 'formats': list(self._extract_formats(video_id, video_data.get('fileUrl'))), | |
caf0f5f8 | 202 | } |
ff4d7860 | 203 | |
204 | ||
21b9413c | 205 | class IwaraUserIE(IwaraBaseIE): |
c14af7a7 L |
206 | _VALID_URL = r'https?://(?:www\.)?iwara\.tv/profile/(?P<id>[^/?#&]+)' |
207 | IE_NAME = 'iwara:user' | |
208 | _PER_PAGE = 32 | |
ff4d7860 | 209 | |
210 | _TESTS = [{ | |
c14af7a7 | 211 | 'url': 'https://iwara.tv/profile/user792540/videos', |
ff4d7860 | 212 | 'info_dict': { |
c14af7a7 | 213 | 'id': 'user792540', |
0a5d7c39 | 214 | 'title': 'Lyu ya', |
ff4d7860 | 215 | }, |
0a5d7c39 | 216 | 'playlist_mincount': 70, |
ff4d7860 | 217 | }, { |
c14af7a7 | 218 | 'url': 'https://iwara.tv/profile/theblackbirdcalls/videos', |
ff4d7860 | 219 | 'info_dict': { |
c14af7a7 | 220 | 'id': 'theblackbirdcalls', |
0a5d7c39 | 221 | 'title': 'TheBlackbirdCalls', |
ff4d7860 | 222 | }, |
c14af7a7 L |
223 | 'playlist_mincount': 723, |
224 | }, { | |
225 | 'url': 'https://iwara.tv/profile/user792540', | |
226 | 'only_matching': True, | |
227 | }, { | |
228 | 'url': 'https://iwara.tv/profile/theblackbirdcalls', | |
229 | 'only_matching': True, | |
0a5d7c39 | 230 | }, { |
231 | 'url': 'https://www.iwara.tv/profile/lumymmd', | |
232 | 'info_dict': { | |
233 | 'id': 'lumymmd', | |
234 | 'title': 'Lumy MMD', | |
235 | }, | |
236 | 'playlist_mincount': 1, | |
ff4d7860 | 237 | }] |
238 | ||
c14af7a7 L |
239 | def _entries(self, playlist_id, user_id, page): |
240 | videos = self._download_json( | |
241 | 'https://api.iwara.tv/videos', playlist_id, | |
242 | note=f'Downloading page {page}', | |
243 | query={ | |
244 | 'page': page, | |
245 | 'sort': 'date', | |
246 | 'user': user_id, | |
247 | 'limit': self._PER_PAGE, | |
0a5d7c39 | 248 | }, headers=self._get_media_token()) |
c14af7a7 L |
249 | for x in traverse_obj(videos, ('results', ..., 'id')): |
250 | yield self.url_result(f'https://iwara.tv/video/{x}') | |
251 | ||
ff4d7860 | 252 | def _real_extract(self, url): |
c14af7a7 L |
253 | playlist_id = self._match_id(url) |
254 | user_info = self._download_json( | |
255 | f'https://api.iwara.tv/profile/{playlist_id}', playlist_id, | |
256 | note='Requesting user info') | |
257 | user_id = traverse_obj(user_info, ('user', 'id')) | |
ff4d7860 | 258 | |
c14af7a7 L |
259 | return self.playlist_result( |
260 | OnDemandPagedList( | |
261 | functools.partial(self._entries, playlist_id, user_id), | |
262 | self._PER_PAGE), | |
263 | playlist_id, traverse_obj(user_info, ('user', 'name'))) | |
ff4d7860 | 264 | |
265 | ||
21b9413c | 266 | class IwaraPlaylistIE(IwaraBaseIE): |
c14af7a7 L |
267 | _VALID_URL = r'https?://(?:www\.)?iwara\.tv/playlist/(?P<id>[0-9a-f-]+)' |
268 | IE_NAME = 'iwara:playlist' | |
269 | _PER_PAGE = 32 | |
ff4d7860 | 270 | |
271 | _TESTS = [{ | |
c14af7a7 | 272 | 'url': 'https://iwara.tv/playlist/458e5486-36a4-4ac0-b233-7e9eef01025f', |
661e7253 | 273 | 'info_dict': { |
c14af7a7 | 274 | 'id': '458e5486-36a4-4ac0-b233-7e9eef01025f', |
661e7253 | 275 | }, |
c14af7a7 | 276 | 'playlist_mincount': 3, |
ff4d7860 | 277 | }] |
278 | ||
c14af7a7 L |
279 | def _entries(self, playlist_id, first_page, page): |
280 | videos = self._download_json( | |
281 | 'https://api.iwara.tv/videos', playlist_id, f'Downloading page {page}', | |
0a5d7c39 | 282 | query={'page': page, 'limit': self._PER_PAGE}, |
283 | headers=self._get_media_token()) if page else first_page | |
c14af7a7 L |
284 | for x in traverse_obj(videos, ('results', ..., 'id')): |
285 | yield self.url_result(f'https://iwara.tv/video/{x}') | |
ff4d7860 | 286 | |
287 | def _real_extract(self, url): | |
c14af7a7 L |
288 | playlist_id = self._match_id(url) |
289 | page_0 = self._download_json( | |
290 | f'https://api.iwara.tv/playlist/{playlist_id}?page=0&limit={self._PER_PAGE}', playlist_id, | |
0a5d7c39 | 291 | note='Requesting playlist info', headers=self._get_media_token()) |
ff4d7860 | 292 | |
ff4d7860 | 293 | return self.playlist_result( |
c14af7a7 L |
294 | OnDemandPagedList( |
295 | functools.partial(self._entries, playlist_id, page_0), | |
296 | self._PER_PAGE), | |
297 | playlist_id, traverse_obj(page_0, ('title', 'name'))) |