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.

130 lines
5.1 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. u'skip': u'Bancamp imposes download limits. See test_playlists:test_bandcamp_album for the playlist test'
  42. }]
  43. def _real_extract(self, url):
  44. mobj = re.match(self._VALID_URL, url)
  45. title = mobj.group('title')
  46. webpage = self._download_webpage(url, title)
  47. # We get the link to the free download page
  48. m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
  49. if m_download is None:
  50. m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
  51. if m_trackinfo:
  52. json_code = m_trackinfo.group(1)
  53. data = json.loads(json_code)
  54. entries = []
  55. for d in data:
  56. formats = [{
  57. 'format_id': 'format_id',
  58. 'url': format_url,
  59. 'ext': format_id.partition('-')[0]
  60. } for format_id, format_url in sorted(d['file'].items())]
  61. entries.append({
  62. 'id': compat_str(d['id']),
  63. 'title': d['title'],
  64. 'formats': formats,
  65. })
  66. return self.playlist_result(entries, title, title)
  67. else:
  68. raise ExtractorError(u'No free songs found')
  69. download_link = m_download.group(1)
  70. id = re.search(r'var TralbumData = {(.*?)id: (?P<id>\d*?)$',
  71. webpage, re.MULTILINE|re.DOTALL).group('id')
  72. download_webpage = self._download_webpage(download_link, id,
  73. 'Downloading free downloads page')
  74. # We get the dictionary of the track from some javascrip code
  75. info = re.search(r'items: (.*?),$',
  76. download_webpage, re.MULTILINE).group(1)
  77. info = json.loads(info)[0]
  78. # We pick mp3-320 for now, until format selection can be easily implemented.
  79. mp3_info = info[u'downloads'][u'mp3-320']
  80. # If we try to use this url it says the link has expired
  81. initial_url = mp3_info[u'url']
  82. re_url = r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$'
  83. m_url = re.match(re_url, initial_url)
  84. #We build the url we will use to get the final track url
  85. # This url is build in Bandcamp in the script download_bunde_*.js
  86. 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'))
  87. final_url_webpage = self._download_webpage(request_url, id, 'Requesting download url')
  88. # If we could correctly generate the .rand field the url would be
  89. #in the "download_url" key
  90. final_url = re.search(r'"retry_url":"(.*?)"', final_url_webpage).group(1)
  91. track_info = {'id':id,
  92. 'title' : info[u'title'],
  93. 'ext' : 'mp3',
  94. 'url' : final_url,
  95. 'thumbnail' : info[u'thumb_url'],
  96. 'uploader' : info[u'artist']
  97. }
  98. return [track_info]
  99. class BandcampAlbumIE(InfoExtractor):
  100. IE_NAME = u'Bandcamp:album'
  101. _VALID_URL = r'http://.*?\.bandcamp\.com/album/(?P<title>.*)'
  102. def _real_extract(self, url):
  103. mobj = re.match(self._VALID_URL, url)
  104. title = mobj.group('title')
  105. webpage = self._download_webpage(url, title)
  106. tracks_paths = re.findall(r'<a href="(.*?)" itemprop="url">', webpage)
  107. if not tracks_paths:
  108. raise ExtractorError(u'The page doesn\'t contain any track')
  109. entries = [
  110. self.url_result(compat_urlparse.urljoin(url, t_path), ie=BandcampIE.ie_key())
  111. for t_path in tracks_paths]
  112. title = self._search_regex(r'album_title : "(.*?)"', webpage, u'title')
  113. return {
  114. '_type': 'playlist',
  115. 'title': title,
  116. 'entries': entries,
  117. }