]>
Commit | Line | Data |
---|---|---|
8bdd16b4 | 1 | # coding: utf-8 |
3798eadc PH |
2 | from __future__ import unicode_literals |
3 | ||
0aacd2de | 4 | import random |
45aef472 | 5 | import re |
0aacd2de | 6 | import time |
45aef472 PH |
7 | |
8 | from .common import InfoExtractor | |
8bdd16b4 | 9 | from ..compat import compat_str |
1cc79574 | 10 | from ..utils import ( |
45aef472 | 11 | ExtractorError, |
ba717dca S |
12 | float_or_none, |
13 | int_or_none, | |
6d923aab | 14 | KNOWN_EXTENSIONS, |
0aacd2de | 15 | parse_filesize, |
4991e16c S |
16 | str_or_none, |
17 | try_get, | |
0aacd2de | 18 | update_url_query, |
62bafabc | 19 | unified_strdate, |
4991e16c | 20 | unified_timestamp, |
3052a30d | 21 | url_or_none, |
8bdd16b4 | 22 | urljoin, |
45aef472 PH |
23 | ) |
24 | ||
25 | ||
8bdd16b4 | 26 | class BandcampIE(InfoExtractor): |
27 | _VALID_URL = r'https?://[^/]+\.bandcamp\.com/track/(?P<id>[^/?#&]+)' | |
cffa6aa1 | 28 | _TESTS = [{ |
3467b3e2 | 29 | 'url': 'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song', |
3798eadc PH |
30 | 'md5': 'c557841d5e50261777a6585648adf439', |
31 | 'info_dict': { | |
d9bf4652 S |
32 | 'id': '1812978515', |
33 | 'ext': 'mp3', | |
8bdd16b4 | 34 | 'title': "youtube-dl \"'/\\ä↭ - youtube-dl \"'/\\ä↭ - youtube-dl test song \"'/\\ä↭", |
d9bf4652 | 35 | 'duration': 9.8485, |
8bdd16b4 | 36 | 'uploader': 'youtube-dl "\'/\\ä↭', |
3a379e5e | 37 | 'upload_date': '20121129', |
8bdd16b4 | 38 | 'timestamp': 1354224127, |
6f5ac90c | 39 | }, |
3798eadc | 40 | '_skip': 'There is a limit of 200 free downloads / month for the test song' |
d9bf4652 | 41 | }, { |
4991e16c | 42 | # free download |
d9bf4652 | 43 | 'url': 'http://benprunty.bandcamp.com/track/lanius-battle', |
d9bf4652 S |
44 | 'info_dict': { |
45 | 'id': '2650410135', | |
0f63dc24 TF |
46 | 'ext': 'aiff', |
47 | 'title': 'Ben Prunty - Lanius (Battle)', | |
4991e16c | 48 | 'thumbnail': r're:^https?://.*\.jpg$', |
0f63dc24 | 49 | 'uploader': 'Ben Prunty', |
4991e16c S |
50 | 'timestamp': 1396508491, |
51 | 'upload_date': '20140403', | |
52 | 'release_date': '20140403', | |
53 | 'duration': 260.877, | |
54 | 'track': 'Lanius (Battle)', | |
55 | 'track_number': 1, | |
56 | 'track_id': '2650410135', | |
57 | 'artist': 'Ben Prunty', | |
58 | 'album': 'FTL: Advanced Edition Soundtrack', | |
d9bf4652 | 59 | }, |
14b7a24c | 60 | }, { |
4991e16c | 61 | # no free download, mp3 128 |
14b7a24c | 62 | 'url': 'https://relapsealumni.bandcamp.com/track/hail-to-fire', |
4991e16c | 63 | 'md5': 'fec12ff55e804bb7f7ebeb77a800c8b7', |
14b7a24c PV |
64 | 'info_dict': { |
65 | 'id': '2584466013', | |
66 | 'ext': 'mp3', | |
4991e16c S |
67 | 'title': 'Mastodon - Hail to Fire', |
68 | 'thumbnail': r're:^https?://.*\.jpg$', | |
69 | 'uploader': 'Mastodon', | |
70 | 'timestamp': 1322005399, | |
71 | 'upload_date': '20111122', | |
72 | 'release_date': '20040207', | |
73 | 'duration': 120.79, | |
74 | 'track': 'Hail to Fire', | |
14b7a24c | 75 | 'track_number': 5, |
4991e16c S |
76 | 'track_id': '2584466013', |
77 | 'artist': 'Mastodon', | |
78 | 'album': 'Call of the Mastodon', | |
14b7a24c | 79 | }, |
cffa6aa1 | 80 | }] |
45aef472 | 81 | |
8bdd16b4 | 82 | def _extract_data_attr(self, webpage, video_id, attr='tralbum', fatal=True): |
83 | return self._parse_json(self._html_search_regex( | |
84 | r'data-%s=(["\'])({.+?})\1' % attr, webpage, | |
85 | attr + ' data', group=2), video_id, fatal=fatal) | |
86 | ||
45aef472 | 87 | def _real_extract(self, url): |
8bdd16b4 | 88 | title = self._match_id(url) |
45aef472 | 89 | webpage = self._download_webpage(url, title) |
8bdd16b4 | 90 | tralbum = self._extract_data_attr(webpage, title) |
91 | thumbnail = self._og_search_thumbnail(webpage) | |
92 | ||
93 | track_id = None | |
94 | track = None | |
95 | track_number = None | |
96 | duration = None | |
97 | ||
98 | formats = [] | |
99 | track_info = try_get(tralbum, lambda x: x['trackinfo'][0], dict) | |
100 | if track_info: | |
101 | file_ = track_info.get('file') | |
102 | if isinstance(file_, dict): | |
103 | for format_id, format_url in file_.items(): | |
104 | if not url_or_none(format_url): | |
105 | continue | |
106 | ext, abr_str = format_id.split('-', 1) | |
107 | formats.append({ | |
108 | 'format_id': format_id, | |
109 | 'url': self._proto_relative_url(format_url, 'http:'), | |
110 | 'ext': ext, | |
111 | 'vcodec': 'none', | |
112 | 'acodec': ext, | |
113 | 'abr': int_or_none(abr_str), | |
114 | }) | |
115 | track = track_info.get('title') | |
116 | track_id = str_or_none( | |
117 | track_info.get('track_id') or track_info.get('id')) | |
118 | track_number = int_or_none(track_info.get('track_num')) | |
119 | duration = float_or_none(track_info.get('duration')) | |
120 | ||
121 | embed = self._extract_data_attr(webpage, title, 'embed', False) | |
122 | current = tralbum.get('current') or {} | |
123 | artist = embed.get('artist') or current.get('artist') or tralbum.get('artist') | |
124 | timestamp = unified_timestamp( | |
125 | current.get('publish_date') or tralbum.get('album_publish_date')) | |
126 | ||
127 | download_link = tralbum.get('freeDownloadPage') | |
4991e16c | 128 | if download_link: |
8bdd16b4 | 129 | track_id = compat_str(tralbum['id']) |
4991e16c S |
130 | |
131 | download_webpage = self._download_webpage( | |
132 | download_link, track_id, 'Downloading free downloads page') | |
133 | ||
8bdd16b4 | 134 | blob = self._extract_data_attr(download_webpage, track_id, 'blob') |
4991e16c S |
135 | |
136 | info = try_get( | |
137 | blob, (lambda x: x['digital_items'][0], | |
138 | lambda x: x['download_items'][0]), dict) | |
139 | if info: | |
140 | downloads = info.get('downloads') | |
141 | if isinstance(downloads, dict): | |
8bdd16b4 | 142 | if not track: |
143 | track = info.get('title') | |
4991e16c S |
144 | if not artist: |
145 | artist = info.get('artist') | |
146 | if not thumbnail: | |
147 | thumbnail = info.get('thumb_url') | |
148 | ||
149 | download_formats = {} | |
150 | download_formats_list = blob.get('download_formats') | |
151 | if isinstance(download_formats_list, list): | |
152 | for f in blob['download_formats']: | |
153 | name, ext = f.get('name'), f.get('file_extension') | |
154 | if all(isinstance(x, compat_str) for x in (name, ext)): | |
155 | download_formats[name] = ext.strip('.') | |
156 | ||
157 | for format_id, f in downloads.items(): | |
158 | format_url = f.get('url') | |
159 | if not format_url: | |
160 | continue | |
161 | # Stat URL generation algorithm is reverse engineered from | |
162 | # download_*_bundle_*.js | |
163 | stat_url = update_url_query( | |
164 | format_url.replace('/download/', '/statdownload/'), { | |
165 | '.rand': int(time.time() * 1000 * random.random()), | |
166 | }) | |
167 | format_id = f.get('encoding_name') or format_id | |
168 | stat = self._download_json( | |
169 | stat_url, track_id, 'Downloading %s JSON' % format_id, | |
170 | transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1], | |
171 | fatal=False) | |
172 | if not stat: | |
173 | continue | |
174 | retry_url = url_or_none(stat.get('retry_url')) | |
175 | if not retry_url: | |
176 | continue | |
8bdd16b4 | 177 | formats.append({ |
4991e16c S |
178 | 'url': self._proto_relative_url(retry_url, 'http:'), |
179 | 'ext': download_formats.get(format_id), | |
180 | 'format_id': format_id, | |
181 | 'format_note': f.get('description'), | |
182 | 'filesize': parse_filesize(f.get('size_mb')), | |
183 | 'vcodec': 'none', | |
184 | }) | |
5ecd3c6a | 185 | |
8bdd16b4 | 186 | self._sort_formats(formats) |
0aacd2de | 187 | |
8bdd16b4 | 188 | title = '%s - %s' % (artist, track) if artist else track |
189 | ||
190 | if not duration: | |
191 | duration = float_or_none(self._html_search_meta( | |
192 | 'duration', webpage, default=None)) | |
45aef472 | 193 | |
5ecd3c6a | 194 | return { |
8bdd16b4 | 195 | 'id': track_id, |
196 | 'title': title, | |
4991e16c | 197 | 'thumbnail': thumbnail, |
8bdd16b4 | 198 | 'uploader': artist, |
4991e16c | 199 | 'timestamp': timestamp, |
8bdd16b4 | 200 | 'release_date': unified_strdate(tralbum.get('album_release_date')), |
201 | 'duration': duration, | |
202 | 'track': track, | |
203 | 'track_number': track_number, | |
204 | 'track_id': track_id, | |
205 | 'artist': artist, | |
206 | 'album': embed.get('album_title'), | |
207 | 'formats': formats, | |
5ecd3c6a | 208 | } |
09804265 JMF |
209 | |
210 | ||
8bdd16b4 | 211 | class BandcampAlbumIE(BandcampIE): |
3798eadc | 212 | IE_NAME = 'Bandcamp:album' |
8bdd16b4 | 213 | _VALID_URL = r'https?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com(?:/album/(?P<id>[^/?#&]+))?' |
09804265 | 214 | |
22a6f150 | 215 | _TESTS = [{ |
3798eadc PH |
216 | 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1', |
217 | 'playlist': [ | |
d35dc6d3 | 218 | { |
3798eadc PH |
219 | 'md5': '39bc1eded3476e927c724321ddf116cf', |
220 | 'info_dict': { | |
13ba3a64 PH |
221 | 'id': '1353101989', |
222 | 'ext': 'mp3', | |
8bdd16b4 | 223 | 'title': 'Blazo - Intro', |
224 | 'timestamp': 1311756226, | |
225 | 'upload_date': '20110727', | |
226 | 'uploader': 'Blazo', | |
d35dc6d3 JMF |
227 | } |
228 | }, | |
229 | { | |
3798eadc PH |
230 | 'md5': '1a2c32e2691474643e912cc6cd4bffaa', |
231 | 'info_dict': { | |
13ba3a64 PH |
232 | 'id': '38097443', |
233 | 'ext': 'mp3', | |
8bdd16b4 | 234 | 'title': 'Blazo - Kero One - Keep It Alive (Blazo remix)', |
235 | 'timestamp': 1311757238, | |
236 | 'upload_date': '20110727', | |
237 | 'uploader': 'Blazo', | |
d35dc6d3 JMF |
238 | } |
239 | }, | |
240 | ], | |
13ba3a64 PH |
241 | 'info_dict': { |
242 | 'title': 'Jazz Format Mixtape vol.1', | |
72c1f8de PH |
243 | 'id': 'jazz-format-mixtape-vol-1', |
244 | 'uploader_id': 'blazo', | |
13ba3a64 | 245 | }, |
3798eadc PH |
246 | 'params': { |
247 | 'playlistend': 2 | |
d35dc6d3 | 248 | }, |
72c1f8de | 249 | 'skip': 'Bandcamp imposes download limits.' |
22a6f150 PH |
250 | }, { |
251 | 'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave', | |
252 | 'info_dict': { | |
253 | 'title': 'Hierophany of the Open Grave', | |
72c1f8de PH |
254 | 'uploader_id': 'nightbringer', |
255 | 'id': 'hierophany-of-the-open-grave', | |
22a6f150 PH |
256 | }, |
257 | 'playlist_mincount': 9, | |
1fa17469 S |
258 | }, { |
259 | 'url': 'http://dotscale.bandcamp.com', | |
260 | 'info_dict': { | |
261 | 'title': 'Loom', | |
72c1f8de PH |
262 | 'id': 'dotscale', |
263 | 'uploader_id': 'dotscale', | |
1fa17469 S |
264 | }, |
265 | 'playlist_mincount': 7, | |
64fc49ab S |
266 | }, { |
267 | # with escaped quote in title | |
268 | 'url': 'https://jstrecords.bandcamp.com/album/entropy-ep', | |
269 | 'info_dict': { | |
270 | 'title': '"Entropy" EP', | |
271 | 'uploader_id': 'jstrecords', | |
272 | 'id': 'entropy-ep', | |
8bdd16b4 | 273 | 'description': 'md5:0ff22959c943622972596062f2f366a5', |
64fc49ab S |
274 | }, |
275 | 'playlist_mincount': 3, | |
019f4c03 YCH |
276 | }, { |
277 | # not all tracks have songs | |
278 | 'url': 'https://insulters.bandcamp.com/album/we-are-the-plague', | |
279 | 'info_dict': { | |
280 | 'id': 'we-are-the-plague', | |
281 | 'title': 'WE ARE THE PLAGUE', | |
282 | 'uploader_id': 'insulters', | |
8bdd16b4 | 283 | 'description': 'md5:b3cf845ee41b2b1141dc7bde9237255f', |
019f4c03 YCH |
284 | }, |
285 | 'playlist_count': 2, | |
22a6f150 | 286 | }] |
d35dc6d3 | 287 | |
62bafabc AV |
288 | @classmethod |
289 | def suitable(cls, url): | |
6d923aab S |
290 | return (False |
291 | if BandcampWeeklyIE.suitable(url) or BandcampIE.suitable(url) | |
292 | else super(BandcampAlbumIE, cls).suitable(url)) | |
62bafabc | 293 | |
09804265 | 294 | def _real_extract(self, url): |
8bdd16b4 | 295 | uploader_id, album_id = re.match(self._VALID_URL, url).groups() |
72c1f8de PH |
296 | playlist_id = album_id or uploader_id |
297 | webpage = self._download_webpage(url, playlist_id) | |
8bdd16b4 | 298 | tralbum = self._extract_data_attr(webpage, playlist_id) |
299 | track_info = tralbum.get('trackinfo') | |
300 | if not track_info: | |
301 | raise ExtractorError('The page doesn\'t contain any tracks') | |
019f4c03 | 302 | # Only tracks with duration info have songs |
09804265 | 303 | entries = [ |
8239c679 | 304 | self.url_result( |
8bdd16b4 | 305 | urljoin(url, t['title_link']), BandcampIE.ie_key(), |
306 | str_or_none(t.get('track_id') or t.get('id')), t.get('title')) | |
307 | for t in track_info | |
308 | if t.get('duration')] | |
309 | ||
310 | current = tralbum.get('current') or {} | |
3a379e5e | 311 | |
09804265 JMF |
312 | return { |
313 | '_type': 'playlist', | |
72c1f8de | 314 | 'uploader_id': uploader_id, |
b48f147d | 315 | 'id': playlist_id, |
8bdd16b4 | 316 | 'title': current.get('title'), |
317 | 'description': current.get('about'), | |
318 | 'entries': entries, | |
09804265 | 319 | } |
62bafabc AV |
320 | |
321 | ||
8bdd16b4 | 322 | class BandcampWeeklyIE(BandcampIE): |
6d923aab S |
323 | IE_NAME = 'Bandcamp:weekly' |
324 | _VALID_URL = r'https?://(?:www\.)?bandcamp\.com/?\?(?:.*?&)?show=(?P<id>\d+)' | |
62bafabc AV |
325 | _TESTS = [{ |
326 | 'url': 'https://bandcamp.com/?show=224', | |
327 | 'md5': 'b00df799c733cf7e0c567ed187dea0fd', | |
328 | 'info_dict': { | |
329 | 'id': '224', | |
330 | 'ext': 'opus', | |
6d923aab S |
331 | 'title': 'BC Weekly April 4th 2017 - Magic Moments', |
332 | 'description': 'md5:5d48150916e8e02d030623a48512c874', | |
333 | 'duration': 5829.77, | |
334 | 'release_date': '20170404', | |
335 | 'series': 'Bandcamp Weekly', | |
336 | 'episode': 'Magic Moments', | |
6d923aab | 337 | 'episode_id': '224', |
8bdd16b4 | 338 | }, |
339 | 'params': { | |
340 | 'format': 'opus-lo', | |
341 | }, | |
62bafabc AV |
342 | }, { |
343 | 'url': 'https://bandcamp.com/?blah/blah@&show=228', | |
344 | 'only_matching': True | |
345 | }] | |
346 | ||
347 | def _real_extract(self, url): | |
8bdd16b4 | 348 | show_id = self._match_id(url) |
349 | webpage = self._download_webpage(url, show_id) | |
62bafabc | 350 | |
8bdd16b4 | 351 | blob = self._extract_data_attr(webpage, show_id, 'blob') |
62bafabc | 352 | |
8bdd16b4 | 353 | show = blob['bcw_data'][show_id] |
62bafabc | 354 | |
6d923aab S |
355 | formats = [] |
356 | for format_id, format_url in show['audio_stream'].items(): | |
3052a30d | 357 | if not url_or_none(format_url): |
6d923aab S |
358 | continue |
359 | for known_ext in KNOWN_EXTENSIONS: | |
360 | if known_ext in format_id: | |
361 | ext = known_ext | |
362 | break | |
363 | else: | |
364 | ext = None | |
365 | formats.append({ | |
366 | 'format_id': format_id, | |
367 | 'url': format_url, | |
368 | 'ext': ext, | |
369 | 'vcodec': 'none', | |
370 | }) | |
371 | self._sort_formats(formats) | |
62bafabc | 372 | |
6d923aab S |
373 | title = show.get('audio_title') or 'Bandcamp Weekly' |
374 | subtitle = show.get('subtitle') | |
375 | if subtitle: | |
376 | title += ' - %s' % subtitle | |
62bafabc | 377 | |
62bafabc | 378 | return { |
8bdd16b4 | 379 | 'id': show_id, |
6d923aab S |
380 | 'title': title, |
381 | 'description': show.get('desc') or show.get('short_desc'), | |
62bafabc | 382 | 'duration': float_or_none(show.get('audio_duration')), |
62bafabc AV |
383 | 'is_live': False, |
384 | 'release_date': unified_strdate(show.get('published_date')), | |
385 | 'series': 'Bandcamp Weekly', | |
6d923aab | 386 | 'episode': show.get('subtitle'), |
8bdd16b4 | 387 | 'episode_id': show_id, |
62bafabc AV |
388 | 'formats': formats |
389 | } |