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