]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/newgrounds.py
[cleanup] Use `_html_extract_title`
[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_extract_title(webpage)
110
111 media_url_string = self._search_regex(
112 r'"url"\s*:\s*("[^"]+"),', webpage, 'media url', default=None)
113
114 if media_url_string:
115 media_url = self._parse_json(media_url_string, media_id)
116 formats = [{
117 'url': media_url,
118 'format_id': 'source',
119 'quality': 1,
120 }]
121 else:
122 json_video = self._download_json('https://www.newgrounds.com/portal/video/' + media_id, media_id, headers={
123 'Accept': 'application/json',
124 'Referer': url,
125 'X-Requested-With': 'XMLHttpRequest'
126 })
127
128 uploader = json_video.get('author')
129 media_formats = json_video.get('sources', [])
130 for media_format in media_formats:
131 media_sources = media_formats[media_format]
132 for source in media_sources:
133 formats.append({
134 'format_id': media_format,
135 'quality': int_or_none(media_format[:-1]),
136 'url': source.get('src')
137 })
138
139 if not uploader:
140 uploader = self._html_search_regex(
141 (r'(?s)<h4[^>]*>(.+?)</h4>.*?<em>\s*(?:Author|Artist)\s*</em>',
142 r'(?:Author|Writer)\s*<a[^>]+>([^<]+)'), webpage, 'uploader',
143 fatal=False)
144
145 age_limit = self._html_search_regex(
146 r'<h2\s*class=["\']rated-([^"\'])["\'][^>]+>', webpage, 'age_limit', default='e')
147 age_limit = self._AGE_LIMIT.get(age_limit)
148
149 timestamp = unified_timestamp(self._html_search_regex(
150 (r'<dt>\s*Uploaded\s*</dt>\s*<dd>([^<]+</dd>\s*<dd>[^<]+)',
151 r'<dt>\s*Uploaded\s*</dt>\s*<dd>([^<]+)'), webpage, 'timestamp',
152 default=None))
153
154 duration = parse_duration(self._html_search_regex(
155 r'"duration"\s*:\s*["\']?(\d+)["\']?', webpage,
156 'duration', default=None))
157
158 description = clean_html(get_element_by_id('author_comments', webpage)) or self._og_search_description(webpage)
159
160 view_count = parse_count(self._html_search_regex(
161 r'(?s)<dt>\s*(?:Views|Listens)\s*</dt>\s*<dd>([\d\.,]+)</dd>', webpage,
162 'view count', default=None))
163
164 filesize = int_or_none(self._html_search_regex(
165 r'"filesize"\s*:\s*["\']?([\d]+)["\']?,', webpage, 'filesize',
166 default=None))
167
168 video_type_description = self._html_search_regex(
169 r'"description"\s*:\s*["\']?([^"\']+)["\']?,', webpage, 'filesize',
170 default=None)
171
172 if len(formats) == 1:
173 formats[0]['filesize'] = filesize
174
175 if video_type_description == 'Audio File':
176 formats[0]['vcodec'] = 'none'
177 self._check_formats(formats, media_id)
178 self._sort_formats(formats)
179
180 return {
181 'id': media_id,
182 'title': title,
183 'uploader': uploader,
184 'timestamp': timestamp,
185 'duration': duration,
186 'formats': formats,
187 'thumbnail': self._og_search_thumbnail(webpage),
188 'description': description,
189 'age_limit': age_limit,
190 'view_count': view_count,
191 }
192
193
194 class NewgroundsPlaylistIE(InfoExtractor):
195 IE_NAME = 'Newgrounds:playlist'
196 _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:collection|[^/]+/search/[^/]+)/(?P<id>[^/?#&]+)'
197 _TESTS = [{
198 'url': 'https://www.newgrounds.com/collection/cats',
199 'info_dict': {
200 'id': 'cats',
201 'title': 'Cats',
202 },
203 'playlist_mincount': 45,
204 }, {
205 'url': 'https://www.newgrounds.com/collection/dogs',
206 'info_dict': {
207 'id': 'dogs',
208 'title': 'Dogs',
209 },
210 'playlist_mincount': 26,
211 }, {
212 'url': 'http://www.newgrounds.com/audio/search/title/cats',
213 'only_matching': True,
214 }]
215
216 def _real_extract(self, url):
217 playlist_id = self._match_id(url)
218
219 webpage = self._download_webpage(url, playlist_id)
220
221 title = self._html_extract_title(webpage, default=None)
222
223 # cut left menu
224 webpage = self._search_regex(
225 r'(?s)<div[^>]+\bclass=["\']column wide(.+)',
226 webpage, 'wide column', default=webpage)
227
228 entries = []
229 for a, path, media_id in re.findall(
230 r'(<a[^>]+\bhref=["\'][^"\']+((?:portal/view|audio/listen)/(\d+))[^>]+>)',
231 webpage):
232 a_class = extract_attributes(a).get('class')
233 if a_class not in ('item-portalsubmission', 'item-audiosubmission'):
234 continue
235 entries.append(
236 self.url_result(
237 f'https://www.newgrounds.com/{path}',
238 ie=NewgroundsIE.ie_key(), video_id=media_id))
239
240 return self.playlist_result(entries, playlist_id, title)
241
242
243 class NewgroundsUserIE(InfoExtractor):
244 IE_NAME = 'Newgrounds:user'
245 _VALID_URL = r'https?://(?P<id>[^\.]+)\.newgrounds\.com/(?:movies|audio)/?(?:[#?]|$)'
246 _TESTS = [{
247 'url': 'https://burn7.newgrounds.com/audio',
248 'info_dict': {
249 'id': 'burn7',
250 },
251 'playlist_mincount': 150,
252 }, {
253 'url': 'https://burn7.newgrounds.com/movies',
254 'info_dict': {
255 'id': 'burn7',
256 },
257 'playlist_mincount': 2,
258 }, {
259 'url': 'https://brian-beaton.newgrounds.com/movies',
260 'info_dict': {
261 'id': 'brian-beaton',
262 },
263 'playlist_mincount': 10,
264 }]
265 _PAGE_SIZE = 30
266
267 def _fetch_page(self, channel_id, url, page):
268 page += 1
269 posts_info = self._download_json(
270 f'{url}/page/{page}', channel_id,
271 note=f'Downloading page {page}', headers={
272 'Accept': 'application/json, text/javascript, */*; q = 0.01',
273 'X-Requested-With': 'XMLHttpRequest',
274 })
275 sequence = posts_info.get('sequence', [])
276 for year in sequence:
277 posts = try_get(posts_info, lambda x: x['years'][str(year)]['items'])
278 for post in posts:
279 path, media_id = self._search_regex(
280 r'<a[^>]+\bhref=["\'][^"\']+((?:portal/view|audio/listen)/(\d+))[^>]+>',
281 post, 'url', group=(1, 2))
282 yield self.url_result(f'https://www.newgrounds.com/{path}', NewgroundsIE.ie_key(), media_id)
283
284 def _real_extract(self, url):
285 channel_id = self._match_id(url)
286
287 entries = OnDemandPagedList(functools.partial(
288 self._fetch_page, channel_id, url), self._PAGE_SIZE)
289
290 return self.playlist_result(entries, channel_id)