Improve iTunesParser

Doc + var name for PEP8
This commit is contained in:
2017-04-14 19:46:45 +02:00
parent 1a3ddf091d
commit b5efe08f35

View File

@@ -60,33 +60,52 @@ class SetEncoder(json.JSONEncoder):
class ITunesParser: class ITunesParser:
"""
Parse an iTunes Library and produce JSON - for ELS
"""
def __init__(self, libraryFile): def __init__(self, libraryFile):
self._albums = {} self._albums = {}
self._artists = {} self._artists = {}
self.libraryFile = libraryFile self.libraryFile = libraryFile
def toJson(self): def toJson(self):
output = self._processSongs() """
Just do processSong() - This method suck
"""
ret = self._processSongs()
# self._writeArtists() # self._writeArtists()
# self._writeAlbums() # self._writeAlbums()
# return json.dumps(jsonObj, indent=indent, cls=SetEncoder) # return json.dumps(jsonObj, indent=indent, cls=SetEncoder)
return output return ret
def toJsonP(self, rating=4): def toJsonP(self): # TODO parameter, rating=4):
json = self.toJson() """
jsonp = ';itgCallback(' + json + ');' Produce JSON-P content
"""
jsonContent = self.toJson()
jsonp = ';itgCallback(' + jsonContent + ');'
return jsonp return jsonp
def _readTracks(self): def _readTracks(self):
"""
Read library and return Tracks part
"""
pl = plistlib.readPlist(self.libraryFile) pl = plistlib.readPlist(self.libraryFile)
return pl['Tracks'] return pl['Tracks']
def _processSongs(self): 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() tracks = self._readTracks()
output = "" ret = ""
for k in tracks: for k in tracks:
track = tracks[k] track = tracks[k]
@@ -107,13 +126,17 @@ class ITunesParser:
# Retrieve for each track album information # Retrieve for each track album information
self._processAlbum(track) self._processAlbum(track)
output += json.dumps(jsonTrackIndic, indent=None, cls=SetEncoder) ret += json.dumps(jsonTrackIndic, indent=None, cls=SetEncoder)
output += "\n" ret += "\n"
output += json.dumps(track, indent=None, cls=SetEncoder) ret += json.dumps(track, indent=None, cls=SetEncoder)
output += "\n" ret += "\n"
return output return ret
def _processArtist(self, track): 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: if 'Artist' not in track:
return return
@@ -144,6 +167,10 @@ class ITunesParser:
return return
def _processAlbum(self, track): 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: if 'Album' not in track:
return return