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.

163 lines
6.2 KiB

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