Created
September 8, 2012 23:41
-
-
Save anonymous/3681165 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import print_function | |
| import sys | |
| import os | |
| import shutil | |
| from mutagen.easyid3 import EasyID3 | |
| from mutagen.id3 import ID3 | |
| from pprint import pprint | |
| def ensureExists(dir): | |
| try: | |
| os.stat(dir) | |
| except OSError: | |
| print("%s does not exist. Maybe create it?" % (dir)) | |
| sys.exit() | |
| def ensureEmpty(dir): | |
| contents = os.listdir(dir) | |
| if len(contents) > 0: | |
| print("Okay dude, %s isn't empty. I don't think this is a good idea" % (write_to_dir)) | |
| sys.exit() | |
| def ensureNotEqual(a, b): | |
| if os.path.realpath(a) == os.path.realpath(b): | |
| print("You gave me the same directory twice, please don't do that.") | |
| sys.exit() | |
| class Mp3Processor(object): | |
| def __init__(self, destination): | |
| self.pathnames = [] | |
| self.tags = {} | |
| self.destination = destination | |
| def processDir(self, dir): | |
| fullpaths = map(lambda file: os.path.join(dir[0], file), dir[2]) | |
| self.pathnames.extend(map(unicode, filter(lambda path: path.endswith('.mp3'), fullpaths))) | |
| def showTags(self): | |
| for pathname in self.pathnames: | |
| self.tags[pathname] = EasyID3(pathname) | |
| print("%-130s\t%s/%s" % (pathname.replace('/Users/davidbernal/Downloads/music', ''), self.tags[pathname]['TPE2'], self.tags[pathname].get('TALB'))) | |
| def debugTags(self, tags): | |
| for tagName in tags.iterkeys(): | |
| x = tags[tagName].pprint() | |
| os.write(sys.stdout.fileno(), x.encode('UTF-8') + "\n") | |
| def copyFiles(self): | |
| dirsMade = {} | |
| for pathname in self.pathnames: | |
| if not pathname in self.tags: | |
| self.tags[pathname] = ID3(pathname) | |
| tagsINeed = ['TPE1', 'TALB', 'TRCK', 'TIT2'] | |
| for tag in tagsINeed: | |
| if tag not in self.tags[pathname]: | |
| print("missing tag %s" % (tag)) | |
| self.debugTags(self.tags[pathname]) | |
| exit() | |
| newPath = self.destination + u"\\" + self.sanitize(unicode(self.tags[pathname]['TPE1'])) + u"\\" \ | |
| + self.sanitize(unicode(self.tags[pathname]['TALB'])) | |
| if not newPath in dirsMade: | |
| try: | |
| os.makedirs(newPath) | |
| except WindowsError: | |
| print("hoping %s exists" % (newPath)) | |
| dirsMade[newPath] = 1 | |
| os.write(sys.stdout.fileno(), pathname.encode('UTF-8') + "!\n") | |
| track = unicode(self.tags[pathname]['TRCK']) | |
| if '\\' in track: | |
| assert len(track.split('\\')) == 2 | |
| track = track.split('\\')[0] | |
| elif '/' in track: | |
| assert len(track.split('/')) == 2 | |
| track = track.split('/')[0] | |
| trackNo = int(track) | |
| trackName = self.sanitize(unicode(self.tags[pathname]['TIT2'])) | |
| newPathFull = newPath + u"\\" + ( "%s - %s.mp3" % (trackNo, trackName)) | |
| os.write(sys.stdout.fileno(), newPathFull.encode('UTF-8') + "\n") | |
| shutil.copy(pathname, newPathFull) | |
| def sanitize(self, pathname): | |
| invalidChars = u'< > : " / \ | ? * \'' | |
| for char in invalidChars.split(' '): | |
| pathname = pathname.replace(char, u"-") | |
| return pathname.strip() | |
| if len(sys.argv) != 3: | |
| print("""I need two things: the first is a directory where your mp3s are, the second is | |
| the directory where you want me to put stuff. Please give me those things and | |
| try again, for fuck's sake.""") | |
| sys.exit() | |
| read_from_dir, write_to_dir = sys.argv[1:3] | |
| ensureExists(read_from_dir) | |
| ensureExists(write_to_dir) | |
| #ensureEmpty(write_to_dir) | |
| ensureNotEqual(read_from_dir, write_to_dir) | |
| print("Okay! reading from %s, writing to %s" % (read_from_dir, write_to_dir)) | |
| pr = Mp3Processor(write_to_dir) | |
| map(pr.processDir, os.walk(unicode(read_from_dir))) | |
| #map(print, pr.pathnames) | |
| #pr.showTags() | |
| pr.copyFiles() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment