Python源码
import os
import threading
import time
import base64
from axhniu.icon import ico
from axhniu import xhtext
from pywinauto.keyboard import send_keys
import tkinter as tk
# 新建文件夹,初始化
def new_folder():
os.makedirs('股票预警', exist_ok=True)
config =r"""微信收件人=我的Python教程
微信路径=D:\Program Files (x86)\Tencent\WeChat\WeChat.exe
预警文件路径=C:\Users\Ms-xiao\Desktop\预警.txt
gbk 或 utf-8编码=gbk
"""
#判断文件是否存在
if not os.path.exists('股票预警/配置.txt'):
xhtext.write_txt('股票预警/配置.txt', config,'utf-8')
if not os.path.exists('股票预警/预警.txt'):
xhtext.write_txt('股票预警/预警.txt', '生成一个初始的txt888', 'gbk')
label.config(text = "初始化文件已生成,请先配置再使用!!")
time.sleep(10)
label.config(text = '微信号:ugouku')
def new_folder_thread():
thread1 = threading.Thread(target=new_folder)
# 设置守护线程,这样在主线程退出时会自动结束这个线程
thread1.daemon = True
# 启动线程
thread1.start()
#模拟微信发送消息给好友/群
def simulate_click(message, addressee):
# 1.打开微信,设置微信的绝对路径
#wx_path = r'D:\Program Files (x86)\Tencent\WeChat\WeChat.exe'
wx_path= xhtext.read_config(r'股票预警\配置.txt', 1, '=')
os.startfile(wx_path)
time.sleep(2)
# 2.进入微信,模拟按键Ctrl+F
send_keys('^f')
send_keys(addressee)
#print(addressee)
time.sleep(1)
send_keys('{ENTER}') # 回车键必须全部大小
time.sleep(1)
# 3.输入聊天内容
# 处理消息格式+空格+换行
msg_lists = message.replace(' ', '{SPACE}{SPACE}').split('\n')
msgs = ''
for msg in msg_lists:
msgs += msg + '^{ENTER}'
# print(msgs)
send_keys(msgs)
# 回车发送消息
send_keys('{ENTER}')
time.sleep(1)
#exit() # 退出程序,不退出就死循环轰炸了
def work():
addressee = xhtext.read_config(r'股票预警\配置.txt', 0, '=')
# print(addressee)
label.config(text="正在开启预警...")
time.sleep(1)
label.config(text='微信号:ugouku')
local_time = time.localtime()
hour = int(time.strftime("%H", local_time))
# 处于开盘时间内
while hour > 9 and hour <= 15:
warning_path = xhtext.read_config(r'股票预警\配置.txt', 2, '=')
# print(code_path)
encod = xhtext.read_config(r'股票预警\配置.txt', 3, '=')
f = open(warning_path, 'r', encoding = encod)
read_msg = f.read()
# print(read_msg)
#读股票预警里面的预警文件
f1 = open(r'股票预警\预警.txt', 'r', encoding = encod)
write_msg = f1.read()
f1.close()
# print(write_msg)
'''
两个预警文件内容不等于的时候,发送消息
消息发送完成了,就重新写一个
写完后,再循环判断
'''
if read_msg != write_msg:
simulate_click(read_msg, addressee)
xhtext.write_txt('股票预警\预警.txt', read_msg, encod)
#print(read_msg != write_msg)
def work_thread():
thread = threading.Thread(target=work)
# 设置守护线程,这样在主线程退出时会自动结束这个线程
thread.daemon = True
# 启动线程
thread.start()
if __name__ == '__main__':
root = tk.Tk()
set_font = ("楷体", 14)
root.title('股票预警信号,发送给微信群+好友1.0')
root.geometry('400x150+350+260')
root.resizable(0,0)
# 加载ico图标
xhnico = open("xhnico.ico", "wb+")
xhnico.write(base64.b64decode(ico))
xhnico.close()
root.iconbitmap('xhnico.ico')
os.remove("xhnico.ico")
button1 = tk.Button(root, text = '初始化配置', width=18, height=1,
font = set_font,
command = new_folder_thread)
button1.pack(pady=10)
button2 = tk.Button(root, text='开启预警信号', width=18, height=1,
font=set_font,
command = work_thread)
button2.pack(pady=10)
label = tk.Label(root, text='微信号:ugouku', width=80,
height=1,font=set_font)
label.pack(pady=10)
root.mainloop()
|