diff --git a/libsaria/path/lastfm.py b/libsaria/path/lastfm.py index 34bf8dee..984226b4 100644 --- a/libsaria/path/lastfm.py +++ b/libsaria/path/lastfm.py @@ -48,7 +48,7 @@ class LastFmRequest(dict): else: return None -def lfm_cache_album(artist, album, title, file): +def lfm_cache_album(artist, album, title): req = LastFmRequest("album.getInfo") req["album"] = album req["artist"] = artist @@ -61,16 +61,16 @@ def lfm_cache_album(artist, album, title, file): return try: url = web.Url(node.data) - for line in url.open(): - file.write(line) - return True + text = url.open().read() + return text except: pass - return False + return None def get_album_art(attrs): - file = cache.get_item(attrs["artist"], attrs["album"], attrs["title"], "Folder.jpg", lfm_cache_album) - callbacks.get_art(file, attrs["id"]) + file = cache.get_item(attrs["artist"], attrs["album"], attrs["title"], "Folder.jpg", lfm_cache_album, "images/ocarina.png") + if file != None: + callbacks.get_art(file, attrs["id"]) def get_artwork(attrs): threading.Thread(target=get_album_art, args=(attrs,)).start() diff --git a/libsaria/storage/cache.py b/libsaria/storage/cache.py index fa2036de..8ba2eb52 100644 --- a/libsaria/storage/cache.py +++ b/libsaria/storage/cache.py @@ -30,18 +30,21 @@ def make_cache_path(artist, album, file): return join(cache_path, file) def fill_cache(artist, album, title, cache_func, cache_path): - f = open(cache_path, 'w') - cache_func(artist, album, title, f) - f.close() - if file_empty(cache_path): - os.remove(cache_path) - return None - return cache_path + text = cache_func(artist, album, title) + if text != None: + f = open(cache_path, 'w') + f.write(text) + f.close() + return True + return False -def get_item(artist, album, title, file, cache_func): +def get_item(artist, album, title, file, cache_func, default): cache_path = make_cache_path(artist, album, file) + filled = True if not file_exists(cache_path): - cache_path = fill_cache(artist, album, title, cache_func, cache_path) + filled = fill_cache(artist, album, title, cache_func, cache_path) + if filled == False: + return default return cache_path def test_item(artist, album, file, default):