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.

142 lines
5.5 KiB

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