def sql(self, query_string): cur = self.con.cursor() try: r = cur.execute(query_string) logger.debug("SQL query string: %s", query_string) message_list = snstype.MessageList() for m in r: obj = self._str2pyobj(m[1]) obj.msg_id = m[0] message_list.append(obj) return message_list except Exception, e: logger.warning("Catch exception when executing '%s': %s", query_string, e) return snstype.MessageList()
def dump(self, fn_pickle): cur = self.con.cursor() r = cur.execute(''' SELECT id,time,userid,username,text,pyobj,flag FROM msg ''') message_list = snstype.MessageList() for m in r: obj = self._str2pyobj(m[5]) obj.msg_id = m[0] obj.flag = m[6] message_list.append(obj) r = cur.execute(''' SELECT msg_id,tag_id FROM msg_tag ''') tag_list = [] for m in r: tag_list.append(m) message = { 'message_list': message_list, 'tag_list': tag_list } with open(fn_pickle, 'w') as fp: fp.write(Serialize.dumps(message))
def sql(self, condition): cur = self.con.cursor() try: # We trust the client string. This software is intended for personal use. qs = "SELECT DISTINCT msg.id,msg.pyobj FROM msg,msg_tag WHERE %s" % condition r = cur.execute(qs) logger.debug("SQL query string: %s", qs) message_list = snstype.MessageList() for m in r: obj = self._str2pyobj(m[1]) obj.msg_id = m[0] message_list.append(obj) return message_list except Exception, e: logger.warning("Catch exception when executing '%s': %s", condition, e) return snstype.MessageList()
def filter_duplicate(fn_pickle, ml): try: sig = pickle.load(open(fn_pickle)) except IOError: sig = set() ret = snstype.MessageList() for m in ml: s = m.digest() if not s in sig: sig.add(s) ret.append(m) pickle.dump(sig, open(fn_pickle, 'w')) return ret
def input(self, channel=None): if channel: ml = self._home_timeline(channel) else: ml = snstype.MessageList() for chn in self.sp: ml.extend(self._home_timeline(chn)) count = 0 for m in ml: if self._inqueue(m): count += 1 logger.info("Input %d new message", count) self.log("Input %d new message" % count) return "Input %s new messages" % count
def output(self, count=20): cur = self.con.cursor() r = cur.execute( ''' SELECT id,time,userid,username,text,pyobj,weight FROM msg WHERE flag='unseen' ORDER BY time DESC LIMIT ? ''', (count, )) message_list = snstype.MessageList() for m in r: obj = self._str2pyobj(m[5]) obj.msg_id = m[0] obj.weight = m[6] message_list.append(obj) return message_list
def output_ranked(self, count, younger_than): latest_time = int(self.time() - younger_than) cur = self.con.cursor() r = cur.execute( ''' SELECT id,time,userid,username,text,pyobj,weight FROM msg WHERE flag='unseen' AND time>? ORDER BY weight DESC LIMIT ? ''', (latest_time, count)) message_list = snstype.MessageList() for m in r: obj = self._str2pyobj(m[5]) obj.msg_id = m[0] obj.weight = m[6] message_list.append(obj) return message_list