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