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.

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