Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # encoding: utf-8
- import sys
- import os
- import unicodedata
- try:
- from mutagen.easyid3 import EasyID3
- except ImportError:
- sys.path.append('/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/')
- from mutagen.easyid3 import EasyID3
- searchPath = u'/Users/ripdog/Music/Local Music Lib/iTunes Media'
- allowedTypes = ['mp3', 'ogg', 'flac']
- class song(object):
- '''Class that reads and contains metadata about a media file.'''
- def __init__(self, location):
- self.file = EasyID3(location)
- self.ID = MakeID()
- self.location = location
- try:
- self.name = self.file['title'][0]
- except KeyError:
- self.name = None
- try:
- self.album = self.file['album'][0]
- except KeyError:
- self.album = None
- try:
- self.artist = self.file['artist'][0]
- except KeyError:
- self.artist = None
- try:
- self.year = self.file['date'][0]
- except KeyError:
- self.year = None
- try:
- self.genre = self.file['genre'][0]
- except KeyError:
- self.genre = None
- try:
- self.track = self.file['tracknumber'][0]
- except KeyError:
- self.track = None
- def __str__(self):
- return 'Track number: ' + self.track + '\nTrack name: ' + self.name + '\nTrack artist: ' + self.artist + '\nTrack genre: yio ' + self.genre
- class library(object):
- """A class that maintains a list of all songs."""
- def __init__(self):
- self.library = []
- def __str__(self):
- for item in self.library:
- # print item
- print 'Track number:', item.track
- print 'Track name: ', item.name
- print 'Track artist: ', item.artist
- print 'Track genre: ', item.genre
- return ""
- def addToLibrary(self, trackLocation):
- self.library.append(song(trackLocation))
- print "Item added! \n", self
- #self.printNumber(3)
- #print self.library
- def printNumber(self, printNo='all'):
- """Prints a specified number of entries, from beginning to end in current sort pattern. Defaults to a fuill print.
- Don't print me! Will append None to the print, just run the function."""
- if printNo == 'all':
- printNo = len(self.library)
- print printNo
- iterator = 0
- while iterator < printNo:
- try:
- testy = self.library[iterator]
- print 'Track number:', self.library[iterator].track
- print 'Track name: ', self.library[iterator].name
- print 'Track artist: ', self.library[iterator].artist
- print 'Track genre: ', self.library[iterator].genre
- iterator += 1
- except IndexError:
- iterator = printNo
- return "Premature stop."
- break
- return
- class musicscanner(object):
- def __init__(self):
- self.currentDirectory = None
- def startScan(self, startDirectory, libraryName):
- os.chdir(startDirectory)
- for root, directory, files in os.walk(startDirectory, topdown=True):
- try:
- item = files.pop()
- print item.encode('utf-8')
- if item.split(".")[-1] in allowedTypes:
- print type(unicodedata.normalize('NFC', item))
- try:
- libraryName.addToLibrary(item)
- except UnicodeDecodeError, IOError:
- break
- except IndexError:
- continue
- # if os.path.splitext(files)[iterator] in allowedTypes:
- #def unicode_me(
- # obj, encoding='utf-8'):
- # if isinstance(obj, basestring):
- # if not isinstance(obj, unicode):
- # obj = unicode(obj, encoding)
- # return obj
- def main():
- newLib = library()
- newScanner = musicscanner()
- newScanner.startScan(startDirectory=searchPath, libraryName=newLib)
- def MakeID():
- return 1
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement