1 from __future__ import unicode_literals
8 from .common import InfoExtractor
24 class BandcampIE(InfoExtractor):
25 _VALID_URL = r'https?://.*?\.bandcamp\.com/track/(?P<title>.*)'
27 'url': 'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song',
28 'md5': 'c557841d5e50261777a6585648adf439',
32 'title': "youtube-dl \"'/\\\u00e4\u21ad - youtube-dl test song \"'/\\\u00e4\u21ad",
35 '_skip': 'There is a limit of 200 free downloads / month for the test song'
37 'url': 'http://benprunty.bandcamp.com/track/lanius-battle',
38 'md5': '0369ace6b939f0927e62c67a1a8d9fa7',
42 'title': 'Ben Prunty - Lanius (Battle)',
43 'uploader': 'Ben Prunty',
47 def _real_extract(self, url):
48 mobj = re.match(self._VALID_URL, url)
49 title = mobj.group('title')
50 webpage = self._download_webpage(url, title)
51 thumbnail = self._html_search_meta('og:image', webpage, default=None)
52 m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
54 m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
56 json_code = m_trackinfo.group(1)
57 data = json.loads(json_code)[0]
58 track_id = compat_str(data['id'])
60 if not data.get('file'):
61 raise ExtractorError('Not streamable', video_id=track_id, expected=True)
64 for format_id, format_url in data['file'].items():
65 ext, abr_str = format_id.split('-', 1)
67 'format_id': format_id,
68 'url': self._proto_relative_url(format_url, 'http:'),
72 'abr': int_or_none(abr_str),
75 self._sort_formats(formats)
79 'title': data['title'],
80 'thumbnail': thumbnail,
82 'duration': float_or_none(data.get('duration')),
85 raise ExtractorError('No free songs found')
87 download_link = m_download.group(1)
88 video_id = self._search_regex(
89 r'(?ms)var TralbumData = .*?[{,]\s*id: (?P<id>\d+),?$',
92 download_webpage = self._download_webpage(
93 download_link, video_id, 'Downloading free downloads page')
95 blob = self._parse_json(
97 r'data-blob=(["\'])(?P<blob>{.+?})\1', download_webpage,
98 'blob', group='blob'),
99 video_id, transform_source=unescapeHTML)
101 info = blob['digital_items'][0]
103 downloads = info['downloads']
104 track = info['title']
106 artist = info.get('artist')
107 title = '%s - %s' % (artist, track) if artist else track
109 download_formats = {}
110 for f in blob['download_formats']:
111 name, ext = f.get('name'), f.get('file_extension')
112 if all(isinstance(x, compat_str) for x in (name, ext)):
113 download_formats[name] = ext.strip('.')
116 for format_id, f in downloads.items():
117 format_url = f.get('url')
120 # Stat URL generation algorithm is reverse engineered from
121 # download_*_bundle_*.js
122 stat_url = update_url_query(
123 format_url.replace('/download/', '/statdownload/'), {
124 '.rand': int(time.time() * 1000 * random.random()),
126 format_id = f.get('encoding_name') or format_id
127 stat = self._download_json(
128 stat_url, video_id, 'Downloading %s JSON' % format_id,
129 transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1],
133 retry_url = stat.get('retry_url')
134 if not isinstance(retry_url, compat_str):
137 'url': self._proto_relative_url(retry_url, 'http:'),
138 'ext': download_formats.get(format_id),
139 'format_id': format_id,
140 'format_note': f.get('description'),
141 'filesize': parse_filesize(f.get('size_mb')),
144 self._sort_formats(formats)
149 'thumbnail': info.get('thumb_url') or thumbnail,
150 'uploader': info.get('artist'),
157 class BandcampAlbumIE(InfoExtractor):
158 IE_NAME = 'Bandcamp:album'
159 _VALID_URL = r'https?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com(?:/album/(?P<album_id>[^?#]+)|/?(?:$|[?#]))'
162 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
165 'md5': '39bc1eded3476e927c724321ddf116cf',
173 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
177 'title': 'Kero One - Keep It Alive (Blazo remix)',
182 'title': 'Jazz Format Mixtape vol.1',
183 'id': 'jazz-format-mixtape-vol-1',
184 'uploader_id': 'blazo',
189 'skip': 'Bandcamp imposes download limits.'
191 'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave',
193 'title': 'Hierophany of the Open Grave',
194 'uploader_id': 'nightbringer',
195 'id': 'hierophany-of-the-open-grave',
197 'playlist_mincount': 9,
199 'url': 'http://dotscale.bandcamp.com',
203 'uploader_id': 'dotscale',
205 'playlist_mincount': 7,
207 # with escaped quote in title
208 'url': 'https://jstrecords.bandcamp.com/album/entropy-ep',
210 'title': '"Entropy" EP',
211 'uploader_id': 'jstrecords',
214 'playlist_mincount': 3,
216 # not all tracks have songs
217 'url': 'https://insulters.bandcamp.com/album/we-are-the-plague',
219 'id': 'we-are-the-plague',
220 'title': 'WE ARE THE PLAGUE',
221 'uploader_id': 'insulters',
227 def suitable(cls, url):
228 return False if BandcampWeeklyIE.suitable(url) else super(BandcampAlbumIE, cls).suitable(url)
230 def _real_extract(self, url):
231 mobj = re.match(self._VALID_URL, url)
232 uploader_id = mobj.group('subdomain')
233 album_id = mobj.group('album_id')
234 playlist_id = album_id or uploader_id
235 webpage = self._download_webpage(url, playlist_id)
236 track_elements = re.findall(
237 r'(?s)<div[^>]*>(.*?<a[^>]+href="([^"]+?)"[^>]+itemprop="url"[^>]*>.*?)</div>', webpage)
238 if not track_elements:
239 raise ExtractorError('The page doesn\'t contain any tracks')
240 # Only tracks with duration info have songs
242 self.url_result(compat_urlparse.urljoin(url, t_path), ie=BandcampIE.ie_key())
243 for elem_content, t_path in track_elements
244 if self._html_search_meta('duration', elem_content, default=None)]
246 title = self._html_search_regex(
247 r'album_title\s*:\s*"((?:\\.|[^"\\])+?)"',
248 webpage, 'title', fatal=False)
250 title = title.replace(r'\"', '"')
253 'uploader_id': uploader_id,
260 class BandcampWeeklyIE(InfoExtractor):
261 IE_NAME = 'Bandcamp:bandcamp_weekly'
262 _VALID_URL = r'https?://(?:www\.)?bandcamp\.com/?\?(?:.*&)?show=(?P<id>\d+)(?:$|[&#])'
264 'url': 'https://bandcamp.com/?show=224',
265 'md5': 'b00df799c733cf7e0c567ed187dea0fd',
269 'title': 'BC Weekly April 4th 2017: Magic Moments',
270 'description': 'Stones Throw\'s Vex Ruffin, plus up and coming singer Salami Rose Joe Louis, in conversation about their fantastic DIY albums.',
273 'url': 'https://bandcamp.com/?blah/blah@&show=228',
274 'only_matching': True
277 def _real_extract(self, url):
278 video_id = self._match_id(url)
279 webpage = self._download_webpage(url, video_id)
281 blob = self._parse_json(
283 r'data-blob=(["\'])(?P<blob>{.+?})\1', webpage,
284 'blob', group='blob'),
285 video_id, transform_source=unescapeHTML)
287 show = blob['bcw_show']
289 # This is desired because any invalid show id redirects to `bandcamp.com`
290 # which happens to expose the latest Bandcamp Weekly episode.
291 video_id = compat_str(show['show_id'])
293 def to_format_dictionaries(audio_stream):
294 dictionaries = [{'format_id': kvp[0], 'url': kvp[1]} for kvp in audio_stream.items()]
295 known_extensions = ['mp3', 'opus']
297 for dictionary in dictionaries:
298 for ext in known_extensions:
299 if ext in dictionary['format_id']:
300 dictionary['ext'] = ext
305 formats = to_format_dictionaries(show['audio_stream'])
306 self._sort_formats(formats)
310 'title': show['audio_title'] + ': ' + show['subtitle'],
311 'description': show.get('desc'),
312 'duration': float_or_none(show.get('audio_duration')),
313 'webpage_url': 'https://bandcamp.com/?show=' + video_id,
315 'release_date': unified_strdate(show.get('published_date')),
316 'series': 'Bandcamp Weekly',
317 'episode_id': compat_str(video_id),