Improve iTunesParser
Doc + var name for PEP8
This commit is contained in:
@@ -60,33 +60,52 @@ class SetEncoder(json.JSONEncoder):
|
||||
|
||||
|
||||
class ITunesParser:
|
||||
"""
|
||||
Parse an iTunes Library and produce JSON - for ELS
|
||||
"""
|
||||
def __init__(self, libraryFile):
|
||||
self._albums = {}
|
||||
self._artists = {}
|
||||
self.libraryFile = libraryFile
|
||||
|
||||
def toJson(self):
|
||||
output = self._processSongs()
|
||||
"""
|
||||
Just do processSong() - This method suck
|
||||
"""
|
||||
|
||||
ret = self._processSongs()
|
||||
|
||||
# self._writeArtists()
|
||||
# self._writeAlbums()
|
||||
|
||||
# return json.dumps(jsonObj, indent=indent, cls=SetEncoder)
|
||||
return output
|
||||
return ret
|
||||
|
||||
def toJsonP(self, rating=4):
|
||||
json = self.toJson()
|
||||
jsonp = ';itgCallback(' + json + ');'
|
||||
def toJsonP(self): # TODO parameter, rating=4):
|
||||
"""
|
||||
Produce JSON-P content
|
||||
"""
|
||||
|
||||
jsonContent = self.toJson()
|
||||
jsonp = ';itgCallback(' + jsonContent + ');'
|
||||
return jsonp
|
||||
|
||||
|
||||
def _readTracks(self):
|
||||
"""
|
||||
Read library and return Tracks part
|
||||
"""
|
||||
pl = plistlib.readPlist(self.libraryFile)
|
||||
return pl['Tracks']
|
||||
|
||||
def _processSongs(self):
|
||||
"""
|
||||
Return an output JSON for an ELS Bulk request - Not a correct format
|
||||
TODO Just return a _correct_ JSON and treat in another place/class
|
||||
"""
|
||||
|
||||
tracks = self._readTracks()
|
||||
output = ""
|
||||
ret = ""
|
||||
|
||||
for k in tracks:
|
||||
track = tracks[k]
|
||||
@@ -107,13 +126,17 @@ class ITunesParser:
|
||||
# Retrieve for each track album information
|
||||
self._processAlbum(track)
|
||||
|
||||
output += json.dumps(jsonTrackIndic, indent=None, cls=SetEncoder)
|
||||
output += "\n"
|
||||
output += json.dumps(track, indent=None, cls=SetEncoder)
|
||||
output += "\n"
|
||||
return output
|
||||
ret += json.dumps(jsonTrackIndic, indent=None, cls=SetEncoder)
|
||||
ret += "\n"
|
||||
ret += json.dumps(track, indent=None, cls=SetEncoder)
|
||||
ret += "\n"
|
||||
return ret
|
||||
|
||||
def _processArtist(self, track):
|
||||
"""
|
||||
Process artists in the track part of library and return a JSON formated for a bulk ELS request
|
||||
"""
|
||||
|
||||
if 'Artist' not in track:
|
||||
return
|
||||
|
||||
@@ -144,6 +167,10 @@ class ITunesParser:
|
||||
return
|
||||
|
||||
def _processAlbum(self, track):
|
||||
"""
|
||||
Process albums in the track part of library and return a JSON formated for a bulk ELS request
|
||||
"""
|
||||
|
||||
if 'Album' not in track:
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user