Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re, json, os, urllib
- url = raw_input("Bandcamp album url:")
- data = urllib.urlopen(url).read()
- toFind = {
- "Artist": {
- "Found": False,
- "Collected": None,
- "Pattern": 'BandData.+?{.+?name: "(.+?)".+?};',
- "Namespace": "artistName"
- },
- "Art": {
- "Found": False,
- "Collected": None,
- "Pattern": 'TralbumData.+?{.+?artFullsizeUrl: "(.+?)".+?};',
- "Namespace": "artData"
- },
- "Album": {
- "Found": False,
- "Collected": None,
- "Pattern": 'EmbedData.+?{.+?album_title: "(.+?)".+?};',
- "Namespace": "albumName"
- },
- "Songs": {
- "Found": False,
- "Collected": None,
- "Pattern": 'trackinfo.+?(\[.+?\])',
- "Namespace": "songsData"
- }
- }
- for subject in toFind:
- print "Looking for "+subject+"...",
- try:
- pattern = toFind[subject]["Pattern"]
- regex_data = re.findall(pattern, data, re.DOTALL)
- if len(regex_data) == 1:
- toFind[subject]["Collected"] = regex_data[0]
- else:
- toFind[subject]["Collected"] = list(reversed(sorted(regex_data, key=lambda i:len(i))))[0]
- toFind[subject]["Found"] = True
- print "\t[FOUND]"
- except Exception, e:
- print "\t[ERROR] {0}".format(e)
- print "Found all necessary data...\n"
- toFind["Songs"]["Collected"] = [(str(i[u"title"]), str(i[u"file"][u'mp3-128']))for i in json.loads(toFind["Songs"]["Collected"])]
- for i in toFind:exec("{ns}=toFind['{i}']['Collected']".format(ns=toFind[i]["Namespace"], i=i))
- directory = "{0} - {1}/".format(artistName, albumName)
- if not os.path.exists(directory): os.mkdir(directory)
- urllib.urlretrieve(artData, directory+"Album"+os.path.splitext(artData)[1])
- Successful = 0
- for song in songsData:
- try:
- fileName = "{0} - {1}.mp3".format(artistName, song[0])
- print "Downloading: {0} ({1}/{2})".format(fileName, Successful+1, len(songsData))
- urllib.urlretrieve(song[1], directory+fileName)
- Successful +=1
- except Exception, e:
- print "Unable to download {0}, {1}".format(song[0], e)
- print "Successfully downloaded {0}/{1}".format(Successful, len(songsData))
- os.system("pause")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement