# -*- coding: utf-8 -*- import zipfile, os #from mutagen.mp3 import MP3 from mutagen.id3 import ID3, TRCK, TPE2, TALB, USLT AlbumArtist = "Caitlyn" AlbumTitle = "" zipfilelist = [x for x in os.listdir('.') if x.endswith('.zip')] #thezipfile = zipfile.ZipFile("blog20100818.zip", "r") #thezipfile.printdir() for zipfilename in zipfilelist: thezipfile = zipfile.ZipFile(zipfilename, "r") for info in thezipfile.infolist(): #find the textfile fname = info.filename if fname.endswith(".txt"): textfile = thezipfile.open(fname) AlbumTitle = textfile.readline() AlbumTitle = AlbumTitle.strip() AlbumTitle = AlbumTitle.capitalize() AlbumTitle = "MP3 " + AlbumTitle print """Album Title: "%s" """ % AlbumTitle #construct clusters for insertion into lyrics: lyricclusters = [""] i = 0 while 1 == 1: line = textfile.readline().strip() if line.find("THIS WEEK'S TRACKS") >= 0: break if len(line) > 0: lyricclusters[i] = lyricclusters[i] + line + "\n" else: i += 1 lyricclusters.append("") textfile.close() #print lyricclusters[9] #construct a list of the tracks in playlist order: playlist = [] for info in thezipfile.infolist(): #find the textfile fname = info.filename if fname.endswith(".m3u"): # extract then update tags playlistfile = thezipfile.open(fname) for line in playlistfile: if line[0] != "#": playlist.append(line.strip()) #print playlist[4] for info in thezipfile.infolist(): #extract the MP3's fname = info.filename if fname.endswith(".mp3"): # extract then update tags #mp3data = thezipfile.read(fname) #fout = open(fname, "w") #fout.write(mp3data) #out.close() thezipfile.extract(fname) print "New file created --> %s" % fname #find the track number tracknumber = "" i=1 for song in playlist: if fname.find(song) >= 0: tracknumber = str(i) i += 1 #find the associated liner notes fnameshortened = fname.replace(".mp3", "") notes = "" i=1 for paragraph in lyricclusters: if paragraph.find(fnameshortened) >= 0: notes = paragraph i += 1 #assign the AlbumArtist, the AlbumTitle, the tracknumber, and the notes to the new MP3 file: id3tags = ID3(fname) id3tags.add(TRCK(encoding=0,text=unicode(tracknumber))) id3tags.add(TPE2(encoding=0,text=unicode(AlbumArtist))) id3tags.add(TALB(encoding=0,text=unicode(AlbumTitle))) id3lyrics = USLT(encoding=1, lang=u'eng', desc=u'desc', text=unicode(notes)) id3tags[u"USLT::'eng'"] = id3lyrics id3tags.save()