def __init__(self, path, user_id): self.path = os.path.abspath(path) if not os.path.exists(self.path): raise IOError('Path `%s` not exist for user %s' % (self.path, user_id)) self.user_id = user_id self.user_hash = id_to_digest(user_id)
def get_chatroom_records(self, chatroom_id, start=datetime(2000, 1, 1), end=datetime(2050, 1, 1)): chatroom_hash = id_to_digest(chatroom_id) chatroom_table = 'Chat_' + chatroom_hash if start is None: start = datetime(2000, 1, 1) if end is None: end = datetime(2050, 1, 1) start = calendar.timegm(start.utctimetuple()) end = calendar.timegm(end.utctimetuple()) chat_db = self.path + '/%s/DB/MM.sqlite' % self.user_hash logger.debug('DB path %s' % chat_db) conn = sqlite3.connect(chat_db) records = [] id_contained_types = (RecordType.TEXT, RecordType.IMAGE, RecordType.VOICE, RecordType.CARD, RecordType.EMOTION, RecordType.LOCATIOM, RecordType.LINK) for row in conn.execute( "SELECT * FROM %s WHERE CreateTime BETWEEN '%s' and '%s'" % (chatroom_table, start, end)): created_at, msg, msg_type, not_self = row[3], row[4], row[7], row[ 8] user_id = None # split out user_id in msg if not_self and msg_type in id_contained_types: if msg_type == 48: msg = row[4] continue user_id, msg = row[4].split(':\n', 1) # TODO: get user_id in non id_contained_types if not not_self: user_id = self.user_id record = dict(user_id=user_id, msg=msg, type=msg_type, not_self=not_self, created_at=created_at) records.append(record) return records
def get_chatroom_records(self, chatroom_id, start=datetime(2000, 1, 1), end=datetime(2050, 1, 1)): chatroom_hash = id_to_digest(chatroom_id) chatroom_table = 'Chat_' + chatroom_hash start = calendar.timegm(start.utctimetuple()) end = calendar.timegm(end.utctimetuple()) chat_db = self.path + '/%s/DB/MM.sqlite' % self.user_hash logger.debug('DB path %s' % chat_db) conn = sqlite3.connect(chat_db) records = [] for row in conn.execute("SELECT * FROM %s WHERE CreateTime BETWEEN '%s' and '%s'" % (chatroom_table, start, end)): created_at, msg, msg_type, not_self = row[3], row[4] ,row[7], row[8] user_id = None # split out user_id in msg id_contained_types = (RecordType.TEXT, RecordType.IMAGE, RecordType.VOICE, RecordType.CARD, RecordType.EMOTION, RecordType.LOCATIOM, RecordType.LINK) if not_self and msg_type in id_contained_types: user_id, msg = row[4].split(':\n', 1) # TODO: get user_id in non id_contained_types if not not_self: user_id = self.user_id record = dict( user_id=user_id, msg=msg, type=msg_type, not_self=not_self, created_at=created_at ) records.append(record) return records