]>
Commit | Line | Data |
---|---|---|
bd2d82a5 PH |
1 | from __future__ import unicode_literals |
2 | ||
70e79672 S |
3 | import re |
4 | ||
eb03f4da | 5 | from .common import InfoExtractor |
70e79672 S |
6 | from ..utils import ( |
7 | extract_attributes, | |
8 | int_or_none, | |
28a4d6cc S |
9 | parse_duration, |
10 | parse_filesize, | |
11 | unified_timestamp, | |
70e79672 | 12 | ) |
eb03f4da | 13 | |
d0ae9e3a | 14 | |
eb03f4da | 15 | class NewgroundsIE(InfoExtractor): |
0de968b5 YCH |
16 | _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:audio/listen|portal/view)/(?P<id>[0-9]+)' |
17 | _TESTS = [{ | |
6c3affcb | 18 | 'url': 'https://www.newgrounds.com/audio/listen/549479', |
bd2d82a5 PH |
19 | 'md5': 'fe6033d297591288fa1c1f780386f07a', |
20 | 'info_dict': { | |
d55433bb PH |
21 | 'id': '549479', |
22 | 'ext': 'mp3', | |
23 | 'title': 'B7 - BusMode', | |
24 | 'uploader': 'Burn7', | |
28a4d6cc S |
25 | 'timestamp': 1378878540, |
26 | 'upload_date': '20130911', | |
27 | 'duration': 143, | |
28 | }, | |
0de968b5 | 29 | }, { |
6c3affcb | 30 | 'url': 'https://www.newgrounds.com/portal/view/673111', |
0de968b5 YCH |
31 | 'md5': '3394735822aab2478c31b1004fe5e5bc', |
32 | 'info_dict': { | |
33 | 'id': '673111', | |
34 | 'ext': 'mp4', | |
35 | 'title': 'Dancin', | |
36 | 'uploader': 'Squirrelman82', | |
28a4d6cc S |
37 | 'timestamp': 1460256780, |
38 | 'upload_date': '20160410', | |
0de968b5 | 39 | }, |
6e999fbc S |
40 | }, { |
41 | # source format unavailable, additional mp4 formats | |
42 | 'url': 'http://www.newgrounds.com/portal/view/689400', | |
43 | 'info_dict': { | |
44 | 'id': '689400', | |
45 | 'ext': 'mp4', | |
46 | 'title': 'ZTV News Episode 8', | |
47 | 'uploader': 'BennettTheSage', | |
28a4d6cc S |
48 | 'timestamp': 1487965140, |
49 | 'upload_date': '20170224', | |
6e999fbc S |
50 | }, |
51 | 'params': { | |
52 | 'skip_download': True, | |
53 | }, | |
0de968b5 | 54 | }] |
eb03f4da R |
55 | |
56 | def _real_extract(self, url): | |
6c3affcb | 57 | media_id = self._match_id(url) |
6e999fbc | 58 | |
6c3affcb | 59 | webpage = self._download_webpage(url, media_id) |
5f6a1245 | 60 | |
bd2d82a5 | 61 | title = self._html_search_regex( |
0de968b5 YCH |
62 | r'<title>([^>]+)</title>', webpage, 'title') |
63 | ||
28a4d6cc | 64 | media_url = self._parse_json(self._search_regex( |
6e999fbc S |
65 | r'"url"\s*:\s*("[^"]+"),', webpage, ''), media_id) |
66 | ||
67 | formats = [{ | |
28a4d6cc | 68 | 'url': media_url, |
6e999fbc S |
69 | 'format_id': 'source', |
70 | 'quality': 1, | |
71 | }] | |
72 | ||
73 | max_resolution = int_or_none(self._search_regex( | |
74 | r'max_resolution["\']\s*:\s*(\d+)', webpage, 'max resolution', | |
75 | default=None)) | |
76 | if max_resolution: | |
28a4d6cc | 77 | url_base = media_url.rpartition('.')[0] |
6e999fbc S |
78 | for resolution in (360, 720, 1080): |
79 | if resolution > max_resolution: | |
80 | break | |
81 | formats.append({ | |
82 | 'url': '%s.%dp.mp4' % (url_base, resolution), | |
83 | 'format_id': '%dp' % resolution, | |
84 | 'height': resolution, | |
85 | }) | |
5f6a1245 | 86 | |
6e999fbc S |
87 | self._check_formats(formats, media_id) |
88 | self._sort_formats(formats) | |
89 | ||
9e167e1e S |
90 | uploader = self._html_search_regex( |
91 | (r'(?s)<h4[^>]*>(.+?)</h4>.*?<em>\s*Author\s*</em>', | |
92 | r'(?:Author|Writer)\s*<a[^>]+>([^<]+)'), webpage, 'uploader', | |
6e999fbc | 93 | fatal=False) |
eb03f4da | 94 | |
9e167e1e S |
95 | timestamp = unified_timestamp(self._html_search_regex( |
96 | (r'<dt>\s*Uploaded\s*</dt>\s*<dd>([^<]+</dd>\s*<dd>[^<]+)', | |
97 | r'<dt>\s*Uploaded\s*</dt>\s*<dd>([^<]+)'), webpage, 'timestamp', | |
28a4d6cc S |
98 | default=None)) |
99 | duration = parse_duration(self._search_regex( | |
9e167e1e S |
100 | r'(?s)<dd>\s*Song\s*</dd>\s*<dd>.+?</dd>\s*<dd>([^<]+)', webpage, |
101 | 'duration', default=None)) | |
28a4d6cc S |
102 | |
103 | filesize_approx = parse_filesize(self._html_search_regex( | |
9e167e1e | 104 | r'(?s)<dd>\s*Song\s*</dd>\s*<dd>(.+?)</dd>', webpage, 'filesize', |
28a4d6cc S |
105 | default=None)) |
106 | if len(formats) == 1: | |
107 | formats[0]['filesize_approx'] = filesize_approx | |
108 | ||
109 | if '<dd>Song' in webpage: | |
110 | formats[0]['vcodec'] = 'none' | |
111 | ||
d0ae9e3a | 112 | return { |
6c3affcb | 113 | 'id': media_id, |
bd2d82a5 | 114 | 'title': title, |
eb03f4da | 115 | 'uploader': uploader, |
28a4d6cc S |
116 | 'timestamp': timestamp, |
117 | 'duration': duration, | |
6e999fbc | 118 | 'formats': formats, |
d0ae9e3a | 119 | } |
70e79672 S |
120 | |
121 | ||
122 | class NewgroundsPlaylistIE(InfoExtractor): | |
123 | _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:collection|[^/]+/search/[^/]+)/(?P<id>[^/?#&]+)' | |
124 | _TESTS = [{ | |
125 | 'url': 'https://www.newgrounds.com/collection/cats', | |
126 | 'info_dict': { | |
127 | 'id': 'cats', | |
128 | 'title': 'Cats', | |
129 | }, | |
130 | 'playlist_mincount': 46, | |
131 | }, { | |
132 | 'url': 'http://www.newgrounds.com/portal/search/author/ZONE-SAMA', | |
133 | 'info_dict': { | |
134 | 'id': 'ZONE-SAMA', | |
135 | 'title': 'Portal Search: ZONE-SAMA', | |
136 | }, | |
137 | 'playlist_mincount': 47, | |
138 | }, { | |
139 | 'url': 'http://www.newgrounds.com/audio/search/title/cats', | |
140 | 'only_matching': True, | |
141 | }] | |
142 | ||
143 | def _real_extract(self, url): | |
144 | playlist_id = self._match_id(url) | |
145 | ||
146 | webpage = self._download_webpage(url, playlist_id) | |
147 | ||
148 | title = self._search_regex( | |
149 | r'<title>([^>]+)</title>', webpage, 'title', default=None) | |
150 | ||
151 | # cut left menu | |
152 | webpage = self._search_regex( | |
153 | r'(?s)<div[^>]+\bclass=["\']column wide(.+)', | |
154 | webpage, 'wide column', default=webpage) | |
155 | ||
156 | entries = [] | |
157 | for a, path, media_id in re.findall( | |
158 | r'(<a[^>]+\bhref=["\']/?((?:portal/view|audio/listen)/(\d+))[^>]+>)', | |
159 | webpage): | |
160 | a_class = extract_attributes(a).get('class') | |
161 | if a_class not in ('item-portalsubmission', 'item-audiosubmission'): | |
162 | continue | |
163 | entries.append( | |
164 | self.url_result( | |
165 | 'https://www.newgrounds.com/%s' % path, | |
166 | ie=NewgroundsIE.ie_key(), video_id=media_id)) | |
167 | ||
168 | return self.playlist_result(entries, playlist_id, title) |