b8d6d918a1
I found more places using libsaria.collection instead of libsaria.sources
78 lines
1.7 KiB
Python
78 lines
1.7 KiB
Python
# Bryan Schumaker (10/24/2010)
|
|
|
|
import libsaria
|
|
from libsaria import sources
|
|
import web
|
|
import xm
|
|
|
|
file_to_id = libsaria.sources.file_to_id
|
|
cache = libsaria.cache
|
|
pref_attr = xm.find_preferred_attribute
|
|
|
|
pref_sizes = ["extralarge", "large", "medium", "small"]
|
|
|
|
APIROOT = "http://ws.audioscrobbler.com/2.0/"
|
|
APIKEY = "2c76f85a6704efd74b5a358821284ef9"
|
|
SECRET = "3a6012bfb627b60a884cf33fc044885c"
|
|
|
|
|
|
class LastFmRequest(dict):
|
|
def __init__(self, method):
|
|
self.method = method
|
|
|
|
def print_file(self, file):
|
|
for line in file:
|
|
print line.strip()
|
|
file.seek(0)
|
|
|
|
def place(self, printfile=False):
|
|
url = web.Url(APIROOT + "?method=" + self.method)
|
|
url["api_key"] = APIKEY
|
|
for param in self:
|
|
url[param] = self[param]
|
|
file = url.open()
|
|
if file == None:
|
|
return None
|
|
if printfile==True:
|
|
self.print_file(file)
|
|
doc = xm.parse( file )
|
|
lfm = xm.child(doc)
|
|
if xm.attributes(lfm)["status"] == "ok":
|
|
return xm.child(lfm)
|
|
else:
|
|
return None
|
|
|
|
def lfm_cache_album(file, artist, album):
|
|
req = LastFmRequest("album.getInfo")
|
|
req["album"] = album
|
|
req["artist"] = artist
|
|
doc = req.place()
|
|
if doc == None:
|
|
return False
|
|
node = pref_attr(doc, "image", "size", pref_sizes)
|
|
node = xm.child(node)
|
|
if node == None:
|
|
return
|
|
try:
|
|
url = web.Url(node.data)
|
|
for line in url.open():
|
|
file.write(line)
|
|
return True
|
|
except:
|
|
pass
|
|
return False
|
|
|
|
def get_artwork_id(id):
|
|
artist, album = libsaria.sources.get_attrs(id, "artist", "album")
|
|
cached = cache[artist]
|
|
file = cached.get("%s.jpg" % album, lfm_cache_album, artist, album)
|
|
return file
|
|
|
|
def get_artwork(filepath):
|
|
id = file_to_id(filepath)
|
|
if id == None:
|
|
return
|
|
file = get_artwork_id(id)
|
|
libsaria.event.start("POSTGETART", file)
|
|
libsaria.event.invite("POSTLOAD", get_artwork, True)
|