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. def _real_extract(self, url):
  22. mobj = re.match(self._VALID_URL, url)
  23. title = mobj.group('title')
  24. webpage = self._download_webpage(url, title)
  25. # We get the link to the free download page
  26. m_download = re.search(r'freeDownloadPage: "(.*?)"', webpage)
  27. if m_download is None:
  28. m_trackinfo = re.search(r'trackinfo: (.+),\s*?\n', webpage)
  29. if m_trackinfo:
  30. json_code = m_trackinfo.group(1)
  31. data = json.loads(json_code)
  32. entries = []
  33. for d in data:
  34. formats = [{
  35. 'format_id': 'format_id',
  36. 'url': format_url,
  37. 'ext': format_id.partition('-')[0]
  38. } for format_id, format_url in sorted(d['file'].items())]
  39. return {
  40. 'id': compat_str(d['id']),
  41. 'title': d['title'],
  42. 'formats': formats,
  43. }
  44. else:
  45. raise ExtractorError(u'No free songs found')
  46. download_link = m_download.group(1)
  47. id = re.search(r'var TralbumData = {(.*?)id: (?P<id>\d*?)$',
  48. webpage, re.MULTILINE|re.DOTALL).group('id')
  49. download_webpage = self._download_webpage(download_link, id,
  50. 'Downloading free downloads page')
  51. # We get the dictionary of the track from some javascrip code
  52. info = re.search(r'items: (.*?),$',
  53. download_webpage, re.MULTILINE).group(1)
  54. info = json.loads(info)[0]
  55. # We pick mp3-320 for now, until format selection can be easily implemented.
  56. mp3_info = info[u'downloads'][u'mp3-320']
  57. # If we try to use this url it says the link has expired
  58. initial_url = mp3_info[u'url']
  59. re_url = r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$'
  60. m_url = re.match(re_url, initial_url)
  61. #We build the url we will use to get the final track url
  62. # This url is build in Bandcamp in the script download_bunde_*.js
  63. 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'))
  64. final_url_webpage = self._download_webpage(request_url, id, 'Requesting download url')
  65. # If we could correctly generate the .rand field the url would be
  66. #in the "download_url" key
  67. final_url = re.search(r'"retry_url":"(.*?)"', final_url_webpage).group(1)
  68. track_info = {'id':id,
  69. 'title' : info[u'title'],
  70. 'ext' : 'mp3',
  71. 'url' : final_url,
  72. 'thumbnail' : info[u'thumb_url'],
  73. 'uploader' : info[u'artist']
  74. }
  75. return [track_info]
  76. class BandcampAlbumIE(InfoExtractor):
  77. IE_NAME = u'Bandcamp:album'
  78. _VALID_URL = r'http://.*?\.bandcamp\.com/album/(?P<title>.*)'
  79. _TEST = {
  80. u'url': u'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
  81. u'playlist': [
  82. {
  83. u'file': u'1353101989.mp3',
  84. u'md5': u'39bc1eded3476e927c724321ddf116cf',
  85. u'info_dict': {
  86. u'title': u'Intro',
  87. }
  88. },
  89. {
  90. u'file': u'38097443.mp3',
  91. u'md5': u'1a2c32e2691474643e912cc6cd4bffaa',
  92. u'info_dict': {
  93. u'title': u'Kero One - Keep It Alive (Blazo remix)',
  94. }
  95. },
  96. ],
  97. u'params': {
  98. u'playlistend': 2
  99. },
  100. u'skip': u'Bancamp imposes download limits. See test_playlists:test_bandcamp_album for the playlist test'
  101. }
  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. }