diff --git a/pyItunes/Library.py b/pyItunes/Library.py new file mode 100644 index 00000000..460a1519 --- /dev/null +++ b/pyItunes/Library.py @@ -0,0 +1,41 @@ +from pyItunes.Song import Song +import time +class Library: + def __init__(self,dictionary): + self.songs = self.parseDictionary(dictionary) + + def parseDictionary(self,dictionary): + songs = [] + format = "%Y-%m-%dT%H:%M:%SZ" + for song,attributes in dictionary.iteritems(): + s = Song() + s.name = attributes.get('Name') + s.artist = attributes.get('Artist') + s.album_artist = attributes.get('Album Aritst') + s.composer = attributes.get('Composer') + s.album = attributes.get('Album') + s.genre = attributes.get('Genre') + s.kind = attributes.get('Kind') + if attributes.get('Size'): + s.size = int(attributes.get('Size')) + s.total_time = attributes.get('Total Time') + s.track_number = attributes.get('Track Number') + if attributes.get('Year'): + s.year = int(attributes.get('Year')) + if attributes.get('Date Modified'): + s.date_modified = time.strptime(attributes.get('Date Modified'),format) + if attributes.get('Date Added'): + s.date_added = time.strptime(attributes.get('Date Added'),format) + if attributes.get('Bit Rate'): + s.bit_rate = int(attributes.get('Bit Rate')) + if attributes.get('Sample Rate'): + s.sample_rate = int(attributes.get('Sample Rate')) + s.comments = attributes.get("Comments ") + if attributes.get('Rating'): + s.rating = int(attributes.get('Rating')) + if attributes.get('Play Count'): + s.play_count = int(attributes.get('Play Count')) + if attributes.get('Location'): + s.location = attributes.get('Location') + songs.append(s) + return songs \ No newline at end of file diff --git a/pyItunes/Library.pyc b/pyItunes/Library.pyc new file mode 100644 index 00000000..b8a8ca11 Binary files /dev/null and b/pyItunes/Library.pyc differ diff --git a/pyItunes/Song.py b/pyItunes/Song.py new file mode 100644 index 00000000..27d44d79 --- /dev/null +++ b/pyItunes/Song.py @@ -0,0 +1,46 @@ +class Song: + """ + Song Attributes: + name (String) + artist (String) + album_arist (String) + composer = None (String) + album = None (String) + genre = None (String) + kind = None (String) + size = None (Integer) + total_time = None (Integer) + track_number = None (Integer) + year = None (Integer) + date_modified = None (Time) + date_added = None (Time) + bit_rate = None (Integer) + sample_rate = None (Integer) + comments = None (String) + rating = None (Integer) + album_rating = None (Integer) + play_count = None (Integer) + location = None (String) + """ + name = None + artist = None + album_arist = None + composer = None + album = None + genre = None + kind = None + size = None + total_time = None + track_number = None + year = None + date_modified = None + date_added = None + bit_rate = None + sample_rate = None + comments = None + rating = None + album_rating = None + play_count = None + location = None + + #title = property(getTitle,setTitle) \ No newline at end of file diff --git a/pyItunes/Song.pyc b/pyItunes/Song.pyc new file mode 100644 index 00000000..565886d9 Binary files /dev/null and b/pyItunes/Song.pyc differ diff --git a/pyItunes/XMLLibraryParser.py b/pyItunes/XMLLibraryParser.py new file mode 100644 index 00000000..7e4b239a --- /dev/null +++ b/pyItunes/XMLLibraryParser.py @@ -0,0 +1,42 @@ +import re +class XMLLibraryParser: + def __init__(self,xmlLibrary): + f = open(xmlLibrary) + s = f.read() + lines = s.split("\n") + self.dictionary = self.parser(lines) + + def getValue(self,restOfLine): + value = re.sub("<.*?>","",restOfLine) + u = unicode(value,"utf-8") + cleanValue = u.encode("ascii","xmlcharrefreplace") + return cleanValue + + def keyAndRestOfLine(self,line): + rawkey = re.search('(.*?)',line).group(0) + key = re.sub("","",rawkey) + restOfLine = re.sub(".*?","",line).strip() + return key,restOfLine + + def parser(self,lines): + dicts = 0 + songs = {} + inSong = False + for line in lines: + if re.search('',line): + dicts += 1 + if re.search('',line): + dicts -= 1 + inSong = False + songs[songkey] = temp + if dicts == 2 and re.search('(.*?)',line): + rawkey = re.search('(.*?)',line).group(0) + songkey = re.sub("","",rawkey) + inSong = True + temp = {} + if dicts == 3 and re.search('(.*?)',line): + key,restOfLine = self.keyAndRestOfLine(line) + temp[key] = self.getValue(restOfLine) + if len(songs) > 0 and dicts < 2: + return songs + return songs \ No newline at end of file diff --git a/pyItunes/XMLLibraryParser.pyc b/pyItunes/XMLLibraryParser.pyc new file mode 100644 index 00000000..79cd2bce Binary files /dev/null and b/pyItunes/XMLLibraryParser.pyc differ diff --git a/pyItunes/__init__.py b/pyItunes/__init__.py new file mode 100644 index 00000000..bc7acfad --- /dev/null +++ b/pyItunes/__init__.py @@ -0,0 +1,3 @@ +from pyItunes.XMLLibraryParser import XMLLibraryParser +from pyItunes.Library import Library +from pyItunes.Song import Song \ No newline at end of file diff --git a/pyItunes/__init__.pyc b/pyItunes/__init__.pyc new file mode 100644 index 00000000..5090244d Binary files /dev/null and b/pyItunes/__init__.pyc differ