]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/newgrounds.py
Implement universal format sorting
[yt-dlp.git] / yt_dlp / extractor / newgrounds.py
1 import functools
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6 clean_html,
7 extract_attributes,
8 get_element_by_id,
9 int_or_none,
10 parse_count,
11 parse_duration,
12 unified_timestamp,
13 OnDemandPagedList,
14 try_get,
15 )
16
17
18 class NewgroundsIE(InfoExtractor):
19 _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:audio/listen|portal/view)/(?P<id>\d+)(?:/format/flash)?'
20 _TESTS = [{
21 'url': 'https://www.newgrounds.com/audio/listen/549479',
22 'md5': 'fe6033d297591288fa1c1f780386f07a',
23 'info_dict': {
24 'id': '549479',
25 'ext': 'mp3',
26 'title': 'B7 - BusMode',
27 'uploader': 'Burn7',
28 'timestamp': 1378878540,
29 'upload_date': '20130911',
30 'duration': 143,
31 'view_count': int,
32 'description': 'md5:b8b3c2958875189f07d8e313462e8c4f',
33 },
34 }, {
35 'url': 'https://www.newgrounds.com/portal/view/1',
36 'md5': 'fbfb40e2dc765a7e830cb251d370d981',
37 'info_dict': {
38 'id': '1',
39 'ext': 'mp4',
40 'title': 'Scrotum 1',
41 'uploader': 'Brian-Beaton',
42 'timestamp': 955064100,
43 'upload_date': '20000406',
44 'view_count': int,
45 'description': 'Scrotum plays "catch."',
46 'age_limit': 17,
47 },
48 }, {
49 # source format unavailable, additional mp4 formats
50 'url': 'http://www.newgrounds.com/portal/view/689400',
51 'info_dict': {
52 'id': '689400',
53 'ext': 'mp4',
54 'title': 'ZTV News Episode 8',
55 'uploader': 'ZONE-SAMA',
56 'timestamp': 1487965140,
57 'upload_date': '20170224',
58 'view_count': int,
59 'description': 'md5:aff9b330ec2e78ed93b1ad6d017accc6',
60 'age_limit': 17,
61 },
62 'params': {
63 'skip_download': True,
64 },
65 }, {
66 'url': 'https://www.newgrounds.com/portal/view/297383',
67 'md5': '2c11f5fd8cb6b433a63c89ba3141436c',
68 'info_dict': {
69 'id': '297383',
70 'ext': 'mp4',
71 'title': 'Metal Gear Awesome',
72 'uploader': 'Egoraptor',
73 'timestamp': 1140663240,
74 'upload_date': '20060223',
75 'view_count': int,
76 'description': 'md5:9246c181614e23754571995104da92e0',
77 'age_limit': 13,
78 }
79 }, {
80 'url': 'https://www.newgrounds.com/portal/view/297383/format/flash',
81 'md5': '5d05585a9a0caca059f5abfbd3865524',
82 'info_dict': {
83 'id': '297383',
84 'ext': 'swf',
85 'title': 'Metal Gear Awesome',
86 'description': 'Metal Gear Awesome',
87 'uploader': 'Egoraptor',
88 'upload_date': '20060223',
89 'timestamp': 1140663240,
90 'age_limit': 13,
91 }
92 }]
93 _AGE_LIMIT = {
94 'e': 0,
95 't': 13,
96 'm': 17,
97 'a': 18,
98 }
99
100 def _real_extract(self, url):
101 media_id = self._match_id(url)
102 formats = []
103 uploader = None
104 webpage = self._download_webpage(url, media_id)
105
106 title = self._html_extract_title(webpage)
107
108 media_url_string = self._search_regex(
109 r'"url"\s*:\s*("[^"]+"),', webpage, 'media url', default=None)
110
111 if media_url_string:
112 media_url = self._parse_json(media_url_string, media_id)
113 formats = [{
114 'url': media_url,
115 'format_id': 'source',
116 'quality': 1,
117 }]
118 else:
119 json_video = self._download_json('https://www.newgrounds.com/portal/video/' + media_id, media_id, headers={
120 'Accept': 'application/json',
121 'Referer': url,
122 'X-Requested-With': 'XMLHttpRequest'
123 })
124
125 uploader = json_video.get('author')
126 media_formats = json_video.get('sources', [])
127 for media_format in media_formats:
128 media_sources = media_formats[media_format]
129 for source in media_sources:
130 formats.append({
131 'format_id': media_format,
132 'quality': int_or_none(media_format[:-1]),
133 'url': source.get('src')
134 })
135
136 if not uploader:
137 uploader = self._html_search_regex(
138 (r'(?s)<h4[^>]*>(.+?)</h4>.*?<em>\s*(?:Author|Artist)\s*</em>',
139 r'(?:Author|Writer)\s*<a[^>]+>([^<]+)'), webpage, 'uploader',
140 fatal=False)
141
142 age_limit = self._html_search_regex(
143 r'<h2\s*class=["\']rated-([^"\'])["\'][^>]+>', webpage, 'age_limit', default='e')
144 age_limit = self._AGE_LIMIT.get(age_limit)
145
146 timestamp = unified_timestamp(self._html_search_regex(
147 (r'<dt>\s*Uploaded\s*</dt>\s*<dd>([^<]+</dd>\s*<dd>[^<]+)',
148 r'<dt>\s*Uploaded\s*</dt>\s*<dd>([^<]+)'), webpage, 'timestamp',
149 default=None))
150
151 duration = parse_duration(self._html_search_regex(
152 r'"duration"\s*:\s*["\']?(\d+)["\']?', webpage,
153 'duration', default=None))
154
155 description = clean_html(get_element_by_id('author_comments', webpage)) or self._og_search_description(webpage)
156
157 view_count = parse_count(self._html_search_regex(
158 r'(?s)<dt>\s*(?:Views|Listens)\s*</dt>\s*<dd>([\d\.,]+)</dd>', webpage,
159 'view count', default=None))
160
161 filesize = int_or_none(self._html_search_regex(
162 r'"filesize"\s*:\s*["\']?([\d]+)["\']?,', webpage, 'filesize',
163 default=None))
164
165 video_type_description = self._html_search_regex(
166 r'"description"\s*:\s*["\']?([^"\']+)["\']?,', webpage, 'filesize',
167 default=None)
168
169 if len(formats) == 1:
170 formats[0]['filesize'] = filesize
171
172 if video_type_description == 'Audio File':
173 formats[0]['vcodec'] = 'none'
174 self._check_formats(formats, media_id)
175 self._sort_formats(formats)
176
177 return {
178 'id': media_id,
179 'title': title,
180 'uploader': uploader,
181 'timestamp': timestamp,
182 'duration': duration,
183 'formats': formats,
184 'thumbnail': self._og_search_thumbnail(webpage),
185 'description': description,
186 'age_limit': age_limit,
187 'view_count': view_count,
188 }
189
190
191 class NewgroundsPlaylistIE(InfoExtractor):
192 IE_NAME = 'Newgrounds:playlist'
193 _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:collection|[^/]+/search/[^/]+)/(?P<id>[^/?#&]+)'
194 _TESTS = [{
195 'url': 'https://www.newgrounds.com/collection/cats',
196 'info_dict': {
197 'id': 'cats',
198 'title': 'Cats',
199 },
200 'playlist_mincount': 45,
201 }, {
202 'url': 'https://www.newgrounds.com/collection/dogs',
203 'info_dict': {
204 'id': 'dogs',
205 'title': 'Dogs',
206 },
207 'playlist_mincount': 26,
208 }, {
209 'url': 'http://www.newgrounds.com/audio/search/title/cats',
210 'only_matching': True,
211 }]
212
213 def _real_extract(self, url):
214 playlist_id = self._match_id(url)
215
216 webpage = self._download_webpage(url, playlist_id)
217
218 title = self._html_extract_title(webpage, default=None)
219
220 # cut left menu
221 webpage = self._search_regex(
222 r'(?s)<div[^>]+\bclass=["\']column wide(.+)',
223 webpage, 'wide column', default=webpage)
224
225 entries = []
226 for a, path, media_id in re.findall(
227 r'(<a[^>]+\bhref=["\'][^"\']+((?:portal/view|audio/listen)/(\d+))[^>]+>)',
228 webpage):
229 a_class = extract_attributes(a).get('class')
230 if a_class not in ('item-portalsubmission', 'item-audiosubmission'):
231 continue
232 entries.append(
233 self.url_result(
234 f'https://www.newgrounds.com/{path}',
235 ie=NewgroundsIE.ie_key(), video_id=media_id))
236
237 return self.playlist_result(entries, playlist_id, title)
238
239
240 class NewgroundsUserIE(InfoExtractor):
241 IE_NAME = 'Newgrounds:user'
242 _VALID_URL = r'https?://(?P<id>[^\.]+)\.newgrounds\.com/(?:movies|audio)/?(?:[#?]|$)'
243 _TESTS = [{
244 'url': 'https://burn7.newgrounds.com/audio',
245 'info_dict': {
246 'id': 'burn7',
247 },
248 'playlist_mincount': 150,
249 }, {
250 'url': 'https://burn7.newgrounds.com/movies',
251 'info_dict': {
252 'id': 'burn7',
253 },
254 'playlist_mincount': 2,
255 }, {
256 'url': 'https://brian-beaton.newgrounds.com/movies',
257 'info_dict': {
258 'id': 'brian-beaton',
259 },
260 'playlist_mincount': 10,
261 }]
262 _PAGE_SIZE = 30
263
264 def _fetch_page(self, channel_id, url, page):
265 page += 1
266 posts_info = self._download_json(
267 f'{url}/page/{page}', channel_id,
268 note=f'Downloading page {page}', headers={
269 'Accept': 'application/json, text/javascript, */*; q = 0.01',
270 'X-Requested-With': 'XMLHttpRequest',
271 })
272 sequence = posts_info.get('sequence', [])
273 for year in sequence:
274 posts = try_get(posts_info, lambda x: x['years'][str(year)]['items'])
275 for post in posts:
276 path, media_id = self._search_regex(
277 r'<a[^>]+\bhref=["\'][^"\']+((?:portal/view|audio/listen)/(\d+))[^>]+>',
278 post, 'url', group=(1, 2))
279 yield self.url_result(f'https://www.newgrounds.com/{path}', NewgroundsIE.ie_key(), media_id)
280
281 def _real_extract(self, url):
282 channel_id = self._match_id(url)
283
284 entries = OnDemandPagedList(functools.partial(
285 self._fetch_page, channel_id, url), self._PAGE_SIZE)
286
287 return self.playlist_result(entries, channel_id)