疫情期间自动上报+截图+发群+邮箱提醒程序
from selenium.webdriver import Chrome
from time import sleep,strftime
from os import path,mkdir
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from smtplib import SMTP_SSL
from email.header import Header
from email.mime.text import MIMEText
import win32gui
import win32con
import win32api
import win32clipboard as w
from PIL import Image
from io import BytesIO
from ctypes import *
def sendImg(to_who):
qq = win32gui.FindWindow(None, to_who)
win32api.PostMessage(qq,win32con.WM_PASTE,0, 0)
sleep(0.5)
win32gui.SendMessage(qq, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
win32gui.SendMessage(qq, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
def setImg(imgpath):
im = Image.open(imgpath)
im.save('a.bmp')
aString = windll.user32.LoadImageW(0, "a.bmp", win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE)
if aString != 0:
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_BITMAP, aString)
w.CloseClipboard()
if __name__=='__main__':
adict = {'http://XXX/h5/static/cksb.svg':True}
option = Options()
option.add_argument('--headless')
option.add_argument('log-level=3')
chrome = Chrome(options=option)
chrome.get('学校疫情上报页面')
try:
with open('./set.ini','r') as f:
uname,password,sender,pwd,receiver = list(map(lambda i:i.replace('\n',''),f.readlines()))
except:
print('本地配置文件读取失败!')
uname,password,sender,pwd,receiver=None,None,None,None,None
if uname and password and sender and pwd and receiver:
pass
else:
while True:
with open('./set.ini','w',encoding='utf-8') as f:
content = [
input('请输入学号:').strip()+'\n',
input('请输入密码:').strip()+'\n',
input('请输入发信人邮箱账号:').strip()+'\n',
input('请输入发信人邮箱密码:').strip()+'\n',
input('请输入收信人邮箱地址:').strip()+'\n'
]
boolean = input('确认输入:').strip()
if boolean.upper() == 'N':
continue
f.writelines(content)
uname,password,sender,pwd,receiver=list(map(lambda i:i.replace('\n',''),content))
break
def send_mail(title,content,sender=sender,pwd=pwd,receiver=receiver):
msg = MIMEText(content,'plain','utf-8')
msg['Subject'] = Header(title,'utf-8')
msg['From'] = sender
msg['To'] = receiver
smtp = SMTP_SSL('smtp.qq.com')
smtp.set_debuglevel(0)
smtp.ehlo('smtp.qq.com')
smtp.login(sender,pwd)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
chrome.find_element_by_xpath(' //uni-view[1]/uni-input/div/input').send_keys(uname)
chrome.find_element_by_xpath(' //uni-view[2]/uni-input/div/input').send_keys(password)
chrome.find_element_by_xpath('//uni-button[text()="下一步"]').click()
WebDriverWait(chrome, 30).until(EC.presence_of_element_located((By.XPATH, '//uni-button[text()="完成"]'))).click()
element = WebDriverWait(chrome, 30).until(EC.presence_of_element_located((By.XPATH, "//uni-image[@style]/img")))
img = element.get_attribute('src')
if not adict.get(img,False) :
boolean = False
elif '235000'<strftime('%H%M%S')<='235959' or '000000'<=strftime('%H%M%S')<'001000':
boolean = False
else:
boolean = True
if boolean:
element.click()
if not path.exists('./上报截图'):
mkdir('./上报截图')
sleep(3)
file_path = f'./上报截图/{strftime("%Y_%m_%d_%H")}.png'
chrome.save_screenshot(file_path)
sleep(1)
to_who='群聊名称'
setImg(file_path)
sendImg(to_who)
try:send_mail('每日疫情上报','截图成功')
except:pass
chrome.quit()
else:
if adict.get(img,False):
while True:
img = WebDriverWait(chrome, 30).until(EC.presence_of_element_located((By.XPATH, "//uni-image[@style]/img"))).get_attribute('src')
if not adict.get(img,False):
break
if not ('235000'<strftime('%H%M%S')<='235959' or '000000'<=strftime('%H%M%S')<'001000'):
raise Exception('Exceeding the specified time')
chrome.refresh()
sleep(15)
msg='无'
try:
element.click()
WebDriverWait(chrome, 30).until(EC.presence_of_element_located((By.XPATH, '//uni-view[text()="今日"]'))).click()
WebDriverWait(chrome, 30).until(EC.presence_of_element_located((By.XPATH, '//uni-button[text()="选择位置"]'))).click()
sleep(1)
chrome.find_element_by_xpath('//uni-view[text()="河北省"]').click()
sleep(0.5)
chrome.find_element_by_xpath('//uni-view[text()="内蒙古自治区"]').click()
sleep(0.5)
chrome.find_element_by_xpath('//uni-view[text()="吉林省"]').click()
sleep(0.5)
chrome.find_element_by_xpath('//uni-view[text()="上海市"]').click()
sleep(0.5)
chrome.find_element_by_xpath('//uni-view[text()="XXX"]').click()
sleep(0.5)
chrome.find_element_by_xpath('//uni-view[text()="XXX"]').click()
sleep(0.5)
chrome.find_element_by_xpath('//uni-view[text()="XXX"]').click()
sleep(1)
ele_list = ['//span[text()="确定"]','//div[text()="确定"]','//uni-view[text()="确认"]']
for p in ele_list:
try:
element = chrome.find_element_by_xpath(p)
if element.is_enabled() and element.is_displayed():
element.click()
break
except:pass
chrome.find_element_by_xpath('//uni-view[@class="padding foot"]/uni-button').click()
sleep(1)
for p in ele_list:
try:
element = chrome.find_element_by_xpath(p)
if element.is_enabled() and element.is_displayed():
element.click()
break
except:pass
WebDriverWait(chrome, 30).until(EC.presence_of_element_located((By.XPATH, '//img[@src="/h5/static/cksb.svg"]'))).click()
sleep(3)
msg = '上报'
file_path = f'./上报截图/{strftime("%Y_%m_%d_%H")}.png'
chrome.save_screenshot(file_path)
msg += '+截图'
sleep(2)
to_who='群聊名称'
setImg(file_path)
sendImg(to_who)
msg += '+发群'
try:send_mail('每日疫情上报',f'{msg}成功!')
except:pass
except Exception as e:
try:send_mail('每日疫情上报',f'上报失败!\n完成步骤:{msg};\n错误:{e}')
except:pass
chrome.quit()