class RatingsDbAgent(AgentThreadedWithEvents): MAX_RETRIEVE_LIMIT=1000 TIMERS_SPEC=[ #("min", 1, "t_announceDbCount") ("min", 1, "t_announceUpdated") #,("sec", 10, "t_countRatings") ] TABLE_PARAMS=[("id", "integer primary key") ,("created", "integer") ,("updated", "integer") ,("source", "text") ,("wsid", "text") ,("artist_name", "text") ,("album_name", "text") ,("track_name", "text") ,("track_mbid", "text") ,("rating", "float") ] def __init__(self, dbpath, dev_mode=False): AgentThreadedWithEvents.__init__(self) self.current_count=0 self.dbh=DbHelper(dbpath, "ratings_db", self.TABLE_PARAMS) ## ====================================================================== ## MESSAGE HANDLERS def h_in_rating(self, source, ref, timestamp, artist_name, album_name, track_name, rating): """ From Dbus "rating" First check if we have a different value in the database - if not, abort. """ statement="""SELECT * FROM %s WHERE artist_name=? AND album_name=? AND track_name=? AND rating<>? LIMIT 1 """ % self.dbh.table_name try: self.dbh.executeStatement(statement, artist_name, album_name, track_name, rating) result=self.dbh.fetchOne(None) except Exception,e: self.pub("llog", "fpath/cache", "error", "Database reading error (%s)" % e) return ### Can't be found OR different rating... then add to the database AND signal ### so that the 'cache' also put to record ready for uploading to the web-service if result is None: now=time.time() ### First try to update if possible statement="""UPDATE %s SET updated=?, rating=? WHERE artist_name=? AND album_name=? AND track_name=? """ % self.dbh.table_name try: self.dbh.executeStatement(statement, now, rating, artist_name, album_name, track_name) self.dbh.commit() c=self.dbh.rowCount() if c==1: ## success self.dprint("db: updated, a(%s) b(%s) t(%s): %s" % (artist_name, album_name, track_name, rating)) return except Exception,e: self.pub("llog", "fpath/db", "error", "Database update error (%s)" % e) return statement=""" INSERT INTO %s ( created, updated, source, artist_name, album_name, track_name, rating) VALUES( ?, ?, ?, ?, ?, ?, ?) """ % self.dbh.table_name try: self.dbh.executeStatement(statement, now, now, source, artist_name, album_name, track_name, rating) self.dbh.commit() self.dprint("db: inserted, a(%s) b(%s) t(%s): %s" % (artist_name, album_name, track_name, rating)) except Exception,e: self.pub("llog", "fpath/db", "error", "Database insertion error (%s)" % e) return