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.

181 lines
6.6 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'(?ms)var TralbumData = .*?[{,]\s*id: (?P<id>\d+),?$',
  67. webpage, 'video id')
  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. all_info = self._parse_json(self._search_regex(
  71. r'(?sm)items: (.*?),$', download_webpage, 'items'), video_id)
  72. info = all_info[0]
  73. # We pick mp3-320 for now, until format selection can be easily implemented.
  74. mp3_info = info['downloads']['mp3-320']
  75. # If we try to use this url it says the link has expired
  76. initial_url = mp3_info['url']
  77. m_url = re.match(
  78. r'(?P<server>http://(.*?)\.bandcamp\.com)/download/track\?enc=mp3-320&fsig=(?P<fsig>.*?)&id=(?P<id>.*?)&ts=(?P<ts>.*)$',
  79. initial_url)
  80. # We build the url we will use to get the final track url
  81. # This url is build in Bandcamp in the script download_bunde_*.js
  82. 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'))
  83. final_url_webpage = self._download_webpage(request_url, video_id, 'Requesting download url')
  84. # If we could correctly generate the .rand field the url would be
  85. # in the "download_url" key
  86. final_url = self._search_regex(
  87. r'"retry_url":"(.*?)"', final_url_webpage, 'final video URL')
  88. return {
  89. 'id': video_id,
  90. 'title': info['title'],
  91. 'ext': 'mp3',
  92. 'vcodec': 'none',
  93. 'url': final_url,
  94. 'thumbnail': info.get('thumb_url'),
  95. 'uploader': info.get('artist'),
  96. }
  97. class BandcampAlbumIE(InfoExtractor):
  98. IE_NAME = 'Bandcamp:album'
  99. _VALID_URL = r'https?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com(?:/album/(?P<album_id>[^?#]+)|/?(?:$|[?#]))'
  100. _TESTS = [{
  101. 'url': 'http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1',
  102. 'playlist': [
  103. {
  104. 'md5': '39bc1eded3476e927c724321ddf116cf',
  105. 'info_dict': {
  106. 'id': '1353101989',
  107. 'ext': 'mp3',
  108. 'title': 'Intro',
  109. }
  110. },
  111. {
  112. 'md5': '1a2c32e2691474643e912cc6cd4bffaa',
  113. 'info_dict': {
  114. 'id': '38097443',
  115. 'ext': 'mp3',
  116. 'title': 'Kero One - Keep It Alive (Blazo remix)',
  117. }
  118. },
  119. ],
  120. 'info_dict': {
  121. 'title': 'Jazz Format Mixtape vol.1',
  122. 'id': 'jazz-format-mixtape-vol-1',
  123. 'uploader_id': 'blazo',
  124. },
  125. 'params': {
  126. 'playlistend': 2
  127. },
  128. 'skip': 'Bandcamp imposes download limits.'
  129. }, {
  130. 'url': 'http://nightbringer.bandcamp.com/album/hierophany-of-the-open-grave',
  131. 'info_dict': {
  132. 'title': 'Hierophany of the Open Grave',
  133. 'uploader_id': 'nightbringer',
  134. 'id': 'hierophany-of-the-open-grave',
  135. },
  136. 'playlist_mincount': 9,
  137. }, {
  138. 'url': 'http://dotscale.bandcamp.com',
  139. 'info_dict': {
  140. 'title': 'Loom',
  141. 'id': 'dotscale',
  142. 'uploader_id': 'dotscale',
  143. },
  144. 'playlist_mincount': 7,
  145. }]
  146. def _real_extract(self, url):
  147. mobj = re.match(self._VALID_URL, url)
  148. uploader_id = mobj.group('subdomain')
  149. album_id = mobj.group('album_id')
  150. playlist_id = album_id or uploader_id
  151. webpage = self._download_webpage(url, playlist_id)
  152. tracks_paths = re.findall(r'<a href="(.*?)" itemprop="url">', webpage)
  153. if not tracks_paths:
  154. raise ExtractorError('The page doesn\'t contain any tracks')
  155. entries = [
  156. self.url_result(compat_urlparse.urljoin(url, t_path), ie=BandcampIE.ie_key())
  157. for t_path in tracks_paths]
  158. title = self._search_regex(
  159. r'album_title\s*:\s*"(.*?)"', webpage, 'title', fatal=False)
  160. return {
  161. '_type': 'playlist',
  162. 'uploader_id': uploader_id,
  163. 'id': playlist_id,
  164. 'title': title,
  165. 'entries': entries,
  166. }