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',
48 'url': 'https://relapsealumni.bandcamp.com/track/hail-to-fire',
52 'title': 'Hail to Fire',
56 'skip_download': True,
60 def _real_extract(self, url):
61 mobj = re.match(self._VALID_URL, url)
62 title = mobj.group('title')
63 webpage = self._download_webpage(url, title)
64 thumbnail = self._html_search_meta('og:image', webpage, default=None)
65 m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
67 m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
69 json_code = m_trackinfo.group(1)
70 data = json.loads(json_code)[0]
71 track_id = compat_str(data['id'])
73 if not data.get('file'):
74 raise ExtractorError('Not streamable', video_id=track_id, expected=True)
77 for format_id, format_url in data['file'].items():
78 ext, abr_str = format_id.split('-', 1)
80 'format_id': format_id,
81 'url': self._proto_relative_url(format_url, 'http:'),
85 'abr': int_or_none(abr_str),
88 self._sort_formats(formats)
92 'title': data['title'],
93 'thumbnail': thumbnail,
95 'duration': float_or_none(data.get('duration')),
96 'track_number': int_or_none(data.get('track_num')),
99 raise ExtractorError('No free songs found')
101 download_link = m_download.group(1)
102 video_id = self._search_regex(
103 r'(?ms)var TralbumData = .*?[{,]\s*id: (?P<id>\d+),?$',
106 download_webpage = self._download_webpage(
107 download_link, video_id, 'Downloading free downloads page')
109 blob = self._parse_json(
111 r'data-blob=(["\'])(?P<blob>{.+?})\1', download_webpage,
112 'blob', group='blob'),
113 video_id, transform_source=unescapeHTML)
115 info = blob['digital_items'][0]
117 downloads = info['downloads']
118 track = info['title']
120 artist = info.get('artist')
121 title = '%s - %s' % (artist, track) if artist else track
123 download_formats = {}
124 for f in blob['download_formats']:
125 name, ext = f.get('name'), f.get('file_extension')
126 if all(isinstance(x, compat_str) for x in (name, ext)):
127 download_formats[name] = ext.strip('.')
130 for format_id, f in downloads.items():
131 format_url = f.get('url')
134 # Stat URL generation algorithm is reverse engineered from
135 # download_*_bundle_*.js
136 stat_url = update_url_query(
137 format_url.replace('/download/', '/statdownload/'), {
138 '.rand': int(time.time() * 1000 * random.random()),
140 format_id = f.get('encoding_name') or format_id
141 stat = self._download_json(
142 stat_url, video_id, 'Downloading %s JSON' % format_id,
143 transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1],
147 retry_url = url_or_none(stat.get('retry_url'))
151 'url': self._proto_relative_url(retry_url, 'http:'),
152 'ext': download_formats.get(format_id),
153 'format_id': format_id,
154 'format_note': f.get('description'),
155 'filesize': parse_filesize(f.get('size_mb')),
158 self._sort_formats(formats)
163 'thumbnail': info.get('thumb_url') or thumbnail,
164 'uploader': info.get('artist'),
171 class BandcampAlbumIE(InfoExtractor):
172 IE_NAME = 'Bandcamp:album'
173 _VALID_URL = r'https?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com(?:/album/(?P<album_id>[^/?#&]+))?'
176 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
179 'md5': '39bc1eded3476e927c724321ddf116cf',
187 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
191 'title': 'Kero One - Keep It Alive (Blazo remix)',
196 'title': 'Jazz Format Mixtape vol.1',
197 'id': 'jazz-format-mixtape-vol-1',
198 'uploader_id': 'blazo',
203 'skip': 'Bandcamp imposes download limits.'
205 'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave',
207 'title': 'Hierophany of the Open Grave',
208 'uploader_id': 'nightbringer',
209 'id': 'hierophany-of-the-open-grave',
211 'playlist_mincount': 9,
213 'url': 'http://dotscale.bandcamp.com',
217 'uploader_id': 'dotscale',
219 'playlist_mincount': 7,
221 # with escaped quote in title
222 'url': 'https://jstrecords.bandcamp.com/album/entropy-ep',
224 'title': '"Entropy" EP',
225 'uploader_id': 'jstrecords',
228 'playlist_mincount': 3,
230 # not all tracks have songs
231 'url': 'https://insulters.bandcamp.com/album/we-are-the-plague',
233 'id': 'we-are-the-plague',
234 'title': 'WE ARE THE PLAGUE',
235 'uploader_id': 'insulters',
241 def suitable(cls, url):
243 if BandcampWeeklyIE.suitable(url) or BandcampIE.suitable(url)
244 else super(BandcampAlbumIE, cls).suitable(url))
246 def _real_extract(self, url):
247 mobj = re.match(self._VALID_URL, url)
248 uploader_id = mobj.group('subdomain')
249 album_id = mobj.group('album_id')
250 playlist_id = album_id or uploader_id
251 webpage = self._download_webpage(url, playlist_id)
252 track_elements = re.findall(
253 r'(?s)<div[^>]*>(.*?<a[^>]+href="([^"]+?)"[^>]+itemprop="url"[^>]*>.*?)</div>', webpage)
254 if not track_elements:
255 raise ExtractorError('The page doesn\'t contain any tracks')
256 # Only tracks with duration info have songs
259 compat_urlparse.urljoin(url, t_path),
260 ie=BandcampIE.ie_key(),
261 video_title=self._search_regex(
262 r'<span\b[^>]+\bitemprop=["\']name["\'][^>]*>([^<]+)',
263 elem_content, 'track title', fatal=False))
264 for elem_content, t_path in track_elements
265 if self._html_search_meta('duration', elem_content, default=None)]
267 title = self._html_search_regex(
268 r'album_title\s*:\s*"((?:\\.|[^"\\])+?)"',
269 webpage, 'title', fatal=False)
271 title = title.replace(r'\"', '"')
274 'uploader_id': uploader_id,
281 class BandcampWeeklyIE(InfoExtractor):
282 IE_NAME = 'Bandcamp:weekly'
283 _VALID_URL = r'https?://(?:www\.)?bandcamp\.com/?\?(?:.*?&)?show=(?P<id>\d+)'
285 'url': 'https://bandcamp.com/?show=224',
286 'md5': 'b00df799c733cf7e0c567ed187dea0fd',
290 'title': 'BC Weekly April 4th 2017 - Magic Moments',
291 'description': 'md5:5d48150916e8e02d030623a48512c874',
293 'release_date': '20170404',
294 'series': 'Bandcamp Weekly',
295 'episode': 'Magic Moments',
296 'episode_number': 208,
300 'url': 'https://bandcamp.com/?blah/blah@&show=228',
301 'only_matching': True
304 def _real_extract(self, url):
305 video_id = self._match_id(url)
306 webpage = self._download_webpage(url, video_id)
308 blob = self._parse_json(
310 r'data-blob=(["\'])(?P<blob>{.+?})\1', webpage,
311 'blob', group='blob'),
312 video_id, transform_source=unescapeHTML)
314 show = blob['bcw_show']
316 # This is desired because any invalid show id redirects to `bandcamp.com`
317 # which happens to expose the latest Bandcamp Weekly episode.
318 show_id = int_or_none(show.get('show_id')) or int_or_none(video_id)
321 for format_id, format_url in show['audio_stream'].items():
322 if not url_or_none(format_url):
324 for known_ext in KNOWN_EXTENSIONS:
325 if known_ext in format_id:
331 'format_id': format_id,
336 self._sort_formats(formats)
338 title = show.get('audio_title') or 'Bandcamp Weekly'
339 subtitle = show.get('subtitle')
341 title += ' - %s' % subtitle
343 episode_number = None
344 seq = blob.get('bcw_seq')
346 if seq and isinstance(seq, list):
348 episode_number = next(
349 int_or_none(e.get('episode_number'))
351 if isinstance(e, dict) and int_or_none(e.get('id')) == show_id)
352 except StopIteration:
358 'description': show.get('desc') or show.get('short_desc'),
359 'duration': float_or_none(show.get('audio_duration')),
361 'release_date': unified_strdate(show.get('published_date')),
362 'series': 'Bandcamp Weekly',
363 'episode': show.get('subtitle'),
364 'episode_number': episode_number,
365 'episode_id': compat_str(video_id),