You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.4 KiB

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