admin管理员组

文章数量:1529460

疫情期间自动上报+截图+发群+邮箱提醒程序

#encoding=utf-8
#selenium 主题功能模块
from selenium.webdriver import Chrome
from time import sleep,strftime
from os import path,mkdir
from selenium.webdriver.chrome.options import Options
#selenium 等待元素模块
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)
    #print(aString)
    if aString != 0:  # 由于图片编码问题  图片载入失败的话  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('学校疫情上报页面')


    
    #尝试加载本地set.ini文件
    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('确认输入<Y/N>:').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
        #ssl登录
        smtp = SMTP_SSL('smtp.qq')
        # 1开启调试模式
        smtp.set_debuglevel(0)
        smtp.ehlo('smtp.qq')
        smtp.login(sender,pwd)
        smtp.sendmail(sender,receiver,msg.as_string())
        smtp.quit()




    #使用selenium 寻找界面元素by_xpath
    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':    #如果入口图片为已上报 并且时间 在0:00偏差不超过10min  进入上报页面
        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):
            #每15s重新检查元素
            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()

            #WebDriverWait(chrome, 30).until(EC.presence_of_element_located((By.XPATH, '//uni-text[text()="上报记录"]')))
            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()
    

本文标签: 疫情截图邮箱程序