Advertisement
Peaser

Bandcamp Ripper

Apr 27th, 2015
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. import re, json, os, urllib
  2. url = raw_input("Bandcamp album url:")
  3. data = urllib.urlopen(url).read()
  4.  
  5. toFind = {
  6.     "Artist": {
  7.         "Found": False,
  8.         "Collected": None,
  9.         "Pattern": 'BandData.+?{.+?name: "(.+?)".+?};',
  10.         "Namespace": "artistName"
  11.     },
  12.     "Art": {
  13.         "Found": False,
  14.         "Collected": None,
  15.         "Pattern": 'TralbumData.+?{.+?artFullsizeUrl: "(.+?)".+?};',
  16.         "Namespace": "artData"
  17.     },
  18.     "Album": {
  19.         "Found": False,
  20.         "Collected": None,
  21.         "Pattern": 'EmbedData.+?{.+?album_title: "(.+?)".+?};',
  22.         "Namespace": "albumName"
  23.     },
  24.     "Songs": {
  25.         "Found": False,
  26.         "Collected": None,
  27.         "Pattern": 'trackinfo.+?(\[.+?\])',
  28.         "Namespace": "songsData"
  29.     }
  30. }
  31.  
  32.  
  33. for subject in toFind:
  34.     print "Looking for "+subject+"...",
  35.     try:
  36.         pattern = toFind[subject]["Pattern"]
  37.         regex_data = re.findall(pattern, data, re.DOTALL)
  38.         if len(regex_data) == 1:
  39.             toFind[subject]["Collected"] = regex_data[0]
  40.         else:
  41.             toFind[subject]["Collected"] = list(reversed(sorted(regex_data, key=lambda i:len(i))))[0]
  42.         toFind[subject]["Found"] = True
  43.         print "\t[FOUND]"
  44.     except Exception, e:
  45.         print "\t[ERROR] {0}".format(e)
  46.  
  47. print "Found all necessary data...\n"
  48.  
  49. toFind["Songs"]["Collected"] = [(str(i[u"title"]), str(i[u"file"][u'mp3-128']))for i in json.loads(toFind["Songs"]["Collected"])]
  50.  
  51. for i in toFind:exec("{ns}=toFind['{i}']['Collected']".format(ns=toFind[i]["Namespace"], i=i))
  52.  
  53. directory = "{0} - {1}/".format(artistName, albumName)
  54. if not os.path.exists(directory): os.mkdir(directory)
  55.  
  56. urllib.urlretrieve(artData, directory+"Album"+os.path.splitext(artData)[1])
  57.  
  58. Successful = 0
  59. for song in songsData:
  60.     try:
  61.         fileName = "{0} - {1}.mp3".format(artistName, song[0])
  62.         print "Downloading: {0} ({1}/{2})".format(fileName, Successful+1, len(songsData))
  63.         urllib.urlretrieve(song[1], directory+fileName)
  64.         Successful +=1
  65.     except Exception, e:
  66.         print "Unable to download {0}, {1}".format(song[0], e)
  67. print "Successfully downloaded {0}/{1}".format(Successful, len(songsData))
  68.  
  69. os.system("pause")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement