Start New Discussion within our Software Development Community

This simple script will copy all music files that were not purchased from the iTunes store to a directory of your choosing. It requires the ID3 module (Created by some person, from somewhere, I forget where, but I DID NOT make this module). The syntax is as follows:

ipoddir = 'E:'
newdir = 'C:\\Documents and Settings\\#\\My Documents\\ipodtest'
a = iPod(ipoddir, newdir debug='on')

That's it, it'll run just like that, and show you which file it's working on, and where it's putting it. It'll organize it by Artist, then Album. As of now, I have not included options for organizing, or for going through steps yet. But it will copy the files, so long as the iPod is not a iPod touch or iPhone
ID3 Module: http://id3-py.sourceforge.net/

'''
iPod Music Copy Module
Copies the music from your iPod to a folder on your computer
'''

from os import rename,  \
               chdir,   \
               getcwd,  \
               listdir, \
               mkdir,   \
               path
from shutil import copy2
from ID3 import ID3

class iPod():

    def __init__(self, iPod, direct, new='no', debug='off', organize='yes'):
        '''
        iPod is the root directory of the iPod
        direct is the directory to copy to
        define new to 'yes' to take you through steps
        '''
        if new != 'yes':
            if len(iPod) == 1:
                iPod = iPod + ":"
            if len(iPod) > 2:
                iPod = iPod[0:2]
            self.iPoddir = iPod+"\\iPod_Control\\Music"
            if (path.exists(self.iPoddir) == False) or (path.exists(direct) == False):
                raw_input("Please press 'Enter' to quit the application\nInvalid iPod or directory")
                exit()
            self.newdirect = direct
            self.organize = organize
            self.debug = 'on'
            self.runipodchange2()
        if new == 'yes':
            print("In order to copy the music from your iPod to your computer, there are a few steps you need to do.")
            print("First off, you need to make your iPod seeable from My Computer")
            print("Also, you'll need to disable auto-sync in order to keep your iPod music in-tact")
            print("If it's not off, simply hold SHIFT and CTRL while plugging in the iPod")
            print("In order to view the iPod in My Computer, ")

    def runipodchange2(self):
        # Change the directory to the directory of the ipod
        chdir(self.iPoddir)
        # List the directory
        folders = listdir('')
        if self.debug == 'on': print(self.iPoddir, folders)
        # Make a list of all songs to keep track of len()
        songstobecopied = []
        for folder in folders:
            for files in listdir(folder):
                songstobecopied.append(files)
        if self.debug == 'on': print(songstobecopied)
        songstobecopied = len(songstobecopied)
        # Make a counter to keep track of which file we're using
        cursong = 0
        # Start the loop to copy folders
        for folder in folders:
            for files in listdir(folder):
                cursong += 1
                if self.debug == 'on': print("Copying file: %s; %d of %d" %(files, cursong, songstobecopied))
                oldfile = self.iPoddir+"\\"+folder+"\\"+files
                # Open the song to check ID3 tags
                song = ID3(oldfile)
                # Strip the filepath of any '/' and ':'
                newdir = self.newdirect + "\\"
                newdir += '_'.join(song.artist.split('/')) + "\\"
                newdir += '_'.join(song.album.split('/')) + "\\"
                newdir = ''.join(newdir.split(':'))
                newdir = newdir[0]+":"+newdir[1:]
                if self.debug == 'on': print(newdir)
                newdirartist = ''
                for x in newdir.split('\\')[:-3]:
                    newdirartist += x + "\\"
                newdirartist += newdir.split('\\')[-3]
                #print(newdirartist)
                try:
                    mkdir(newdirartist)
                except:
                    pass
                try:
                    mkdir(newdir)
                except:
                    pass
                if path.exists(newdir+"\\"+files) != True:
                    songname = newdir+"\\"+files
                    copy2(oldfile, songname)
                elif path.exists(newdir+"\\"+files) == True:
                    asdf = 0
                    while path.exists(newdir+"\\"+str(asdf)+" "+files) == True:
                        asdf += 1
                    songname = newdir+"\\"+str(asdf)+" "+files
                    copy2(oldfile, songname)
                # Now is time to rename the file
                if songname.split('\\')[-1] != song.title+".mp3":
                    newname = '\\'.join(songname.split('\\')[:-1])+song.title+".mp3"
                    if path.exists(newname) != True:
                        rename(songname, '\\'.join(songname.split('\\')[:-1])+song.title+".mp3")
                    elif path.exists(newname) == True:
                        filecount = 0
                        while path.exists(newname[:-4]+str(filecount)+".mp3"):
                            filecount += 1
                        newname = newname[:-4]+str(filecount)+".mp3"
                        rename(songname, newname)
                song.file.close()