def getMetaData(songPath): cachePath = os.path.join(cacheDir, str(abs(hash(songPath)))) song = SongFile() try: f = open(cachePath, 'r') jsonData = json.load(f) f.close() # TODO: Eventually we should have these expire after a time if jsonData['path'] == songPath: song.fromJson(jsonData) else: # Hash collision ! # TODO: Do something better about this raise LookupError() except (LookupError, IOError) as e: song.load(songPath) f = open(cachePath,'w') f.write(song.toJson()) f.close() # Pass any errors farther up return song
#!/usr/bin/env python from songfile import SongFile import json mp3Path = "/home/mike/Music/Gorillaz/Demon Days/03 Kids With Guns.mp3" songFile = SongFile() songFile.load(mp3Path) print "songFile" print songFile print hash(songFile) print songFile.jsonHash() print jsonStr = songFile.toJson() print "jsonStr" print jsonStr print songFile2 = SongFile() songFile2.fromJson(jsonStr) print "songFile2" print songFile2 print songFile2.artist print hash(songFile2) print songFile2.jsonHash()