]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bandcamp.py
Clarify use of mplayer.
[youtubedl] / youtube_dl / extractor / 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
13 def _real_extract(self, url):
14 mobj = re.match(self._VALID_URL, url)
15 title = mobj.group('title')
16 webpage = self._download_webpage(url, title)
17 # We get the link to the free download page
18 m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
19 if m_download is None:
20 raise ExtractorError(u'No free songs found')
21
22 download_link = m_download.group(1)
23 id = re.search(r'var TralbumData = {(.*?)id: (?P<id>\d*?)$',
24 webpage, re.MULTILINE|re.DOTALL).group('id')
25
26 download_webpage = self._download_webpage(download_link, id,
27 'Downloading free downloads page')
28 # We get the dictionary of the track from some javascrip code
29 info = re.search(r'items: (.*?),$',
30 download_webpage, re.MULTILINE).group(1)
31 info = json.loads(info)[0]
32 # We pick mp3-320 for now, until format selection can be easily implemented.
33 mp3_info = info[u'downloads'][u'mp3-320']
34 # If we try to use this url it says the link has expired
35 initial_url = mp3_info[u'url']
36 re_url = r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$'
37 m_url = re.match(re_url, initial_url)
38 #We build the url we will use to get the final track url
39 # This url is build in Bandcamp in the script download_bunde_*.js
40 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'))
41 final_url_webpage = self._download_webpage(request_url, id, 'Requesting download url')
42 # If we could correctly generate the .rand field the url would be
43 #in the "download_url" key
44 final_url = re.search(r'"retry_url":"(.*?)"', final_url_webpage).group(1)
45
46 track_info = {'id':id,
47 'title' : info[u'title'],
48 'ext' : 'mp3',
49 'url' : final_url,
50 'thumbnail' : info[u'thumb_url'],
51 'uploader' : info[u'artist']
52 }
53
54 return [track_info]