+import threading
+
+class CoverFetcher(threading.Thread):
+ def __init__(self):
+ threading.Thread.__init__(self)
+ self.setDaemon(True)
+ self.cond = threading.Condition()
+ self.work = []
+
+ def _fetch_cover(self, albumid, size):
+ coverdir = _COVERDIR if _COVERDIR else '/tmp'
+ to = os.path.join(coverdir, '%d-%d.jpg'%(albumid, size))
+ if not os.path.isfile(to):
+ url = _GET2+'image/album/redirect/?id=%d&imagesize=%d'%(albumid, size)
+ urllib.urlretrieve(url, to)
+ return to
+
+ def request_cover(self, albumid, size, cb):
+ self.cond.acquire()
+ self.work.insert(0, (albumid, size, cb))
+ self.cond.notify()
+ self.cond.release()
+
+ def run(self):
+ while True:
+ work = []
+ self.cond.acquire()
+ while True:
+ work = self.work
+ if work:
+ self.work = []
+ break
+ self.cond.wait()
+ self.cond.release()
+
+ for albumid, size, cb in work:
+ cover = self._fetch_cover(albumid, size)
+ cb(albumid, size, cover)
+