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.

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