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