diff --git a/iTunesParser.py b/iTunesParser.py index 60a028e..d3c3d86 100644 --- a/iTunesParser.py +++ b/iTunesParser.py @@ -63,48 +63,48 @@ class ITunesParser: """ Parse an iTunes Library and produce JSON - for ELS """ - def __init__(self, libraryFile): + def __init__(self, library_file): self._albums = {} self._artists = {} - self.libraryFile = libraryFile + self.library_file = library_file - def toJson(self): + def to_json(self): """ Just do processSong() - This method suck """ - ret = self._processSongs() + ret = self._process_songs() -# self._writeArtists() -# self._writeAlbums() + # self._write_artists() + # self._write_albums() # return json.dumps(jsonObj, indent=indent, cls=SetEncoder) return ret - def toJsonP(self): # TODO parameter, rating=4): + def to_json_p(self): # TODO parameter, rating=4): """ Produce JSON-P content """ - jsonContent = self.toJson() - jsonp = ';itgCallback(' + jsonContent + ');' + json_content = self.to_json() + jsonp = ';itgCallback(' + json_content + ');' return jsonp - def _readTracks(self): + def _read_tracks(self): """ Read library and return Tracks part """ - pl = plistlib.readPlist(self.libraryFile) - return pl['Tracks'] + plist = plistlib.readPlist(self.library_file) + return plist['Tracks'] - def _processSongs(self): + def _process_songs(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._read_tracks() ret = "" for k in tracks: @@ -116,23 +116,23 @@ class ITunesParser: if 'Podcast' in track or 'Has Video' in track: continue - persistentId = track['Persistent ID'] - jsonTrackIndic = { - "index": {"_index": "itunessongs", "_type": "song", "_id": persistentId} + persistent_id = track['Persistent ID'] + json_track_index = { + "index": {"_index": "itunessongs", "_type": "song", "_id": persistent_id} } # Retrieve for each track artist information - self._processArtist(track) + self._process_artist(track) # Retrieve for each track album information - self._processAlbum(track) + self._process_album(track) - ret += json.dumps(jsonTrackIndic, indent=None, cls=SetEncoder) + ret += json.dumps(json_track_index, indent=None, cls=SetEncoder) ret += "\n" ret += json.dumps(track, indent=None, cls=SetEncoder) ret += "\n" return ret - def _processArtist(self, track): + def _process_artist(self, track): """ Process artists in the track part of library and return a JSON formated for a bulk ELS request """ @@ -162,11 +162,11 @@ class ITunesParser: return # Split up the Genres - genreParts = track['Genre'].split('/') - self._artists[akey]['genres'] |= set(genreParts) + genre_parts = track['Genre'].split('/') + self._artists[akey]['genres'] |= set(genre_parts) return - def _processAlbum(self, track): + def _process_album(self, track): """ Process albums in the track part of library and return a JSON formated for a bulk ELS request """ @@ -196,8 +196,8 @@ class ITunesParser: return # Split up the Genres - genreParts = track['Genre'].split('/') - self._albums[akey]['genres'] |= set(genreParts) + genre_parts = track['Genre'].split('/') + self._albums[akey]['genres'] |= set(genre_parts) ## Add different artists if 'Artist' not in track: @@ -205,44 +205,52 @@ class ITunesParser: self._albums[akey]['artist'].add(track['Artist']) return -# def _writeArtists(self): -# fileArtist = io.open('es-artist-data.json', 'wb') -# for a in self._artists: -# jsonTrackIndic = { -# "index": {"_index": "itunessongs", "_type": "artist"} -# } -# fileArtist.write(json.dumps(jsonTrackIndic, indent=None, cls=SetEncoder)) -# fileArtist.write("\n") -# fileArtist.write(json.dumps(self._artists[a], indent=None, cls=SetEncoder)) -# fileArtist.write("\n") -# fileArtist.close() + def _write_artists(self): + """ + Write artists data to another JSON file + """ -# def _writeAlbums(self): -# fileAlbums = io.open('es-albums-data.json', 'wb') -# for a in self._albums: -# jsonTrackIndic = { -# "index": {"_index": "itunessongs", "_type": "album"} -# } -# fileAlbums.write(json.dumps(jsonTrackIndic, indent=None, cls=SetEncoder)) -# fileAlbums.write("\n") -# fileAlbums.write(json.dumps(self._albums[a], indent=None, cls=SetEncoder)) -# fileAlbums.write("\n") -# fileAlbums.close() + file_artist = io.open('es-artist-data.json', 'wb') + for artist in self._artists: + json_track_index = { + "index": {"_index": "itunessongs", "_type": "artist"} + } + file_artist.write(bytes(json.dumps(json_track_index, indent=None, cls=SetEncoder), 'UTF-8')) + file_artist.write(bytes("\n", 'UTF-8')) + file_artist.write(bytes(json.dumps(self._artists[artist], indent=None, cls=SetEncoder), 'UTF-8')) + file_artist.write(bytes("\n", 'UTF-8')) + file_artist.close() + + def _write_albums(self): + """ + Write albums data to another JSON file + """ + + file_albums = io.open('es-albums-data.json', 'wb') + for album in self._albums: + json_track_index = { + "index": {"_index": "itunessongs", "_type": "album"} + } + file_albums.write(bytes(json.dumps(json_track_index, indent=None, cls=SetEncoder), 'UTF-8')) + file_albums.write(bytes("\n", 'UTF-8')) + file_albums.write(bytes(json.dumps(self._albums[album], indent=None, cls=SetEncoder), 'UTF-8')) + file_albums.write(bytes("\n", 'UTF-8')) + file_albums.close() #### main block #### # Default input & output files -defaultLibraryFile = os.path.expanduser('iTunesMiniLibrary.xml') -defaultOutputFile = os.path.dirname(os.path.realpath(__file__)) + '/es-music-data.json' +DEFAULT_LIBRARY_FILE = os.path.expanduser('iTunesMiniLibrary.xml') +DEFAULT_OUTPUT_FILE = os.path.dirname(os.path.realpath(__file__)) + '/es-music-data.json' # Get options parser = OptionParser(version="%prog 1.0") parser.add_option('-f', '--file', dest='file', type='string', help='iTunes Library XML file path', - default=defaultLibraryFile) + default=DEFAULT_LIBRARY_FILE) parser.add_option('-o', '--output', dest='output', type='string', help='Output to file (default=./js/music-data.json)', - default=defaultOutputFile) + default=DEFAULT_OUTPUT_FILE) parser.add_option('-c', '--console', dest='console', action='store_true', help='Output to console instead of file') parser.add_option('-p', '--jsonp', dest='jsonp', action='store_true', @@ -253,11 +261,11 @@ parser.add_option('-v', '--verbose', dest='verbose', action='store_true', if __name__ == '__main__': (options, args) = parser.parse_args() - itunesParser = ITunesParser(options.file) + itunes_parser = ITunesParser(options.file) if options.jsonp: - output = itunesParser.toJsonP() + output = itunes_parser.to_json_p() else: - output = itunesParser.toJson() + output = itunes_parser.to_json() if options.console: print(output) diff --git a/pylintrc b/pylintrc index 13e7501..d2137cc 100644 --- a/pylintrc +++ b/pylintrc @@ -30,7 +30,7 @@ load-plugins= # can either give multiple identifier separated by comma (,) or put this option # multiple time (only on the command line, not in the configuration file where # it should appear only once). -disable=C0325 +#disable=C0325 [REPORTS] @@ -112,7 +112,9 @@ bad-functions=filter,apply,input module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ # Regular expression which should only match correct module level names -const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$ +#const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$ +# For constant with lower case characters +const-rgx=(([a-zA-Z0-9_]*)|(__.*__))$ # Regular expression which should only match correct class names class-rgx=[A-Z_][a-zA-Z0-9]+$