bandcamp.py 3.6 KB

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