""" Set MP3 Artist Tag Plugin ========================== Sample class which can be used as a plugin for Peter's Flexible Renaming Kit (PFrank) Just copy the class definition (no need to copy the other declarations or test code) to your PFrankUser.py file and add a reference to the class in the PFrankUser.py 'UserRenamerList' table. Or just rename this file as PFrankUser.py and move it to your PFrank install folder if you don't want any other plugins. More details can be found in the PFrankUser.py.template file that is included with the PFrank download. Feb 1, 2008 Initial Creation 0.1 """ ########################################## # list your imported modules here. # If you get import errors, then the modules are not # present in the environment. In this case you will have to # insert the required module in the PFrank install folder. ########################################## # start of user imported modules import re import string # end of user imported modules. import os import eyeD3 ########################################## # The following modules must always be imported ########################################## # start of required imported modules from SearchRep_init import configurator from SearchRepCase import SearchRepLower # end of required imported modules class UserRenameMP3ArtistTitleTag (SearchRepLower) : """ This is a sample renamer class. The SearchRepLower base class is mandatory. This example is used to perform a non-renaming function. The function is to look for (PREVIEW: www.domain.com) text in the artist and album mp3 tags of music files and replace them with empty text. The actual setting of the tags has been commented out for now. """ def __init__ (self) : """ this is the initilaizer for the classee. This is mandatory. """ # call base class initializer. This is mandatory SearchRepLower.__init__(self) # This is the ID string that will appear in the pre-defined command # pull down list. It will also appear in the custom list when # inserted. The id will also be used in the scan summary # The name 'self.idstring' must not change. Set the assigned string # on the right-hand side of the assignment statement to whatever # name you want. self.idstring = "(User) - Change MP3 tag/artist" # this call should remain here self.initforscan() def initforscan(self) : """ This function is called whenever a new scan or rescan is made. Use it to reset anything that needs to be reinitialized before a scan. This function is mandatory. """ try: self.mp3_objFile = eyeD3.Tag() except : self.mp3_objFile = None print "Failed to create mp3file object" pass def fixnames(self, filename) : """ This function converts name of file to a new name. Input: string representing the old filename or portion (either All, Prefix, or Extension) The name does not include the path to the file. Output: change indicator True if change to input string occurred, otherwise False string representing the new filename When the old filename string is passed into this routine, then depending on where this user command is placed in the custom list, the string could be in the middle of a transformation. e.g. if the original filename was ABCDEF.jpg, and the first command of the custom list inserted a counter in front of the name, and the second command was this user command, then the filename string that is passed in could be something like 0010-ABCDEF.jpg. This name of course no longer looks like the original name. If you needed to extract meta data from the file, then you would not be able to use the passed in string. Therefore the global variable 'configurator.filename' is provided which has the full path to the filename. You can then just open that name, extract the meta data, and then close the file. """ # full path to original filename fullPathForFile = configurator.filename prefix, extension = os.path.splitext(filename) ######################################################################### # Insert code to modify the filename here ######################################################################### if extension == '.mp3' and \ self.mp3_objFile != None : self.mp3_objFile.link(fullPathForFile) for i in range (1) : try: valid = eyeD3.isMp3File(fullPathForFile) if valid: self.mp3audioFile = eyeD3.Mp3AudioFile(fullPathForFile) artist = self.mp3_objFile.getArtist() album = self.mp3_objFile.getAlbum() else : #print "not valid" break print "file is: ", fullPathForFile print "artist is: ", artist newartist = artist.replace("(PREVIEW: www.domain.com)", "") print "new artist is: ", newartist #self.mp3_objFile.setArtist(newartist) print "album is: ", album newalbum = album.replace("(PREVIEW: www.domain.com)", "") print "new album is: ", newalbum #self.mp3_objFile.setAlbum(newalbum) #self.mp3_objFile.update() except : # invalid file - ignore break newname = prefix + extension ######################################################################### # these statements are mandatory ######################################################################### change = False if filename != newname : change = True return change, newname ######################################################################### # end of the class definition ######################################################################### ################################################################### # Create the Renamer Objects in a list. The list is mandatory. # The name of the list must not change. # You can list as many names in the list as you like. PFrank will try to # load all of them. Each name in the list must correspond to the # name of a user command renaming class. A name can only appear # once in the list. # # User Command Objects are created with the following syntax: # ClassName() # i.e. the name of the class followed by () ################################################################### UserRenamerList = [ #UserRenamer1(), # object created for first user renaming class #UserRenamer2(), # object created for second user renaming class UserRenameMP3ArtistTitleTag(), ] ###################################### # Code for testing the renamer objects ###################################### if __name__ == '__main__' : """ This is some test code to test the renaming objects. Test the code using the python IDLE tool to verify that things work. Then try importing the file to PFrank """ print "Testing User Defined Renaming Classes" fullpathTable = [ #"TestFile.txt", #"TestFile1.jpg", #"Test32File3.mp3", #"Test1.mp3", #"33T.ogg", "C:\Documents and Settings\peter\Desktop\\PFrankTest\\neftest\sample.mp3", "C:\Documents and Settings\peter\Desktop\\PFrankTest\\neftest\sample2.mp3", ] for renamer in UserRenamerList : print "*************Testing Renamer: ", renamer.idstring for path in fullpathTable : # save the full path in configurator object just as PFrank does configurator.filename = path basename, name = os.path.split(path) # call fixnames with name of file (or folder) change, newname = renamer.fixnames(name) print "Oldname is: ", name print "Newname is: ", newname assert (change == (not name == newname)) print ""