1 from __future__ import unicode_literals
5 from .common import InfoExtractor
15 class NewgroundsIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:audio/listen|portal/view)/(?P<id>[0-9]+)'
18 'url': 'https://www.newgrounds.com/audio/listen/549479',
19 'md5': 'fe6033d297591288fa1c1f780386f07a',
23 'title': 'B7 - BusMode',
25 'timestamp': 1378878540,
26 'upload_date': '20130911',
30 'url': 'https://www.newgrounds.com/portal/view/673111',
31 'md5': '3394735822aab2478c31b1004fe5e5bc',
36 'uploader': 'Squirrelman82',
37 'timestamp': 1460256780,
38 'upload_date': '20160410',
41 # source format unavailable, additional mp4 formats
42 'url': 'http://www.newgrounds.com/portal/view/689400',
46 'title': 'ZTV News Episode 8',
47 'uploader': 'BennettTheSage',
48 'timestamp': 1487965140,
49 'upload_date': '20170224',
52 'skip_download': True,
56 def _real_extract(self, url):
57 media_id = self._match_id(url)
59 webpage = self._download_webpage(url, media_id)
61 title = self._html_search_regex(
62 r'<title>([^>]+)</title>', webpage, 'title')
64 media_url = self._parse_json(self._search_regex(
65 r'"url"\s*:\s*("[^"]+"),', webpage, ''), media_id)
69 'format_id': 'source',
73 max_resolution = int_or_none(self._search_regex(
74 r'max_resolution["\']\s*:\s*(\d+)', webpage, 'max resolution',
77 url_base = media_url.rpartition('.')[0]
78 for resolution in (360, 720, 1080):
79 if resolution > max_resolution:
82 'url': '%s.%dp.mp4' % (url_base, resolution),
83 'format_id': '%dp' % resolution,
87 self._check_formats(formats, media_id)
88 self._sort_formats(formats)
90 uploader = self._html_search_regex(
91 (r'(?s)<h4[^>]*>(.+?)</h4>.*?<em>\s*Author\s*</em>',
92 r'(?:Author|Writer)\s*<a[^>]+>([^<]+)'), webpage, 'uploader',
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',
99 duration = parse_duration(self._search_regex(
100 r'(?s)<dd>\s*Song\s*</dd>\s*<dd>.+?</dd>\s*<dd>([^<]+)', webpage,
101 'duration', default=None))
103 filesize_approx = parse_filesize(self._html_search_regex(
104 r'(?s)<dd>\s*Song\s*</dd>\s*<dd>(.+?)</dd>', webpage, 'filesize',
106 if len(formats) == 1:
107 formats[0]['filesize_approx'] = filesize_approx
109 if '<dd>Song' in webpage:
110 formats[0]['vcodec'] = 'none'
115 'uploader': uploader,
116 'timestamp': timestamp,
117 'duration': duration,
122 class NewgroundsPlaylistIE(InfoExtractor):
123 _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:collection|[^/]+/search/[^/]+)/(?P<id>[^/?#&]+)'
125 'url': 'https://www.newgrounds.com/collection/cats',
130 'playlist_mincount': 46,
132 'url': 'http://www.newgrounds.com/portal/search/author/ZONE-SAMA',
135 'title': 'Portal Search: ZONE-SAMA',
137 'playlist_mincount': 47,
139 'url': 'http://www.newgrounds.com/audio/search/title/cats',
140 'only_matching': True,
143 def _real_extract(self, url):
144 playlist_id = self._match_id(url)
146 webpage = self._download_webpage(url, playlist_id)
148 title = self._search_regex(
149 r'<title>([^>]+)</title>', webpage, 'title', default=None)
152 webpage = self._search_regex(
153 r'(?s)<div[^>]+\bclass=["\']column wide(.+)',
154 webpage, 'wide column', default=webpage)
157 for a, path, media_id in re.findall(
158 r'(<a[^>]+\bhref=["\']/?((?:portal/view|audio/listen)/(\d+))[^>]+>)',
160 a_class = extract_attributes(a).get('class')
161 if a_class not in ('item-portalsubmission', 'item-audiosubmission'):
165 'https://www.newgrounds.com/%s' % path,
166 ie=NewgroundsIE.ie_key(), video_id=media_id))
168 return self.playlist_result(entries, playlist_id, title)