Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7d50bca

Browse files
committed
定时+ 日志
1 parent 367c1fb commit 7d50bca

File tree

1 file changed

+147
-40
lines changed

1 file changed

+147
-40
lines changed

qq加群/新浪企业微信发消息.py

Lines changed: 147 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,86 @@
1010
import time
1111
import ctypes
1212
import pyautogui
13+
from datetime import datetime
14+
from datetime import time as dtime
15+
import smtplib # 发送邮件 连接邮件服务器
16+
from email.mime.text import MIMEText # 构建邮件格式
17+
from fake_useragent import UserAgent
1318

1419
from qq加群.sina_spider import sina_spider
1520

1621
# 安全限制
1722
pyautogui.FAILSAFE = False
1823

1924
# qq_or_wx = "TXGuiFoundation" qq
20-
# qq_or_wx = "ChatWnd" #微信
21-
qq_or_wx = "WwStandaloneConversationWnd" # 企业微信
22-
sleep_time = 300
25+
qq_or_wx = "ChatWnd" # 微信
26+
# qq_or_wx = "WwStandaloneConversationWnd" # 企业微信
27+
sleep_time = 120
2328
send_message_count = 10 # 每隔多长时间发送一次联系人微信
2429
# 休眠时间
25-
winname = ["阿尔萨斯"] # 需要发送的
26-
wx_number = {"测试群": [1, "李不搭", "15735656005"],
30+
winname = ["Q"] # 需要发送的
31+
wx_number = {"Q": [1, "李不搭", "15735656005"],
2732
"期货快讯1088群": [1088, "小祝", "876134889"],
2833
"快讯3088群@孺子牛": [3088, "孺子牛", "13699679997"],
2934
} # 不同群对应发送的微信号
3035
add_txt = "\n \n 各位朋友好,欢迎来到实时期货快讯{}群,我是{},我的微信号是:{},欢迎大家一起交流。"
31-
36+
user_list = ['1364826576@qq.com', "1410000000@qq.com"] # 给谁发邮件
37+
log_path = "log.txt" #日志信息
3238
totol_dic = set() # 去重
3339

3440
l, res = sina_spider()
3541

36-
for one in res:
42+
for one in res[1:]:
3743
totol_dic.add(one)
3844

3945

46+
class SendEmail:
47+
def __init__(self):
48+
# 发件人
49+
self.send_user = "李晋军" + "<1364826576@qq.com>"
50+
# 登录名
51+
self.login_user = '1364826576@qq.com'
52+
# 这里要注意 不是qq密码 而是在邮箱里设置的发送邮箱的授权码
53+
self.password = 'btfixrcdeguejfja'
54+
# 发送邮件的服务器地址 qq为smtp.qq.com 163邮箱为smtp.163.com
55+
self.email_host = 'smtp.qq.com'
56+
57+
def send_email(self, userlist, subject, content):
58+
message = MIMEText(content, _subtype='plain', _charset='utf-8')
59+
message['Subject'] = subject
60+
message['From'] = self.send_user
61+
message['To'] = ';'.join(userlist) # 收件人列表以分号隔开
62+
# 实例化邮件发送服务器
63+
server = smtplib.SMTP()
64+
# 连接qq邮箱服务器
65+
server.connect(self.email_host)
66+
# 登录服务器
67+
server.login(self.login_user, self.password)
68+
# 发送邮件 注意此处消息的格式应该用as_string()函数
69+
server.sendmail(self.send_user, userlist, message.as_string())
70+
# 关闭邮箱
71+
server.close()
72+
73+
def send_test(self, userlist, passNumber, failNumber):
74+
"""
75+
发送测试结果
76+
:param userlist:
77+
:param passNumber:
78+
:param failNumber:
79+
:return:
80+
"""
81+
totalNumber = passNumber + failNumber
82+
83+
# %.2f表示保留小数点后两位小数 %%表示百分之百
84+
passPercentage = "%.2f%%" % (passNumber / totalNumber * 100)
85+
failPercentae = "%.2f%%" % (failNumber / totalNumber * 100)
86+
87+
sub = "测试结果报告"
88+
content = " 测试用例总数\t%s个\n通过个数\t%s个\n失败个数\t%s个\n通过率\t%s\n失败率\t%s" % (
89+
totalNumber, passNumber, failNumber, passPercentage, failPercentae)
90+
self.send_email(userlist, sub, content)
91+
return True
92+
4093
def logger(msg):
4194
"""
4295
日志信息
@@ -140,45 +193,99 @@ def sendMsgToWX(msg, winname):
140193

141194

142195
def main(winname):
196+
send_em = SendEmail()
143197
count = 0
144198
global totol_dic
145199
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)
200+
current_time = datetime.now().time()
201+
202+
DAY_START = dtime(8, 0)
203+
DAY_END = dtime(12, 0)
204+
205+
NIGHT_START = dtime(13, 0)
206+
NIGHT_END = dtime(20, 0)
207+
208+
if DAY_START <= current_time <= DAY_END or (NIGHT_START <= current_time <= NIGHT_END):
209+
logger("检测新闻中")
210+
try:
211+
l, new_res = sina_spider()
212+
213+
if new_res:
214+
logger(new_res)
215+
for one in new_res:
216+
if one not in totol_dic:
217+
count += 1
218+
if count % send_message_count == 0:
219+
tem = time.strftime("%H:%M", time.localtime(time.time())) + " " + one + add_txt
164220
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))
221+
tem = time.strftime("%H:%M", time.localtime(time.time())) + " " + one
222+
for wn in winname:
223+
s = tem
224+
s = s.format(*wx_number[wn])
225+
if sendMsgToWX(s, wn):
226+
logger("发送成功:{}".format(s))
227+
totol_dic.add(one)
228+
with open(log_path, 'a') as f:
229+
now = time.ctime()
230+
content = "[%s] %s" % (now, s)
231+
f.write(content)
232+
f.write("\n")
233+
send_em.send_email(user_list, "新浪信号发送成功通知", "信息为:{}".format(s))
234+
else:
235+
flag = 3
236+
while flag > 0:
237+
if sendMsgToWX(s, wn):
238+
flag = 0
239+
logger("发送成功:{}".format(s))
240+
totol_dic.add(one)
241+
with open(log_path, 'a') as f:
242+
now = time.ctime()
243+
content = "[%s] %s" % (now, s)
244+
f.write(content)
245+
f.write("\n")
246+
send_em.send_email(user_list, "新浪信号发送成功通知", "信息为:{}".format(s))
247+
else:
248+
flag -= 1
249+
with open(log_path, 'a') as f:
250+
now = time.ctime()
251+
content = "[%s] %s" % (now, s + "发送失败!")
252+
f.write(content)
253+
f.write("\n")
254+
# else:
255+
# break
256+
257+
else:
258+
logger("可能被反爬导致无数据:{}".format(new_res))
259+
260+
except Exception as e:
261+
logger("运行错误.{}".format(e))
262+
with open(log_path, 'a') as f:
263+
now = time.ctime()
264+
content = "[%s] %s" % (now, e)
265+
f.write(content)
266+
f.write("\n")
267+
send_em.send_email(user_list, "新浪发微信程序异常", "异常信息为:{}".format(e))
268+
269+
else:
270+
logger("不在时间段")
173271

272+
time.sleep(sleep_time)
174273

175274
if __name__ == '__main__':
176-
# test(winname) #测试用
177-
# test(winname2) #测试用
275+
178276
logger("请将所有需要发送消息的窗口单独拖出来....")
179-
main(winname) # 实际用
180277

181-
# 测试多个窗口
182-
# for i in range(10):
183-
# for one in winname:
184-
# sendMsgToWX("测试"+str(i)+"", one)
278+
# 断网或者其他未知错误捕获 写入日志 发送邮件
279+
while 1:
280+
try:
281+
main(winname) # 实际用
282+
except Exception as e:
283+
logger("运行错误.{}".format(e))
284+
with open(log_path, 'a') as f:
285+
now = time.ctime()
286+
content = "[%s] %s" % (now, e)
287+
f.write(content)
288+
f.write("\n")
289+
send_em = SendEmail()
290+
send_em.send_email(user_list, "新浪发微信程序异常", "异常信息为:{}".format(e))
291+
time.sleep(60)

0 commit comments

Comments
 (0)