Exemple #1
0
 def _get_users(self, sqlpath: str):
     """
     扫描联系人后缀的数据,添加task信息后返回
     :param sqlpath:
     :return:
     """
     limitdata = 1000
     offsetdata = 0
     while True:
         sql = '''SELECT 
         *
         FROM users LIMIT ? OFFSET ?'''
         pars = (
             limitdata,
             offsetdata,
         )
         offsetdata += limitdata
         res = self._select_data(sqlpath, sql, pars)
         if len(res) == 0:
             return
         all_contact = CONTACT(self._clientid, self.task, self.task.apptype)
         for el in res:
             try:
                 # 将为空的字段全部剔除
                 # eldict = {k: v for k, v in el.items() if v is not None}
                 eldict = el
                 if eldict.get('id') is None:
                     continue
                 contact_one = CONTACT_ONE(self._userid, eldict.get('id'),
                                           self.task, self.task.apptype)
                 if eldict.get('phone') is not None:
                     contact_one.phone = self._output_format_str(
                         eldict.pop('phone'))
                 nickname = b''
                 if eldict.get('last_name') is not None:
                     nickname += eldict.pop('last_name')
                 if eldict.get('first_name') is not None:
                     nickname += eldict.pop('first_name')
                 if nickname == b'' and eldict.get('username') is not None:
                     nickname += eldict.pop('username')
                 if nickname != b'':
                     contact_one.nickname = self._output_format_str(
                         nickname)
                 if eldict.get('contact') is not None:
                     contact_one.isfriend = eldict.pop('contact')
                 if eldict.get('mutualContact') is not None:
                     contact_one.bothfriend = eldict.pop('mutualContact')
                 if eldict.get('deleted') is not None:
                     contact_one.isdeleted = eldict.pop('deleted')
                 all_contact.append_innerdata(contact_one)
             except Exception:
                 self._logger.error(
                     f"Get profile error,err:{traceback.format_exc()}")
                 continue
         if all_contact.innerdata_len != 0:
             yield all_contact
         if len(res) < limitdata:
             break
Exemple #2
0
    def _read_datas(self, dbpath) -> iter:
        """read datas from db"""
        try:
            # 下载数据成功
            sql = '''SELECT * FROM msg
            '''
            res_lines = self._select_data(dbpath, sql)
            if len(res_lines) == 0:
                return
            contacts_all = {}
            logs_all = ICHATLOG(self._clientid, self.task, self.task.apptype)
            for line in res_lines:
                if line.get('content') is None and line.get('content') == '':
                    continue
                try:
                    conversionid = line.get('conversation_id').split(':')
                    if len(conversionid) != 0:
                        if self._userid == '':
                            self._userid = conversionid[-1]
                        contact_id = conversionid[-2]
                        contacts_all[contact_id] = 1
                    else:
                        continue
                    messagetype: EResourceType = self._resources_type.get(
                        line['type'])
                    sessionid = conversionid[-2]
                    messageid = line['msg_uuid']
                    senderid = line['sender']

                    cr_time = time.strftime(
                        '%Y-%m-%d %H:%M:%S',
                        time.localtime(line['created_time'] / 1000))
                    log_one = ICHATLOG_ONE(self.task, self.task.apptype,
                                           self._userid, messagetype.value,
                                           sessionid, 1, messageid, senderid,
                                           cr_time)
                    if messagetype == EResourceType.Picture or messagetype == EResourceType.Audio:
                        for res_file in self._get_resources(line, messagetype):
                            log_one.append_resource(res_file)
                            yield res_file
                    try:
                        json_text = json.loads(line['content'])
                    except Exception as err:
                        self._logger.error(
                            "Content data is not json, err:{}".format(err))
                        continue
                    # 文字
                    if json_text.get('text') is not None:
                        log_one.content = json_text.get('text')
                    logs_all.append_innerdata(log_one)
                except Exception:
                    self._logger.error(
                        "Parse one data line from db error: {} {}".format(
                            self.uname_str, traceback.format_exc()))
            if logs_all.innerdata_len > 0:
                yield logs_all
            if self._userid != '':
                p_data = PROFILE(self._clientid, self.task, self.task.apptype,
                                 self._userid)
                p_data.account = self._phone
                p_data.phone = self._phone
                yield p_data
            if len(contacts_all) != 0:
                ct_all = CONTACT(self._clientid, self.task, self.task.apptype)
                for ct in contacts_all:
                    ct_one = CONTACT_ONE(self._userid, ct, self.task,
                                         self.task.apptype)
                    ct_one.isfriend = 1
                    ct_one.isdeleted = 0
                    ct_all.append_innerdata(ct_one)
                yield ct_all
        except Exception:
            self._logger.error("Read datas from db error: {} {}".format(
                self.uname_str, traceback.format_exc()))