|
| 1 | +# @Time : 2020/4/4 17:13 |
| 2 | +# @Author : Libuda |
| 3 | +# @FileName: tt.py |
| 4 | +# @Software: PyCharm |
| 5 | +import win32gui |
| 6 | +import win32api |
| 7 | +import win32con |
| 8 | +import win32clipboard as w |
| 9 | +import win32clipboard |
| 10 | +import time |
| 11 | +import ctypes |
| 12 | +import pyautogui |
| 13 | + |
| 14 | +from qq加群.spider import spider |
| 15 | + |
| 16 | +# 安全限制 |
| 17 | +pyautogui.FAILSAFE = False |
| 18 | + |
| 19 | +# qq_or_wx = "TXGuiFoundation" qq |
| 20 | +# qq_or_wx = "ChatWnd" #微信 |
| 21 | +qq_or_wx = "WwStandaloneConversationWnd" # 企业微信 |
| 22 | +sleep_time = 60 # 休眠时间 |
| 23 | +winname = ["阿尔萨斯", "Q", "不搭*"] # 需要发送的 |
| 24 | + |
| 25 | +add_txt = "\n ❤ 更多咨询请联系客服微信:876134889" |
| 26 | + |
| 27 | +totol_dic = set() # 去重 |
| 28 | + |
| 29 | + |
| 30 | +def logger(msg): |
| 31 | + """ |
| 32 | + 日志信息 |
| 33 | +
|
| 34 | + """ |
| 35 | + now = time.ctime() |
| 36 | + print("[%s] %s" % (now, msg)) |
| 37 | + |
| 38 | + |
| 39 | +def testss(): |
| 40 | + # 获取窗口大小 |
| 41 | + hwnd = win32gui.FindWindow(qq_or_wx, winname) |
| 42 | + win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 500, 500, win32con.SWP_SHOWWINDOW) |
| 43 | + win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL) |
| 44 | + |
| 45 | + if win32gui.IsIconic(hwnd): |
| 46 | + logger(">>>窗口已经最小化了") |
| 47 | + win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL) |
| 48 | + time.sleep(0.1) |
| 49 | + logger(">>>开始虚拟按键操作") |
| 50 | + |
| 51 | + logger(">>>开始虚拟按键操作") |
| 52 | + win32gui.SetForegroundWindow(hwnd) |
| 53 | + # 设置剪贴板 |
| 54 | + win32clipboard.OpenClipboard() |
| 55 | + win32clipboard.EmptyClipboard() |
| 56 | + win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, "123") |
| 57 | + win32clipboard.CloseClipboard() |
| 58 | + time.sleep(0.1) |
| 59 | + # 填充消息 |
| 60 | + win32gui.PostMessage(hwnd, win32con.WM_CHAR, 22, 2080193) |
| 61 | + win32gui.PostMessage(hwnd, win32con.WM_PASTE, 0, 0) |
| 62 | + time.sleep(0.1) |
| 63 | + # 回车发送消息 |
| 64 | + win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) |
| 65 | + win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0) |
| 66 | + time.sleep(0.1) |
| 67 | + # 清空剪贴板 |
| 68 | + win32clipboard.OpenClipboard() |
| 69 | + win32clipboard.EmptyClipboard() |
| 70 | + win32clipboard.CloseClipboard() |
| 71 | + time.sleep(0.5) |
| 72 | + |
| 73 | + |
| 74 | +def get_window_rect(hwnd): |
| 75 | + try: |
| 76 | + f = ctypes.windll.dwmapi.DwmGetWindowAttribute |
| 77 | + except WindowsError: |
| 78 | + f = None |
| 79 | + if f: |
| 80 | + rect = ctypes.wintypes.RECT() |
| 81 | + DWMWA_EXTENDED_FRAME_BOUNDS = 9 |
| 82 | + f(ctypes.wintypes.HWND(hwnd), |
| 83 | + ctypes.wintypes.DWORD(DWMWA_EXTENDED_FRAME_BOUNDS), |
| 84 | + ctypes.byref(rect), |
| 85 | + ctypes.sizeof(rect) |
| 86 | + ) |
| 87 | + return rect.left, rect.top, rect.right, rect.bottom |
| 88 | + |
| 89 | + |
| 90 | +def sendMsgToWX(msg, winname): |
| 91 | + try: |
| 92 | + hwnd = win32gui.FindWindow(qq_or_wx, winname) |
| 93 | + react = get_window_rect(hwnd) |
| 94 | + # win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL) |
| 95 | + try: |
| 96 | + win32gui.SetForegroundWindow(hwnd) |
| 97 | + except Exception as e: |
| 98 | + logger("请检查聊天窗口是否被覆盖") |
| 99 | + return False |
| 100 | + |
| 101 | + center_x = react[0] + int((react[2] - react[0]) / 2) |
| 102 | + center_y = react[3] - int((react[3] - react[1]) / 5) |
| 103 | + |
| 104 | + pyautogui.moveTo(center_x, center_y) |
| 105 | + |
| 106 | + if center_x < 0 or center_y < 0: |
| 107 | + raise Exception("该聊天窗口已被后台 请将聊天框置前并且那聊天窗口点击下光标") |
| 108 | + # print(center_x,center_y) |
| 109 | + # 将微信放在前台 |
| 110 | + |
| 111 | + # 将鼠标移到(750, 700) |
| 112 | + pyautogui.moveTo(center_x, center_y) |
| 113 | + win32api.SetCursorPos((center_x, center_y)) |
| 114 | + # 单击左键获取焦点 |
| 115 | + # win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, center_x, center_y, 0, 0) |
| 116 | + # win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, center_x, center_y, 0, 0) |
| 117 | + |
| 118 | + # 将内容写入到粘贴板 |
| 119 | + w.OpenClipboard() |
| 120 | + w.EmptyClipboard() |
| 121 | + w.SetClipboardData(win32con.CF_TEXT, msg.encode(encoding='gbk')) |
| 122 | + w.CloseClipboard() |
| 123 | + |
| 124 | + pyautogui.hotkey('ctrl', 'v') |
| 125 | + pyautogui.keyDown("enter") |
| 126 | + return True |
| 127 | + except Exception as e: |
| 128 | + logger("请确保该聊天窗口存在,异常信息:{}".format(e)) |
| 129 | + return False |
| 130 | + |
| 131 | + |
| 132 | +def main(winname): |
| 133 | + # totol_dic = set() |
| 134 | + # 接收内容 |
| 135 | + l, res = spider() |
| 136 | + res = res |
| 137 | + for one in res: |
| 138 | + totol_dic.add(one) |
| 139 | + |
| 140 | + while 1: |
| 141 | + logger("检测新闻中") |
| 142 | + try: |
| 143 | + new_l, new_res = spider() |
| 144 | + if new_res: |
| 145 | + for one in new_res: |
| 146 | + if one not in totol_dic: |
| 147 | + one = time.strftime("%H:%M", time.localtime(time.time())) + " " + one + add_txt |
| 148 | + for wn in winname: |
| 149 | + if sendMsgToWX(one, wn): |
| 150 | + logger("发送成功:{}".format(one)) |
| 151 | + totol_dic.add(one) |
| 152 | + else: |
| 153 | + logger("发送失败:{},下次将会重新发送".format(one)) |
| 154 | + else: |
| 155 | + break |
| 156 | + time.sleep(sleep_time) |
| 157 | + except Exception as e: |
| 158 | + logger("运行错误.{}".format(e)) |
| 159 | + |
| 160 | + |
| 161 | +def test(winname): |
| 162 | + totol_dic = set() |
| 163 | + # 接收内容 |
| 164 | + l, res = spider() |
| 165 | + res = res |
| 166 | + for one in res: |
| 167 | + one = one + add_txt |
| 168 | + if sendMsgToWX(one, winname): |
| 169 | + logger("发送成功:{}".format(one)) |
| 170 | + time.sleep(2) |
| 171 | + else: |
| 172 | + loop = True |
| 173 | + while loop: |
| 174 | + if sendMsgToWX(one, winname): |
| 175 | + loop = False |
| 176 | + logger("发送失败:{},下次将会重新发送".format(one)) |
| 177 | + time.sleep(5) |
| 178 | + |
| 179 | + time.sleep(3) |
| 180 | + |
| 181 | + |
| 182 | +if __name__ == '__main__': |
| 183 | + # test(winname) #测试用 |
| 184 | + # test(winname2) #测试用 |
| 185 | + logger("请将所有需要发送消息的窗口单独拖出来....") |
| 186 | + main(winname) # 实际用 |
| 187 | + |
| 188 | + # 测试多个窗口 |
| 189 | + # for i in range(10): |
| 190 | + # for one in winname: |
| 191 | + # sendMsgToWX("测试"+str(i)+"", one) |
0 commit comments