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.

129 lines
5.0 KiB

  1. import json
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_str,
  6. compat_urlparse,
  7. ExtractorError,
  8. )
  9. class BandcampIE(InfoExtractor):
  10. IE_NAME = u'Bandcamp'
  11. _VALID_URL = r'http://.*?\.bandcamp\.com/track/(?P<title>.*)'
  12. _TESTS = [{
  13. u'url': u'http://youtube-dl.bandcamp.com/track/youtube-dl-test-song',
  14. u'file': u'1812978515.mp3',
  15. u'md5': u'cdeb30cdae1921719a3cbcab696ef53c',
  16. u'info_dict': {
  17. u"title": u"youtube-dl test song \"'/\\\u00e4\u21ad"
  18. },
  19. u'skip': u'There is a limit of 200 free downloads / month for the test song'
  20. }, {
  21. u'url': u'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
  22. u'playlist': [
  23. {
  24. u'file': u'1353101989.mp3',
  25. u'md5': u'39bc1eded3476e927c724321ddf116cf',
  26. u'info_dict': {
  27. u'title': u'Intro',
  28. }
  29. },
  30. {
  31. u'file': u'38097443.mp3',
  32. u'md5': u'1a2c32e2691474643e912cc6cd4bffaa',
  33. u'info_dict': {
  34. u'title': u'Kero One - Keep It Alive (Blazo remix)',
  35. }
  36. },
  37. ],
  38. u'params': {
  39. u'playlistend': 2
  40. }
  41. }]
  42. def _real_extract(self, url):
  43. mobj = re.match(self._VALID_URL, url)
  44. title = mobj.group('title')
  45. webpage = self._download_webpage(url, title)
  46. # We get the link to the free download page
  47. m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
  48. if m_download is None:
  49. m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
  50. if m_trackinfo:
  51. json_code = m_trackinfo.group(1)
  52. data = json.loads(json_code)
  53. entries = []
  54. for d in data:
  55. formats = [{
  56. 'format_id': 'format_id',
  57. 'url': format_url,
  58. 'ext': format_id.partition('-')[0]
  59. } for format_id, format_url in sorted(d['file'].items())]
  60. entries.append({
  61. 'id': compat_str(d['id']),
  62. 'title': d['title'],
  63. 'formats': formats,
  64. })
  65. return self.playlist_result(entries, title, title)
  66. else:
  67. raise ExtractorError(u'No free songs found')
  68. download_link = m_download.group(1)
  69. id = re.search(r'var TralbumData = {(.*?)id: (?P<id>\d*?)$',
  70. webpage, re.MULTILINE|re.DOTALL).group('id')
  71. download_webpage = self._download_webpage(download_link, id,
  72. 'Downloading free downloads page')
  73. # We get the dictionary of the track from some javascrip code
  74. info = re.search(r'items: (.*?),$',
  75. download_webpage, re.MULTILINE).group(1)
  76. info = json.loads(info)[0]
  77. # We pick mp3-320 for now, until format selection can be easily implemented.
  78. mp3_info = info[u'downloads'][u'mp3-320']
  79. # If we try to use this url it says the link has expired
  80. initial_url = mp3_info[u'url']
  81. re_url = r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$'
  82. m_url = re.match(re_url, initial_url)
  83. #We build the url we will use to get the final track url
  84. # This url is build in Bandcamp in the script download_bunde_*.js
  85. 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'))
  86. final_url_webpage = self._download_webpage(request_url, id, 'Requesting download url')
  87. # If we could correctly generate the .rand field the url would be
  88. #in the "download_url" key
  89. final_url = re.search(r'"retry_url":"(.*?)"', final_url_webpage).group(1)
  90. track_info = {'id':id,
  91. 'title' : info[u'title'],
  92. 'ext' : 'mp3',
  93. 'url' : final_url,
  94. 'thumbnail' : info[u'thumb_url'],
  95. 'uploader' : info[u'artist']
  96. }
  97. return [track_info]
  98. class BandcampAlbumIE(InfoExtractor):
  99. IE_NAME = u'Bandcamp:album'
  100. _VALID_URL = r'http://.*?\.bandcamp\.com/album/(?P<title>.*)'
  101. def _real_extract(self, url):
  102. mobj = re.match(self._VALID_URL, url)
  103. title = mobj.group('title')
  104. webpage = self._download_webpage(url, title)
  105. tracks_paths = re.findall(r'<a href="(.*?)" itemprop="url">', webpage)
  106. if not tracks_paths:
  107. raise ExtractorError(u'The page doesn\'t contain any track')
  108. entries = [
  109. self.url_result(compat_urlparse.urljoin(url, t_path), ie=BandcampIE.ie_key())
  110. for t_path in tracks_paths]
  111. title = self._search_regex(r'album_title : "(.*?)"', webpage, u'title')
  112. return {
  113. '_type': 'playlist',
  114. 'title': title,
  115. 'entries': entries,
  116. }