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.

148 lines
5.7 KiB

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