def get_chatroom_friends(self, chatroom_id): session_db = self.path + '/%s/session/session.db' % self.user_hash logger.debug('DB path %s' % session_db) group_table = 'SessionAbstract' conn = sqlite3.connect(session_db) # GET group users nickname xml file c = conn.execute('SELECT * FROM %s WHERE UsrName="%s"' % (group_table, chatroom_id)) row = c.fetchone() session_path = row[5] full_session_path = self.path + '/%s%s' % (self.user_hash, session_path) logger.debug('Bin path %s' % full_session_path) f = open(full_session_path, 'r') raw_xml = f.read() pattern = '<RoomData>.*</RoomData>' chatroom_xml = re.search(pattern, raw_xml, re.MULTILINE).group() xml_dict = xmltodict.parse(chatroom_xml) friends = [] for member in xml_dict['RoomData']['Member']: friend = dict( id=member['@UserName'], nickname=member.get('DisplayName'), ) friends.append(friend) return friends
def get_friends(self): chat_db = self.path + '/%s/DB/MM.sqlite' % self.user_hash logger.debug('DB path %s' % chat_db) conn = sqlite3.connect(chat_db) friends = [] for row in conn.execute( 'SELECT f.*,fe.ConStrRes2, fe.ConRemark FROM Friend as f JOIN Friend_Ext as fe USING(UsrName) WHERE `Type` NOT IN %s AND `UsrName` NOT LIKE "gh_%%"' % FriendTypeExlude.__str__()): label_pattern = '<LabelList>(.*)</LabelList>' label_list_str = re.search(label_pattern, row[13], re.MULTILINE).group(1) label_list = label_list_str.split(',') label_list = [int(label_id) for label_id in label_list if label_id] friend = dict( id=row[1], nickname=row[2], gender=row[6], type=row[10], label_ids=label_list, remark=row[14], ) friends.append(friend) return friends
def get_chatrooms(self): chat_db = self.path + '/%s/DB/MM.sqlite' % self.user_hash logger.debug('DB path %s' % chat_db) conn = sqlite3.connect(chat_db) friends = [] for row in conn.execute('SELECT * FROM `Friend` WHERE `UsrName` LIKE "%chatroom"'): friend = dict( id=row[1], nickname=row[2], type=row[10], ) friends.append(friend) return friends
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_chatrooms(self): chat_db = self.path + '/%s/DB/MM.sqlite' % self.user_hash logger.debug('DB path %s' % chat_db) conn = sqlite3.connect(chat_db) friends = [] for row in conn.execute( 'SELECT * FROM `Friend` WHERE `UsrName` LIKE "%chatroom"'): friend = dict( id=row[1], nickname=row[2], type=row[10], ) friends.append(friend) return friends
def get_chatrooms(self): chat_db = self.path + '/%s/DB/WCDB_Contact.sqlite' % self.user_hash logger.debug('DB path %s' % chat_db) conn = sqlite3.connect(chat_db) friends = [] for row in conn.execute( 'SELECT * FROM `Friend` WHERE `userName` LIKE "%chatroom"'): remark_list = self.get_remark_list(row[7]) friend = dict( id=row[0], nickname=remark_list[0], type=row[10], ) friends.append(friend) return friends
def get_friends(self): chat_db = self.path + '/%s/DB/WCDB_Contact.sqlite' % self.user_hash logger.debug('DB path %s' % chat_db) conn = sqlite3.connect(chat_db) friends = [] for row in conn.execute( 'SELECT * FROM Friend WHERE `certificationFlag` = 0 AND `type` NOT IN %s AND `userName` NOT LIKE "gh_%%" AND `userName` NOT LIKE "%%@chatroom"' % FriendTypeExlude.__str__()): # label_pattern = '<LabelList>(.*)</LabelList>' # label_list_str = re.search(label_pattern, row[4], re.MULTILINE).group(1) # label_list = label_list_str.split(',') # label_list = [int(label_id) for label_id in label_list if label_id] # avatar_pattern = '<HeadImgUrl>(.*)</HeadImgUrl>' # avatar_url = re.search(avatar_pattern, row[8], re.MULTILINE).group(1) remark_list = self.get_remark_list(row[8]) # 本字段可能有多种存储格式,所以格式暂时并不能统一 # 0 昵称 # 3 昵称拼音 # 1 备注 # 2 微信号 # 4 备注拼音 # 5 备注首字母大写 # 6 描述 # 7 # Type 的参考资料 # https://www.jianshu.com/p/07a8d87e698b label_list = [ int(i.encode('utf-8')) for i in remark_list[7].split(',') if i ] friend = dict( # id=row[0] if len(remark_list) <= 1 or remark_list[1] == u"" else remark_list[2], id=row[15], nickname=remark_list[0], # gender=row[6], type=row[2], label_ids=label_list, remark=remark_list[1] if len(remark_list) > 1 else "", avatar_url=row[9], ) friends.append(friend) # print row return friends
def get_friends(self): chat_db = self.path + '/%s/DB/MM.sqlite' % self.user_hash logger.debug('DB path %s' % chat_db) conn = sqlite3.connect(chat_db) friends = [] for row in conn.execute('SELECT f.*,fe.ConStrRes2, fe.ConRemark FROM Friend as f JOIN Friend_Ext as fe USING(UsrName) WHERE `Type` NOT IN %s AND `UsrName` NOT LIKE "gh_%%"' % FriendTypeExlude.__str__()): label_pattern = '<LabelList>(.*)</LabelList>' label_list_str = re.search(label_pattern, row[13], re.MULTILINE).group(1) label_list = label_list_str.split(',') label_list = [int(label_id) for label_id in label_list if label_id] friend = dict( id=row[1], nickname=row[2], gender=row[6], type=row[10], label_ids = label_list, remark = row[14], ) friends.append(friend) return friends
def get_chatroom_friends(self, chatroom_id): session_db = self.path + '/%s/session/session.db' % self.user_hash logger.debug('DB path %s' % session_db) group_table = 'SessionAbstract' conn = sqlite3.connect(session_db) # GET group users nickname xml file c = conn.execute('SELECT * FROM %s WHERE UsrName="%s"' % (group_table, chatroom_id)) row = c.fetchone() session_path = "" if row[5] is None else row[5] full_session_path = self.path + '/%s%s' % (self.user_hash, session_path) logger.debug('Bin path %s' % full_session_path) f = open(full_session_path, 'r') raw_xml = f.read() pattern = '<RoomData>.*</RoomData>' chatroom_xml = re.search(pattern, raw_xml, re.MULTILINE).group() xml_dict = xmltodict.parse(chatroom_xml) chat_db = self.path + '/%s/DB/WCDB_Contact.sqlite' % self.user_hash conn2 = sqlite3.connect(chat_db) friends = [] for member in xml_dict['RoomData']['Member']: c2 = conn2.execute('SELECT * FROM `Friend` WHERE userName="******"' % member['@UserName']) row2 = c2.fetchone() remark_list = [None] if row2 is not None: remark_list = self.get_remark_list(row2[7]) friend = dict( id=member['@UserName'], nickname=remark_list[0], ) friends.append(friend) return friends
def get_friends(self): chat_db = self.path + '/%s/DB/WCDB_Contact.sqlite' % self.user_hash logger.debug('DB path %s' % chat_db) conn = sqlite3.connect(chat_db) friends = [] # for row in conn.execute('SELECT f.*,fe.ConStrRes2, fe.ConRemark FROM Friend as f JOIN Friend_Ext as fe USING(UsrName) WHERE `Type` NOT IN %s AND `UsrName` NOT LIKE "gh_%%"' % FriendTypeExlude.__str__()): for row in conn.execute( 'SELECT * FROM Friend WHERE `certificationFlag` = 0 AND `type` NOT IN %s AND `userName` NOT LIKE "gh_%%" AND `userName` NOT LIKE "%%@chatroom"' % FriendTypeExlude.__str__()): # label_pattern = '<LabelList>(.*)</LabelList>' # label_list_str = re.search(label_pattern, row[4], re.MULTILINE).group(1) # label_list = label_list_str.split(',') # label_list = [int(label_id) for label_id in label_list if label_id] # avatar_pattern = '<HeadImgUrl>(.*)</HeadImgUrl>' # avatar_url = re.search(avatar_pattern, row[8], re.MULTILINE).group(1) remark_list = self.get_remark_list(row[7]) if len(remark_list) != 8: continue label_list = [ int(i.encode('utf-8')) for i in remark_list[7].split(',') if i ] friend = dict( id=row[0] if len(remark_list) <= 1 or remark_list[1] == u"" else remark_list[1], # id=row[2], nickname=remark_list[0], gender=row[6], type=row[2], label_ids=label_list, remark=remark_list[2] if len(remark_list) > 2 else u"", avatar_url=row[8], ) friends.append(friend) return friends
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