From b5efe08f3596506ec7663f5de28900f93539f3c6 Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Fri, 14 Apr 2017 19:46:45 +0200 Subject: [PATCH] Improve iTunesParser Doc + var name for PEP8 --- iTunesParser.py | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/iTunesParser.py b/iTunesParser.py index bed700d..60a028e 100644 --- a/iTunesParser.py +++ b/iTunesParser.py @@ -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