Exemplo n.º 1
0
    def OnSend(self, event):
        try:
            self.m_send.Disable()
            if not hyhr_key.Hyhr_key.IsKeyValid(hyhr_key.KEY.Key):
                logger.error(hyhr_def.HYHR_KEY_ERROR)
                wx.MessageBox(hyhr_def.HYHR_KEY_ERROR)
                return
            else:
                logger.info(hyhr_def.HYHR_KEY_VALID)
            #check key exists
            # if not os.path.isfile(key):
            # 	wx.MessageBox('认证码不存在,请先到菜单文件--认证中输入认证码')
            # 	logger.error('认证码不存在')
            # 	return
            # else:
            # 	with open(key, 'r', encoding='utf-8') as k:
            # 		data = k.read()
            # 		key_date = datetime.datetime.strptime(hyhr_encrypt.Decrypt(data.encode()),hyhr_def.HYHR_KEY_FORMAT)
            # 		if hyhr_encrypt.Global_Cur_Date:
            # 			cur_date = datetime.datetime.strptime(hyhr_encrypt.Global_Cur_Date,hyhr_def.HYHR_KEY_FORMAT)
            # 		else:
            # 			cur_date = datetime.datetime.strptime(hyhr_encrypt.GetCurDate(),hyhr_def.HYHR_KEY_FORMAT)
            # 		if cur_date > key_date:
            # 			wx.MessageBox(hyhr_def.HYHR_KEY_EXPIRED)
            # 			logger.error(hyhr_def.HYHR_KEY_EXPIRED)
            # 			return
            # 		else:
            # 			logger.info('认证码有效!')
            self.msg = self.m_richText3.GetValue()
            if len(self.names) == 0 or (len(self.msg) == 0
                                        and len(self.file) == 0):
                wx.MessageBox(hyhr_def.HYHR_NAME_MSG_FILE_EMPTY)
                logger.error(hyhr_def.HYHR_NAME_MSG_FILE_EMPTY)
                return

            if not os.path.isfile(wechat):
                wx.MessageBox(hyhr_def.HYHR_WECHAT_NOT_INSTALLED)
                logger.error(hyhr_def.HYHR_WECHAT_NOT_INSTALLED)
                return
            app = Application(backend='uia').start(wechat)
            try:
                dlg = Desktop(backend='uia').window(
                    class_name=wechat_class_name)
                dlg.set_focus()
            except Exception as ex:
                wx.MessageBox(hyhr_def.HYHR_WECHAT_NOT_STARTED)
                logger.error(f'{hyhr_def.HYHR_WECHAT_NOT_STARTED} {ex}')
                return
            wrapper = dlg.wrapper_object()
            wrapper.click_input(coords=(searchbox_x, searchbox_y))

            counter = 0
            for info in self.names:
                name = None
                data = []
                vals = []
                validData = True
                info = info.strip()
                if not len(info):
                    continue
                vals = re.split(r'\s+', info)
                length = len(vals)
                if length >= 1:
                    name = vals[0]
                else:
                    logger.warning('异常数据,忽略.')
                    counter += 1
                    continue

                if len(self.msg):
                    for i in range(1, length):
                        try:
                            fee = float(vals[i])
                            data.append(fee)
                        except Exception as ex:
                            logger.warning('异常数值:{}!暂不发送,请核对后重试!'.format(info))
                            validData = False
                            break

                if not validData:
                    counter += 1
                    continue

                try:
                    wrapper.click_input(coords=(searchbox_x, searchbox_y))
                    wrapper.type_keys(name)
                    time.sleep(1.5)
                    wrapper.click_input(coords=(searchbox_x, searchResult_y))
                    noFoundDlg = Desktop(backend='uia').window(
                        class_name=wechat_notFound_class_name)
                    #time.sleep(0.5)
                    if noFoundDlg.exists(timeout=2):
                        noFoundDlg.close()
                        logger.warning('{} 不存在,请手工确认'.format(name))
                        time.sleep(1)
                        counter += 1
                        continue

                    if len(self.msg):
                        if len(data) == 0:
                            dlg.type_keys(self.msg.replace('{', '').replace(
                                '}', ''),
                                          with_spaces=True)
                        else:
                            if '{}' in self.msg:
                                if self.msg.count('{}') == len(data):
                                    dlg.type_keys(self.msg.format(*data),
                                                  with_spaces=True)
                                else:
                                    logger.warning(
                                        f'{name} 数据数目和消息格式不匹配,忽略该条记录!')
                                    counter += 1
                                    continue
                            else:
                                dlg.type_keys(self.msg, with_spaces=True)
                        dlg.type_keys('%{s}')
                        logger.info('发送消息给{}成功'.format(name))

                    if len(self.file):
                        #try to send file
                        #for f in os.listdir(self.file):
                        wrapper.click_input(coords=(sendfile_x, sendfile_y))
                        #pywinauto.keyboard.send_keys(self.file)
                        pywinauto.keyboard.send_keys(self.file)
                        pywinauto.keyboard.send_keys('{ENTER 1}')
                        dlg.type_keys('%{s}')
                        logger.info('发送文件{}给{}成功'.format(self.file, name))

                    self.m_namesList.SetItem(counter, 1, '✔')
                    self.m_namesList.SetItemTextColour(counter, wx.GREEN)
                    self.m_namesList.itemDataMap[counter] = (name, '✔')
                    counter += 1

                except Exception as ex:
                    #wx.MessageBox('发送错误!{}.'.format(ex))
                    logger.warning(f'{hyhr_def.HYHR_SEND_ERROR}{ex}.')
                    counter += 1
        except Exception as ex:
            wx.MessageBox(f'{hyhr_def.HYHR_SEND_ERROR}{ex}.')
            logger.error(f'{hyhr_def.HYHR_SEND_ERROR} {ex}.')
        finally:
            self.m_send.Enable()
        event.Skip()
Exemplo n.º 2
0
    os.listdir(filesRootPath))
bNeedSendMsg = os.path.isfile(msgFile) and os.stat(msgFile).st_size

assert os.path.isfile(namesFile), '名单不存在'
assert bNeedSendMsg or bNeedSendFile, '信息文件不存或者没有要发送的文件'

#app = Application(backend='uia').start(r'C:\Program Files\WindowsApps\TencentWeChatLimited.forWindows10_2.6.3.0_x86__sdtnhv12zgd7a\WeChatStore\WeChatStore.exe')
app = Application(
    backend='uia').start(r'C:\Program Files (x86)\Tencent\WeChat\WeChat.exe')
try:
    dlg = Desktop(backend='uia').window(class_name='WeChatMainWndForPC')
    dlg.set_focus()
except:
    print('未能发现桌面版微信,退出!')
    sys.exit()
wrapper = dlg.wrapper_object()

#try to locate the search textbox
width = 100
height = 30
retry = 5
while retry > 0:
    wrapper.click_input(coords=(width, height))
    if dlg['3'].exists(timeout=2):
        break
    else:
        retry -= 1
        width += 10
        height += 10

if not dlg['3'].exists(timeout=2):