class RatingsCacheAgent(AgentThreadedWithEvents): TIMERS_SPEC=[ ("min", 1, "t_ProcessMbid") ] TABLE_PARAMS=[("id", "integer primary key") ,("created", "integer") ,("updated", "integer") ,("source", "text") ,("artist_name", "text") ,("album_name", "text") ,("track_name", "text") ,("rating", "float") ] BATCH_MBID_MAX=50 def __init__(self, dbpath, dev_mode=False): AgentThreadedWithEvents.__init__(self) self.dbh=DbHelper(dbpath, "cache", self.TABLE_PARAMS) def h_rating(self, source, timestamp, artist_name, album_name, track_name, rating): """ Caches the rating locally This message is issued as a result of receving the "/Ratings/rating" DBus signal """ now=time.time() statement="""INSERT INTO cache (created, updated, source, artist_name, album_name, track_name, track_mbid, rating) VALUES (?, ?, ?, ?, ?, ?, ?)""" try: self.dbh.executeStatement(statement, now, timestamp, artist_name, album_name, track_name, "", rating) self.dbh.commit() except Exception,e: self.pub("log", "error", "%s: error writing to database for inserting a rating (%s)" % (self.__class__, e))
class RatingsCacheAgent(AgentThreadedWithEvents): TIMERS_SPEC = [ ("min", 1, "t_processMbid"), ("min", 1, "t_findUploads") # ,("min", 1, "t_processDetectMb") ] TABLE_PARAMS = [ ("id", "integer primary key"), ("created", "integer"), ("updated", "integer"), ("source", "text"), ("artist_name", "text"), ("album_name", "text"), ("track_name", "text"), ("track_mbid", "text"), ("rating", "float"), ] BATCH_UPLOAD_MAX = 100 BATCH_MBID_MAX = 200 # MB_DETECT_GONE_THRESHOLD=3 ## minutes def __init__(self, dbpath, dev_mode=False): AgentThreadedWithEvents.__init__(self) self.dbh = DbHelper(dbpath, "ratings_cache", self.TABLE_PARAMS) self.mb_detected = False def h_to_update(self, source, ref, timestamp, artist_name, album_name, track_name, rating): """ Caches the rating locally once the database has determine it is OK to do so """ now = time.time() statement = ( """UPDATE %s SET updated=?, rating=? WHERE artist_name=? AND album_name=? AND track_name=?""" % self.dbh.table_name ) try: self.dbh.executeStatement(statement, timestamp, rating, artist_name, album_name, track_name) self.dbh.commit() except Exception, e: self.pub("llog", "fpath/cache", "error", "Database insertion error (%s)" % e) self.dprint("cache: update error: %s" % e) return rc = self.dbh.rowCount() if rc > 0: self.dprint( "! rating updated in cache: artist(%s) album(%s) track(%s) rating(%s)" % (artist_name, album_name, track_name, rating) ) return if rc == 0: statement = ( """INSERT INTO %s (created, updated, source, artist_name, album_name, track_name, track_mbid, rating) VALUES (?, ?, ?, ?, ?, ?, ?, ?)""" % self.dbh.table_name ) try: self.dbh.executeStatement( statement, now, timestamp, source, artist_name, album_name, track_name, "", rating ) self.dbh.commit() except Exception, e: self.pub("llog", "fpath/cache", "error", "Database insertion error (%s)" % e) self.dprint("cache: insertion error: %s" % e) return self.dprint( "! rating inserted in cache: artist(%s) album(%s) track(%s) rating(%s)" % (artist_name, album_name, track_name, rating) )