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))
def __init__(self, dbpath, dev_mode=False): AgentThreadedWithEvents.__init__(self) self.current_count=0 self.dbh=DbHelper(dbpath, "ratings_db", self.TABLE_PARAMS)
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
def __init__(self, dbpath, dev_mode=False): AgentThreadedWithEvents.__init__(self) self.dbh = DbHelper(dbpath, "ratings_cache", self.TABLE_PARAMS) self.mb_detected = False
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) )
def __init__(self, dbpath, dev_mode=False): AgentThreadedWithEvents.__init__(self) self.dbh=DbHelper(dbpath, "cache", self.TABLE_PARAMS)