class JamendoManager(Singleton, Client): def __init__(self): Singleton.__init__(self) Client.__init__(self) self.prefs = PluginPrefs("jamendo") self.username = self.get_preference("username", "") self.password = self.get_preference("password", "") def is_logged(self): return self.logged def has_preference(self, name): return self.prefs.has_key(name) def get_preference(self, name, default=None): return self.prefs.get(name, default) def set_preference(self, name, value): self.prefs[name] = value self.prefs.save() def get_username(self): return self.username def set_username(self, value): self.username = value self.set_preference("username", value) def get_password(self): return self.password def set_password(self, value): self.password = value self.set_preference("password", value)
class AudioScrobbler(PlayerHook): terra_type = "Hook/Player/Audio" time_cons = 240 np_time = 5 max_cached = 200 def __init__(self): PlayerHook.__init__(self) self._model = None self._timer = None self.start_time = None self._timer_paused = False self.prefs = PluginPrefs("lastfm") self.sending_submits = False self.pending_submits = Queue() def media_changed(self, model): """Function that is called everytime that the Player's Controller changes the model. """ if self._timer is not None: self._timer.delete() self._timer_paused = False log.warning("media changed to %s" % model.title) self._model = model self._length = 0 self.start_time = None def _validate_cmd(self, submit=False, length=None, name=None, album=None): dsc = bool(submit) and "submit" or "now playing" name = name or self._model.name album = album or self._model.album if length is None: length = self._length if not lastfm_manager.get_username() or \ not lastfm_manager.get_password(): log.warning("%s ignored (user or pass empty): %s - %s" % \ (dsc, name, album)) return False if not lastfm_manager.scrobble_enabled: log.warning("%s ignored (scrobble disabled): %s - %s" % \ (dsc, name, album)) return False if lastfm_manager.session_id and not lastfm_manager.post_session_id: log.warning("%s ignored (no post sessiond id): %s - %s" % \ (dsc, name, album)) return False if submit and not length: log.error("%s ignored (length not specified): %s - %s" % \ (dsc, name, album)) return False log.warning("sending %s: %s - %s" % (dsc, name, album)) return True def _now_playing(self): if self._validate_cmd() and (network and network.status > 0.0): ThreadedFunction(None, lastfm_manager.now_playing, self._model.name, self._model.artist, self._model.album, self._model.trackno, self._length).start() def _cache_submit_send(self): # create offline cache if not exists if not self.prefs.has_key('submit_cache'): self.prefs['submit_cache'] = [] # refresh offline cache from queue while self.pending_submits.qsize() > 0: if len(self.prefs['submit_cache']) >= self.max_cached: self.prefs['submit_cache'] = \ self.prefs['submit_cache'][-self.max_cached + 1:] args = self.pending_submits.get() self.prefs['submit_cache'].append(args) # save to file self.prefs.save() # nothing to do if not self.prefs['submit_cache']: return # send all cached submits for args in self.prefs['submit_cache']: if self._validate_cmd(submit=True, length=args[4], name=args[0], album=args[2]): try: lastfm_manager.submit(*args) except Exception, e: log.error("error on submit %s" % e.message) self.prefs['submit_cache'] = self.prefs['submit_cache'][1:] # save to file self.prefs.save()