4 from .common import InfoExtractor
12 class BandcampIE(InfoExtractor):
14 _VALID_URL = r'http://.*?\.bandcamp\.com/track/(?P<title>.*)'
16 u'url': u'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song',
17 u'file': u'1812978515.mp3',
18 u'md5': u'cdeb30cdae1921719a3cbcab696ef53c',
20 u"title": u"youtube-dl test song \"'/\\\u00e4\u21ad"
22 u'skip': u'There is a limit of 200 free downloads / month for the test song'
24 u'url': u'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
27 u'file': u'1353101989.mp3',
28 u'md5': u'39bc1eded3476e927c724321ddf116cf',
34 u'file': u'38097443.mp3',
35 u'md5': u'1a2c32e2691474643e912cc6cd4bffaa',
37 u'title': u'Kero One - Keep It Alive (Blazo remix)',
46 def _real_extract(self, url):
47 mobj = re.match(self._VALID_URL, url)
48 title = mobj.group('title')
49 webpage = self._download_webpage(url, title)
50 # We get the link to the free download page
51 m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
52 if m_download is None:
53 m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
55 json_code = m_trackinfo.group(1)
56 data = json.loads(json_code)
61 'format_id': 'format_id',
63 'ext': format_id.partition('-')[0]
64 } for format_id, format_url in sorted(d['file'].items())]
66 'id': compat_str(d['id']),
71 return self.playlist_result(entries, title, title)
73 raise ExtractorError(u'No free songs found')
75 download_link = m_download.group(1)
76 id = re.search(r'var TralbumData = {(.*?)id: (?P<id>\d*?)$',
77 webpage, re.MULTILINE|re.DOTALL).group('id')
79 download_webpage = self._download_webpage(download_link, id,
80 'Downloading free downloads page')
81 # We get the dictionary of the track from some javascrip code
82 info = re.search(r'items: (.*?),$',
83 download_webpage, re.MULTILINE).group(1)
84 info = json.loads(info)[0]
85 # We pick mp3-320 for now, until format selection can be easily implemented.
86 mp3_info = info[u'downloads'][u'mp3-320']
87 # If we try to use this url it says the link has expired
88 initial_url = mp3_info[u'url']
89 re_url = r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$'
90 m_url = re.match(re_url, initial_url)
91 #We build the url we will use to get the final track url
92 # This url is build in Bandcamp in the script download_bunde_*.js
93 request_url = '%s/statdownload/track?enc=mp3-320&fsig=%s&id=%s&ts=%s&.rand=665028774616&.vrs=1' % (m_url.group('server'), m_url.group('fsig'), id, m_url.group('ts'))
94 final_url_webpage = self._download_webpage(request_url, id, 'Requesting download url')
95 # If we could correctly generate the .rand field the url would be
96 #in the "download_url" key
97 final_url = re.search(r'"retry_url":"(.*?)"', final_url_webpage).group(1)
99 track_info = {'id':id,
100 'title' : info[u'title'],
103 'thumbnail' : info[u'thumb_url'],
104 'uploader' : info[u'artist']
110 class BandcampAlbumIE(InfoExtractor):
111 IE_NAME = u'Bandcamp:album'
112 _VALID_URL = r'http://.*?\.bandcamp\.com/album/(?P<title>.*)'
114 def _real_extract(self, url):
115 mobj = re.match(self._VALID_URL, url)
116 title = mobj.group('title')
117 webpage = self._download_webpage(url, title)
118 tracks_paths = re.findall(r'<a href="(.*?)" itemprop="url">', webpage)
120 raise ExtractorError(u'The page doesn\'t contain any track')
122 self.url_result(compat_urlparse.urljoin(url, t_path), ie=BandcampIE.ie_key())
123 for t_path in tracks_paths]
124 title = self._search_regex(r'album_title : "(.*?)"', webpage, u'title')