1 from __future__ import unicode_literals
8 from .common import InfoExtractor
26 class BandcampIE(InfoExtractor):
27 _VALID_URL = r'https?://.*?\.bandcamp\.com/track/(?P<title>[^/?#&]+)'
29 'url': 'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song',
30 'md5': 'c557841d5e50261777a6585648adf439',
34 'title': "youtube-dl \"'/\\\u00e4\u21ad - youtube-dl test song \"'/\\\u00e4\u21ad",
37 '_skip': 'There is a limit of 200 free downloads / month for the test song'
39 'url': 'http://benprunty.bandcamp.com/track/lanius-battle',
40 'md5': '0369ace6b939f0927e62c67a1a8d9fa7',
44 'title': 'Ben Prunty - Lanius (Battle)',
45 'uploader': 'Ben Prunty',
49 def _real_extract(self, url):
50 mobj = re.match(self._VALID_URL, url)
51 title = mobj.group('title')
52 webpage = self._download_webpage(url, title)
53 thumbnail = self._html_search_meta('og:image', webpage, default=None)
54 m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
56 m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
58 json_code = m_trackinfo.group(1)
59 data = json.loads(json_code)[0]
60 track_id = compat_str(data['id'])
62 if not data.get('file'):
63 raise ExtractorError('Not streamable', video_id=track_id, expected=True)
66 for format_id, format_url in data['file'].items():
67 ext, abr_str = format_id.split('-', 1)
69 'format_id': format_id,
70 'url': self._proto_relative_url(format_url, 'http:'),
74 'abr': int_or_none(abr_str),
77 self._sort_formats(formats)
81 'title': data['title'],
82 'thumbnail': thumbnail,
84 'duration': float_or_none(data.get('duration')),
87 raise ExtractorError('No free songs found')
89 download_link = m_download.group(1)
90 video_id = self._search_regex(
91 r'(?ms)var TralbumData = .*?[{,]\s*id: (?P<id>\d+),?$',
94 download_webpage = self._download_webpage(
95 download_link, video_id, 'Downloading free downloads page')
97 blob = self._parse_json(
99 r'data-blob=(["\'])(?P<blob>{.+?})\1', download_webpage,
100 'blob', group='blob'),
101 video_id, transform_source=unescapeHTML)
103 info = blob['digital_items'][0]
105 downloads = info['downloads']
106 track = info['title']
108 artist = info.get('artist')
109 title = '%s - %s' % (artist, track) if artist else track
111 download_formats = {}
112 for f in blob['download_formats']:
113 name, ext = f.get('name'), f.get('file_extension')
114 if all(isinstance(x, compat_str) for x in (name, ext)):
115 download_formats[name] = ext.strip('.')
118 for format_id, f in downloads.items():
119 format_url = f.get('url')
122 # Stat URL generation algorithm is reverse engineered from
123 # download_*_bundle_*.js
124 stat_url = update_url_query(
125 format_url.replace('/download/', '/statdownload/'), {
126 '.rand': int(time.time() * 1000 * random.random()),
128 format_id = f.get('encoding_name') or format_id
129 stat = self._download_json(
130 stat_url, video_id, 'Downloading %s JSON' % format_id,
131 transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1],
135 retry_url = url_or_none(stat.get('retry_url'))
139 'url': self._proto_relative_url(retry_url, 'http:'),
140 'ext': download_formats.get(format_id),
141 'format_id': format_id,
142 'format_note': f.get('description'),
143 'filesize': parse_filesize(f.get('size_mb')),
146 self._sort_formats(formats)
151 'thumbnail': info.get('thumb_url') or thumbnail,
152 'uploader': info.get('artist'),
159 class BandcampAlbumIE(InfoExtractor):
160 IE_NAME = 'Bandcamp:album'
161 _VALID_URL = r'https?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com(?:/album/(?P<album_id>[^/?#&]+))?'
164 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
167 'md5': '39bc1eded3476e927c724321ddf116cf',
175 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
179 'title': 'Kero One - Keep It Alive (Blazo remix)',
184 'title': 'Jazz Format Mixtape vol.1',
185 'id': 'jazz-format-mixtape-vol-1',
186 'uploader_id': 'blazo',
191 'skip': 'Bandcamp imposes download limits.'
193 'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave',
195 'title': 'Hierophany of the Open Grave',
196 'uploader_id': 'nightbringer',
197 'id': 'hierophany-of-the-open-grave',
199 'playlist_mincount': 9,
201 'url': 'http://dotscale.bandcamp.com',
205 'uploader_id': 'dotscale',
207 'playlist_mincount': 7,
209 # with escaped quote in title
210 'url': 'https://jstrecords.bandcamp.com/album/entropy-ep',
212 'title': '"Entropy" EP',
213 'uploader_id': 'jstrecords',
216 'playlist_mincount': 3,
218 # not all tracks have songs
219 'url': 'https://insulters.bandcamp.com/album/we-are-the-plague',
221 'id': 'we-are-the-plague',
222 'title': 'WE ARE THE PLAGUE',
223 'uploader_id': 'insulters',
229 def suitable(cls, url):
231 if BandcampWeeklyIE.suitable(url) or BandcampIE.suitable(url)
232 else super(BandcampAlbumIE, cls).suitable(url))
234 def _real_extract(self, url):
235 mobj = re.match(self._VALID_URL, url)
236 uploader_id = mobj.group('subdomain')
237 album_id = mobj.group('album_id')
238 playlist_id = album_id or uploader_id
239 webpage = self._download_webpage(url, playlist_id)
240 track_elements = re.findall(
241 r'(?s)<div[^>]*>(.*?<a[^>]+href="([^"]+?)"[^>]+itemprop="url"[^>]*>.*?)</div>', webpage)
242 if not track_elements:
243 raise ExtractorError('The page doesn\'t contain any tracks')
244 # Only tracks with duration info have songs
247 compat_urlparse.urljoin(url, t_path),
248 ie=BandcampIE.ie_key(),
249 video_title=self._search_regex(
250 r'<span\b[^>]+\bitemprop=["\']name["\'][^>]*>([^<]+)',
251 elem_content, 'track title', fatal=False))
252 for elem_content, t_path in track_elements
253 if self._html_search_meta('duration', elem_content, default=None)]
255 title = self._html_search_regex(
256 r'album_title\s*:\s*"((?:\\.|[^"\\])+?)"',
257 webpage, 'title', fatal=False)
259 title = title.replace(r'\"', '"')
262 'uploader_id': uploader_id,
269 class BandcampWeeklyIE(InfoExtractor):
270 IE_NAME = 'Bandcamp:weekly'
271 _VALID_URL = r'https?://(?:www\.)?bandcamp\.com/?\?(?:.*?&)?show=(?P<id>\d+)'
273 'url': 'https://bandcamp.com/?show=224',
274 'md5': 'b00df799c733cf7e0c567ed187dea0fd',
278 'title': 'BC Weekly April 4th 2017 - Magic Moments',
279 'description': 'md5:5d48150916e8e02d030623a48512c874',
281 'release_date': '20170404',
282 'series': 'Bandcamp Weekly',
283 'episode': 'Magic Moments',
284 'episode_number': 208,
288 'url': 'https://bandcamp.com/?blah/blah@&show=228',
289 'only_matching': True
292 def _real_extract(self, url):
293 video_id = self._match_id(url)
294 webpage = self._download_webpage(url, video_id)
296 blob = self._parse_json(
298 r'data-blob=(["\'])(?P<blob>{.+?})\1', webpage,
299 'blob', group='blob'),
300 video_id, transform_source=unescapeHTML)
302 show = blob['bcw_show']
304 # This is desired because any invalid show id redirects to `bandcamp.com`
305 # which happens to expose the latest Bandcamp Weekly episode.
306 show_id = int_or_none(show.get('show_id')) or int_or_none(video_id)
309 for format_id, format_url in show['audio_stream'].items():
310 if not url_or_none(format_url):
312 for known_ext in KNOWN_EXTENSIONS:
313 if known_ext in format_id:
319 'format_id': format_id,
324 self._sort_formats(formats)
326 title = show.get('audio_title') or 'Bandcamp Weekly'
327 subtitle = show.get('subtitle')
329 title += ' - %s' % subtitle
331 episode_number = None
332 seq = blob.get('bcw_seq')
334 if seq and isinstance(seq, list):
336 episode_number = next(
337 int_or_none(e.get('episode_number'))
339 if isinstance(e, dict) and int_or_none(e.get('id')) == show_id)
340 except StopIteration:
346 'description': show.get('desc') or show.get('short_desc'),
347 'duration': float_or_none(show.get('audio_duration')),
349 'release_date': unified_strdate(show.get('published_date')),
350 'series': 'Bandcamp Weekly',
351 'episode': show.get('subtitle'),
352 'episode_number': episode_number,
353 'episode_id': compat_str(video_id),