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