]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/newgrounds.py
debian/README.source: Add my preference with respect to patches.
[youtubedl] / youtube_dl / extractor / newgrounds.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7 extract_attributes,
8 int_or_none,
9 parse_duration,
10 parse_filesize,
11 unified_timestamp,
12 )
13
14
15 class NewgroundsIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:audio/listen|portal/view)/(?P<id>[0-9]+)'
17 _TESTS = [{
18 'url': 'https://www.newgrounds.com/audio/listen/549479',
19 'md5': 'fe6033d297591288fa1c1f780386f07a',
20 'info_dict': {
21 'id': '549479',
22 'ext': 'mp3',
23 'title': 'B7 - BusMode',
24 'uploader': 'Burn7',
25 'timestamp': 1378878540,
26 'upload_date': '20130911',
27 'duration': 143,
28 },
29 }, {
30 'url': 'https://www.newgrounds.com/portal/view/673111',
31 'md5': '3394735822aab2478c31b1004fe5e5bc',
32 'info_dict': {
33 'id': '673111',
34 'ext': 'mp4',
35 'title': 'Dancin',
36 'uploader': 'Squirrelman82',
37 'timestamp': 1460256780,
38 'upload_date': '20160410',
39 },
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',
48 'timestamp': 1487965140,
49 'upload_date': '20170224',
50 },
51 'params': {
52 'skip_download': True,
53 },
54 }]
55
56 def _real_extract(self, url):
57 media_id = self._match_id(url)
58
59 webpage = self._download_webpage(url, media_id)
60
61 title = self._html_search_regex(
62 r'<title>([^>]+)</title>', webpage, 'title')
63
64 media_url = self._parse_json(self._search_regex(
65 r'"url"\s*:\s*("[^"]+"),', webpage, ''), media_id)
66
67 formats = [{
68 'url': media_url,
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:
77 url_base = media_url.rpartition('.')[0]
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 })
86
87 self._check_formats(formats, media_id)
88 self._sort_formats(formats)
89
90 uploader = self._search_regex(
91 r'(?:Author|Writer)\s*<a[^>]+>([^<]+)', webpage, 'uploader',
92 fatal=False)
93
94 timestamp = unified_timestamp(self._search_regex(
95 r'<dt>Uploaded</dt>\s*<dd>([^<]+)', webpage, 'timestamp',
96 default=None))
97 duration = parse_duration(self._search_regex(
98 r'<dd>Song\s*</dd><dd>.+?</dd><dd>([^<]+)', webpage, 'duration',
99 default=None))
100
101 filesize_approx = parse_filesize(self._html_search_regex(
102 r'<dd>Song\s*</dd><dd>(.+?)</dd>', webpage, 'filesize',
103 default=None))
104 if len(formats) == 1:
105 formats[0]['filesize_approx'] = filesize_approx
106
107 if '<dd>Song' in webpage:
108 formats[0]['vcodec'] = 'none'
109
110 return {
111 'id': media_id,
112 'title': title,
113 'uploader': uploader,
114 'timestamp': timestamp,
115 'duration': duration,
116 'formats': formats,
117 }
118
119
120 class NewgroundsPlaylistIE(InfoExtractor):
121 _VALID_URL = r'https?://(?:www\.)?newgrounds\.com/(?:collection|[^/]+/search/[^/]+)/(?P<id>[^/?#&]+)'
122 _TESTS = [{
123 'url': 'https://www.newgrounds.com/collection/cats',
124 'info_dict': {
125 'id': 'cats',
126 'title': 'Cats',
127 },
128 'playlist_mincount': 46,
129 }, {
130 'url': 'http://www.newgrounds.com/portal/search/author/ZONE-SAMA',
131 'info_dict': {
132 'id': 'ZONE-SAMA',
133 'title': 'Portal Search: ZONE-SAMA',
134 },
135 'playlist_mincount': 47,
136 }, {
137 'url': 'http://www.newgrounds.com/audio/search/title/cats',
138 'only_matching': True,
139 }]
140
141 def _real_extract(self, url):
142 playlist_id = self._match_id(url)
143
144 webpage = self._download_webpage(url, playlist_id)
145
146 title = self._search_regex(
147 r'<title>([^>]+)</title>', webpage, 'title', default=None)
148
149 # cut left menu
150 webpage = self._search_regex(
151 r'(?s)<div[^>]+\bclass=["\']column wide(.+)',
152 webpage, 'wide column', default=webpage)
153
154 entries = []
155 for a, path, media_id in re.findall(
156 r'(<a[^>]+\bhref=["\']/?((?:portal/view|audio/listen)/(\d+))[^>]+>)',
157 webpage):
158 a_class = extract_attributes(a).get('class')
159 if a_class not in ('item-portalsubmission', 'item-audiosubmission'):
160 continue
161 entries.append(
162 self.url_result(
163 'https://www.newgrounds.com/%s' % path,
164 ie=NewgroundsIE.ie_key(), video_id=media_id))
165
166 return self.playlist_result(entries, playlist_id, title)