[bandcamp] add support for albums (reported in #1270)
[youtube-dl] / youtube_dl / extractor / bandcamp.py
1 import json
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_urlparse,
7     ExtractorError,
8 )
9
10
11 class BandcampIE(InfoExtractor):
12     IE_NAME = u'Bandcamp'
13     _VALID_URL = r'http://.*?\.bandcamp\.com/track/(?P<title>.*)'
14     _TEST = {
15         u'url': u'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song',
16         u'file': u'1812978515.mp3',
17         u'md5': u'cdeb30cdae1921719a3cbcab696ef53c',
18         u'info_dict': {
19             u"title": u"youtube-dl test song \"'/\\\u00e4\u21ad"
20         },
21         u'skip': u'There is a limit of 200 free downloads / month for the test song'
22     }
23
24     def _real_extract(self, url):
25         mobj = re.match(self._VALID_URL, url)
26         title = mobj.group('title')
27         webpage = self._download_webpage(url, title)
28         # We get the link to the free download page
29         m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
30         if m_download is None:
31             raise ExtractorError(u'No free songs found')
32
33         download_link = m_download.group(1)
34         id = re.search(r'var TralbumData = {(.*?)id: (?P<id>\d*?)$', 
35                        webpage, re.MULTILINE|re.DOTALL).group('id')
36
37         download_webpage = self._download_webpage(download_link, id,
38                                                   'Downloading free downloads page')
39         # We get the dictionary of the track from some javascrip code
40         info = re.search(r'items: (.*?),$',
41                          download_webpage, re.MULTILINE).group(1)
42         info = json.loads(info)[0]
43         # We pick mp3-320 for now, until format selection can be easily implemented.
44         mp3_info = info[u'downloads'][u'mp3-320']
45         # If we try to use this url it says the link has expired
46         initial_url = mp3_info[u'url']
47         re_url = r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$'
48         m_url = re.match(re_url, initial_url)
49         #We build the url we will use to get the final track url
50         # This url is build in Bandcamp in the script download_bunde_*.js
51         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'))
52         final_url_webpage = self._download_webpage(request_url, id, 'Requesting download url')
53         # If we could correctly generate the .rand field the url would be
54         #in the "download_url" key
55         final_url = re.search(r'"retry_url":"(.*?)"', final_url_webpage).group(1)
56
57         track_info = {'id':id,
58                       'title' : info[u'title'],
59                       'ext' :   'mp3',
60                       'url' :   final_url,
61                       'thumbnail' : info[u'thumb_url'],
62                       'uploader' :  info[u'artist']
63                       }
64
65         return [track_info]
66
67
68 class BandcampAlbumIE(InfoExtractor):
69     IE_NAME = u'Bandcamp:album'
70     _VALID_URL = r'http://.*?\.bandcamp\.com/album/(?P<title>.*)'
71
72     def _real_extract(self, url):
73         mobj = re.match(self._VALID_URL, url)
74         title = mobj.group('title')
75         webpage = self._download_webpage(url, title)
76         tracks_paths = re.findall(r'<a href="(.*?)" itemprop="url">', webpage)
77         if not tracks_paths:
78             raise ExtractorError(u'The page doesn\'t contain any track')
79         entries = [
80             self.url_result(compat_urlparse.urljoin(url, t_path), ie=BandcampIE.ie_key())
81             for t_path in tracks_paths]
82         title = self._search_regex(r'album_title : "(.*?)"', webpage, u'title')
83         return {
84             '_type': 'playlist',
85             'title': title,
86             'entries': entries,
87         }