]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/iwara.py
[extractor/iwara] Fix format sorting (#6651)
[yt-dlp.git] / yt_dlp / extractor / iwara.py
1 import functools
2 import urllib.parse
3 import hashlib
4
5 from .common import InfoExtractor
6 from ..utils import (
7 ExtractorError,
8 OnDemandPagedList,
9 int_or_none,
10 mimetype2ext,
11 qualities,
12 traverse_obj,
13 unified_timestamp,
14 )
15
16
17 class IwaraIE(InfoExtractor):
18 IE_NAME = 'iwara'
19 _VALID_URL = r'https?://(?:www\.|ecchi\.)?iwara\.tv/videos?/(?P<id>[a-zA-Z0-9]+)'
20 _TESTS = [{
21 # this video cannot be played because of migration
22 'only_matching': True,
23 'url': 'https://www.iwara.tv/video/k2ayoueezfkx6gvq',
24 'info_dict': {
25 'id': 'k2ayoueezfkx6gvq',
26 'ext': 'mp4',
27 'age_limit': 18,
28 'title': 'Defeat of Irybelda - アイリベルダの敗北',
29 'description': 'md5:70278abebe706647a8b4cb04cf23e0d3',
30 'uploader': 'Inwerwm',
31 'uploader_id': 'inwerwm',
32 'tags': 'count:1',
33 'like_count': 6133,
34 'view_count': 1050343,
35 'comment_count': 1,
36 'timestamp': 1677843869,
37 'modified_timestamp': 1679056362,
38 },
39 }, {
40 'url': 'https://iwara.tv/video/1ywe1sbkqwumpdxz5/',
41 'md5': '20691ce1473ec2766c0788e14c60ce66',
42 'info_dict': {
43 'id': '1ywe1sbkqwumpdxz5',
44 'ext': 'mp4',
45 'age_limit': 18,
46 'title': 'Aponia 阿波尼亚SEX Party Tonight 手动脱衣 大奶 裸腿',
47 'description': 'md5:0c4c310f2e0592d68b9f771d348329ca',
48 'uploader': '龙也zZZ',
49 'uploader_id': 'user792540',
50 'tags': [
51 'uncategorized'
52 ],
53 'like_count': 1809,
54 'view_count': 25156,
55 'comment_count': 1,
56 'timestamp': 1678732213,
57 'modified_timestamp': 1679110271,
58 },
59 }]
60
61 def _extract_formats(self, video_id, fileurl):
62 up = urllib.parse.urlparse(fileurl)
63 q = urllib.parse.parse_qs(up.query)
64 paths = up.path.rstrip('/').split('/')
65 # https://github.com/yt-dlp/yt-dlp/issues/6549#issuecomment-1473771047
66 x_version = hashlib.sha1('_'.join((paths[-1], q['expires'][0], '5nFp9kmbNnHdAFhaqMvt')).encode()).hexdigest()
67
68 preference = qualities(['preview', '360', '540', 'Source'])
69
70 files = self._download_json(fileurl, video_id, headers={'X-Version': x_version})
71 for fmt in files:
72 yield traverse_obj(fmt, {
73 'format_id': 'name',
74 'url': ('src', ('view', 'download'), {self._proto_relative_url}),
75 'ext': ('type', {mimetype2ext}),
76 'quality': ('name', {preference}),
77 'height': ('name', {int_or_none}),
78 }, get_all=False)
79
80 def _real_extract(self, url):
81 video_id = self._match_id(url)
82 video_data = self._download_json(f'https://api.iwara.tv/video/{video_id}', video_id, expected_status=lambda x: True)
83 errmsg = video_data.get('message')
84 # at this point we can actually get uploaded user info, but do we need it?
85 if errmsg == 'errors.privateVideo':
86 self.raise_login_required('Private video. Login if you have permissions to watch')
87 elif errmsg:
88 raise ExtractorError(f'Iwara says: {errmsg}')
89
90 if not video_data.get('fileUrl'):
91 if video_data.get('embedUrl'):
92 return self.url_result(video_data.get('embedUrl'))
93 raise ExtractorError('This video is unplayable', expected=True)
94
95 return {
96 'id': video_id,
97 'age_limit': 18 if video_data.get('rating') == 'ecchi' else 0, # ecchi is 'sexy' in Japanese
98 **traverse_obj(video_data, {
99 'title': 'title',
100 'description': 'body',
101 'uploader': ('user', 'name'),
102 'uploader_id': ('user', 'username'),
103 'tags': ('tags', ..., 'id'),
104 'like_count': 'numLikes',
105 'view_count': 'numViews',
106 'comment_count': 'numComments',
107 'timestamp': ('createdAt', {unified_timestamp}),
108 'modified_timestamp': ('updatedAt', {unified_timestamp}),
109 'thumbnail': ('file', 'id', {str}, {
110 lambda x: f'https://files.iwara.tv/image/thumbnail/{x}/thumbnail-00.jpg'}),
111 }),
112 'formats': list(self._extract_formats(video_id, video_data.get('fileUrl'))),
113 }
114
115
116 class IwaraUserIE(InfoExtractor):
117 _VALID_URL = r'https?://(?:www\.)?iwara\.tv/profile/(?P<id>[^/?#&]+)'
118 IE_NAME = 'iwara:user'
119 _PER_PAGE = 32
120
121 _TESTS = [{
122 'url': 'https://iwara.tv/profile/user792540/videos',
123 'info_dict': {
124 'id': 'user792540',
125 },
126 'playlist_mincount': 80,
127 }, {
128 'url': 'https://iwara.tv/profile/theblackbirdcalls/videos',
129 'info_dict': {
130 'id': 'theblackbirdcalls',
131 },
132 'playlist_mincount': 723,
133 }, {
134 'url': 'https://iwara.tv/profile/user792540',
135 'only_matching': True,
136 }, {
137 'url': 'https://iwara.tv/profile/theblackbirdcalls',
138 'only_matching': True,
139 }]
140
141 def _entries(self, playlist_id, user_id, page):
142 videos = self._download_json(
143 'https://api.iwara.tv/videos', playlist_id,
144 note=f'Downloading page {page}',
145 query={
146 'page': page,
147 'sort': 'date',
148 'user': user_id,
149 'limit': self._PER_PAGE,
150 })
151 for x in traverse_obj(videos, ('results', ..., 'id')):
152 yield self.url_result(f'https://iwara.tv/video/{x}')
153
154 def _real_extract(self, url):
155 playlist_id = self._match_id(url)
156 user_info = self._download_json(
157 f'https://api.iwara.tv/profile/{playlist_id}', playlist_id,
158 note='Requesting user info')
159 user_id = traverse_obj(user_info, ('user', 'id'))
160
161 return self.playlist_result(
162 OnDemandPagedList(
163 functools.partial(self._entries, playlist_id, user_id),
164 self._PER_PAGE),
165 playlist_id, traverse_obj(user_info, ('user', 'name')))
166
167
168 class IwaraPlaylistIE(InfoExtractor):
169 # the ID is an UUID but I don't think it's necessary to write concrete regex
170 _VALID_URL = r'https?://(?:www\.)?iwara\.tv/playlist/(?P<id>[0-9a-f-]+)'
171 IE_NAME = 'iwara:playlist'
172 _PER_PAGE = 32
173
174 _TESTS = [{
175 'url': 'https://iwara.tv/playlist/458e5486-36a4-4ac0-b233-7e9eef01025f',
176 'info_dict': {
177 'id': '458e5486-36a4-4ac0-b233-7e9eef01025f',
178 },
179 'playlist_mincount': 3,
180 }]
181
182 def _entries(self, playlist_id, first_page, page):
183 videos = self._download_json(
184 'https://api.iwara.tv/videos', playlist_id, f'Downloading page {page}',
185 query={'page': page, 'limit': self._PER_PAGE}) if page else first_page
186 for x in traverse_obj(videos, ('results', ..., 'id')):
187 yield self.url_result(f'https://iwara.tv/video/{x}')
188
189 def _real_extract(self, url):
190 playlist_id = self._match_id(url)
191 page_0 = self._download_json(
192 f'https://api.iwara.tv/playlist/{playlist_id}?page=0&limit={self._PER_PAGE}', playlist_id,
193 note='Requesting playlist info')
194
195 return self.playlist_result(
196 OnDemandPagedList(
197 functools.partial(self._entries, playlist_id, page_0),
198 self._PER_PAGE),
199 playlist_id, traverse_obj(page_0, ('title', 'name')))