7 from .common
import InfoExtractor
21 class IwaraBaseIE(InfoExtractor
):
22 _NETRC_MACHINE
= 'iwara'
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')
32 def _get_user_token(self
):
33 username
, password
= self
._get
_login
_info
()
34 if not username
or not password
:
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
(
40 'https://api.iwara.tv/user/login', None, note
='Logging in',
41 headers
={'Content-Type': 'application/json'}, data
=json
.dumps({
44 }).encode(), expected_status
=lambda x
: True)
45 user_token
= traverse_obj(response
, ('token', {str}
))
47 error
= traverse_obj(response
, ('message', {str}
))
48 if 'invalidLogin' in error
:
49 raise ExtractorError('Invalid login credentials', expected
=True)
51 raise ExtractorError(f
'Iwara API said: {error or "nothing"}')
53 self
.cache
.store(self
._NETRC
_MACHINE
, username
, user_token
)
55 IwaraBaseIE
._USERTOKEN
= user_token
57 def _get_media_token(self
):
58 self
._get
_user
_token
()
59 if not IwaraBaseIE
._USERTOKEN
:
60 return # user has not passed credentials
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',
66 'Authorization': f
'Bearer {IwaraBaseIE._USERTOKEN}',
67 'Content-Type': 'application/json',
70 return {'Authorization': f
'Bearer {IwaraBaseIE._MEDIATOKEN}'}
72 def _perform_login(self
, username
, password
):
73 self
._get
_media
_token
()
76 class IwaraIE(IwaraBaseIE
):
78 _VALID_URL
= r
'https?://(?:www\.|ecchi\.)?iwara\.tv/videos?/(?P<id>[a-zA-Z0-9]+)'
80 'url': 'https://www.iwara.tv/video/k2ayoueezfkx6gvq',
82 'id': 'k2ayoueezfkx6gvq',
85 'title': 'Defeat of Irybelda - アイリベルダの敗北',
86 'description': 'md5:70278abebe706647a8b4cb04cf23e0d3',
87 'uploader': 'Inwerwm',
88 'uploader_id': 'inwerwm',
91 'view_count': 1050343,
93 'timestamp': 1677843869,
94 'modified_timestamp': 1679056362,
96 'skip': 'this video cannot be played because of migration',
98 'url': 'https://iwara.tv/video/1ywe1sbkqwumpdxz5/',
99 'md5': '7645f966f069b8ec9210efd9130c9aad',
101 'id': '1ywe1sbkqwumpdxz5',
104 'title': 'Aponia アポニア SEX Party Tonight 手の脱衣 巨乳 ',
105 'description': 'md5:3f60016fff22060eef1ef26d430b1f67',
106 'uploader': 'Lyu ya',
107 'uploader_id': 'user792540',
113 'comment_count': int,
114 'timestamp': 1678732213,
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',
121 'url': 'https://iwara.tv/video/blggmfno8ghl725bg',
123 'id': 'blggmfno8ghl725bg',
126 'title': 'お外でおしっこしちゃう猫耳ロリメイド',
127 'description': 'md5:0342ba9bf6db09edbbb28729657c3611',
128 'uploader': 'Fe_Kurosabi',
129 'uploader_id': 'fekurosabi',
135 'comment_count': int,
136 'timestamp': 1598880567,
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',
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()
152 preference
= qualities(['preview', '360', '540', 'Source'])
154 files
= self
._download
_json
(fileurl
, video_id
, headers
={'X-Version': x_version
})
156 yield traverse_obj(fmt
, {
158 'url': ('src', ('view', 'download'), {self
._proto
_relative
_url
}),
159 'ext': ('type', {mimetype2ext}
),
160 'quality': ('name', {preference}
),
161 'height': ('name', {int_or_none}
),
164 def _real_extract(self
, url
):
165 video_id
= self
._match
_id
(url
)
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
())
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':
173 self
.raise_login_required('Private video. Login if you have permissions to watch', method
='password')
174 elif errmsg
== 'errors.notFound' and not username
:
175 self
.raise_login_required('Video may need login to view', method
='password')
176 elif errmsg
: # None if success
177 raise ExtractorError(f
'Iwara says: {errmsg}')
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)
186 'age_limit': 18 if video_data
.get('rating') == 'ecchi' else 0, # ecchi is 'sexy' in Japanese
187 **traverse_obj(video_data
, {
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'}),
201 'formats': list(self
._extract
_formats
(video_id
, video_data
.get('fileUrl'))),
205 class IwaraUserIE(IwaraBaseIE
):
206 _VALID_URL
= r
'https?://(?:www\.)?iwara\.tv/profile/(?P<id>[^/?#&]+)'
207 IE_NAME
= 'iwara:user'
211 'url': 'https://iwara.tv/profile/user792540/videos',
216 'playlist_mincount': 70,
218 'url': 'https://iwara.tv/profile/theblackbirdcalls/videos',
220 'id': 'theblackbirdcalls',
221 'title': 'TheBlackbirdCalls',
223 'playlist_mincount': 723,
225 'url': 'https://iwara.tv/profile/user792540',
226 'only_matching': True,
228 'url': 'https://iwara.tv/profile/theblackbirdcalls',
229 'only_matching': True,
231 'url': 'https://www.iwara.tv/profile/lumymmd',
236 'playlist_mincount': 1,
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}',
247 'limit': self
._PER
_PAGE
,
248 }, headers
=self
._get
_media
_token
())
249 for x
in traverse_obj(videos
, ('results', ..., 'id')):
250 yield self
.url_result(f
'https://iwara.tv/video/{x}')
252 def _real_extract(self
, url
):
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'))
259 return self
.playlist_result(
261 functools
.partial(self
._entries
, playlist_id
, user_id
),
263 playlist_id
, traverse_obj(user_info
, ('user', 'name')))
266 class IwaraPlaylistIE(IwaraBaseIE
):
267 _VALID_URL
= r
'https?://(?:www\.)?iwara\.tv/playlist/(?P<id>[0-9a-f-]+)'
268 IE_NAME
= 'iwara:playlist'
272 'url': 'https://iwara.tv/playlist/458e5486-36a4-4ac0-b233-7e9eef01025f',
274 'id': '458e5486-36a4-4ac0-b233-7e9eef01025f',
276 'playlist_mincount': 3,
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}',
282 query
={'page': page
, 'limit': self
._PER
_PAGE
},
283 headers
=self
._get
_media
_token
()) if page
else first_page
284 for x
in traverse_obj(videos
, ('results', ..., 'id')):
285 yield self
.url_result(f
'https://iwara.tv/video/{x}')
287 def _real_extract(self
, url
):
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
,
291 note
='Requesting playlist info', headers
=self
._get
_media
_token
())
293 return self
.playlist_result(
295 functools
.partial(self
._entries
, playlist_id
, page_0
),
297 playlist_id
, traverse_obj(page_0
, ('title', 'name')))