]> git.bitcoin.ninja Git - youtube-dl/blob - bandcamp.py
129a20f4497b4cc6fc9f031e8e48dd8eb8980f66
[youtube-dl] / bandcamp.py
1 import json
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7 )
8
9
10 class BandcampIE(InfoExtractor):
11     _VALID_URL = r'http://.*?\.bandcamp\.com/track/(?P<title>.*)'
12     _TEST = {
13         u'url': u'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song',
14         u'file': u'1812978515.mp3',
15         u'md5': u'cdeb30cdae1921719a3cbcab696ef53c',
16         u'info_dict': {
17             u"title": u"youtube-dl test song \"'/\\\u00e4\u21ad"
18         },
19         u'skip': u'There is a limit of 200 free downloads / month for the test song'
20     }
21
22     def _real_extract(self, url):
23         mobj = re.match(self._VALID_URL, url)
24         title = mobj.group('title')
25         webpage = self._download_webpage(url, title)
26         # We get the link to the free download page
27         m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
28         if m_download is None:
29             raise ExtractorError(u'No free songs found')
30
31         download_link = m_download.group(1)
32         id = re.search(r'var TralbumData = {(.*?)id: (?P<id>\d*?)$', 
33                        webpage, re.MULTILINE|re.DOTALL).group('id')
34
35         download_webpage = self._download_webpage(download_link, id,
36                                                   'Downloading free downloads page')
37         # We get the dictionary of the track from some javascrip code
38         info = re.search(r'items: (.*?),$',
39                          download_webpage, re.MULTILINE).group(1)
40         info = json.loads(info)[0]
41         # We pick mp3-320 for now, until format selection can be easily implemented.
42         mp3_info = info[u'downloads'][u'mp3-320']
43         # If we try to use this url it says the link has expired
44         initial_url = mp3_info[u'url']
45         re_url = r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$'
46         m_url = re.match(re_url, initial_url)
47         #We build the url we will use to get the final track url
48         # This url is build in Bandcamp in the script download_bunde_*.js
49         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'))
50         final_url_webpage = self._download_webpage(request_url, id, 'Requesting download url')
51         # If we could correctly generate the .rand field the url would be
52         #in the "download_url" key
53         final_url = re.search(r'"retry_url":"(.*?)"', final_url_webpage).group(1)
54
55         track_info = {'id':id,
56                       'title' : info[u'title'],
57                       'ext' :   'mp3',
58                       'url' :   final_url,
59                       'thumbnail' : info[u'thumb_url'],
60                       'uploader' :  info[u'artist']
61                       }
62
63         return [track_info]