libsaria: Don't automatically create cache file

The cache filler function will pass back a string to be written to this
file.  If this string != None, then I create the file and fill it.
This commit is contained in:
Bryan Schumaker 2011-06-18 09:39:55 -04:00
parent e6849dc5cd
commit 7319767ac3
2 changed files with 19 additions and 16 deletions

View File

@ -48,7 +48,7 @@ class LastFmRequest(dict):
else: else:
return None return None
def lfm_cache_album(artist, album, title, file): def lfm_cache_album(artist, album, title):
req = LastFmRequest("album.getInfo") req = LastFmRequest("album.getInfo")
req["album"] = album req["album"] = album
req["artist"] = artist req["artist"] = artist
@ -61,16 +61,16 @@ def lfm_cache_album(artist, album, title, file):
return return
try: try:
url = web.Url(node.data) url = web.Url(node.data)
for line in url.open(): text = url.open().read()
file.write(line) return text
return True
except: except:
pass pass
return False return None
def get_album_art(attrs): def get_album_art(attrs):
file = cache.get_item(attrs["artist"], attrs["album"], attrs["title"], "Folder.jpg", lfm_cache_album) file = cache.get_item(attrs["artist"], attrs["album"], attrs["title"], "Folder.jpg", lfm_cache_album, "images/ocarina.png")
callbacks.get_art(file, attrs["id"]) if file != None:
callbacks.get_art(file, attrs["id"])
def get_artwork(attrs): def get_artwork(attrs):
threading.Thread(target=get_album_art, args=(attrs,)).start() threading.Thread(target=get_album_art, args=(attrs,)).start()

View File

@ -30,18 +30,21 @@ def make_cache_path(artist, album, file):
return join(cache_path, file) return join(cache_path, file)
def fill_cache(artist, album, title, cache_func, cache_path): def fill_cache(artist, album, title, cache_func, cache_path):
f = open(cache_path, 'w') text = cache_func(artist, album, title)
cache_func(artist, album, title, f) if text != None:
f.close() f = open(cache_path, 'w')
if file_empty(cache_path): f.write(text)
os.remove(cache_path) f.close()
return None return True
return cache_path 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) cache_path = make_cache_path(artist, album, file)
filled = True
if not file_exists(cache_path): 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 return cache_path
def test_item(artist, album, file, default): def test_item(artist, album, file, default):