-
Notifications
You must be signed in to change notification settings - Fork 11
/
login1.1_firefox_for_linux_automail_edition.py
198 lines (172 loc) · 8.89 KB
/
login1.1_firefox_for_linux_automail_edition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# 每次打卡成功后通过邮件服务向指定的邮箱发送打卡成功邮件,避免手工登录查询自动打卡是否成功。
# 目前发件邮箱支持QQ(需开启STMP服务,获取授权码,可参考https://jingyan.baidu.com/article/6079ad0eb14aaa28fe86db5a.html),收件邮箱不限。
#
# 首次输入要输入用户名、密码、发件邮箱、授权码、收件邮箱,保存在logData.txt。
# Ubuntu18.04亲测可用
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys # 键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素
from selenium.webdriver.firefox.options import Options
import time
import random
# smtplib 用于邮件的发信动作
import smtplib
# email 用于构建邮件内容
from email.mime.text import MIMEText
# 用于构建邮件头
from email.header import Header
# 加启动配置 禁用日志log
firefox_opt = Options()
firefox_opt.add_argument( '–no-sandbox' ) # “–no - sandbox”参数是让root权限下跑
firefox_opt.add_argument( '--start-maximized' ) # 最大化
firefox_opt.add_argument( '--incognito' ) # 无痕隐身模式
firefox_opt.add_argument( "disable-cache" ) # 禁用缓存
firefox_opt.add_argument( 'log-level=3' )
firefox_opt.add_argument( 'disable-infobars' )
firefox_opt.add_argument( '--headless' )
url = "https://newids.seu.edu.cn/authserver/login?service=http://ehall.seu.edu.cn/qljfwapp2/sys/lwReportEpidemicSeu/*default/index.do"
dailyDone = False # 今日是否已经打卡
# 创建打卡记录log文件
def writeLog(text):
with open( 'log.txt', 'a' ) as f:
s = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime() ) + ' ' + text
print( s )
f.write( s + '\n' )
f.close()
def enterUserPW():
# 创建账号密码文件,以后都不用重复输入
try:
with open( "loginData.txt", mode='r', encoding='utf-8' ) as f:
# 去掉换行符
user = f.readline().strip()
pw = f.readline().strip()
# 发信方的信息:发信邮箱 授权码
from_addr = f.readline().strip()
authorization_code = f.readline().strip()
# 收信方的信息:收邮箱
to_addr = f.readline().strip()
f.close()
except FileNotFoundError:
print( "Welcome to AUTO DO THE F***ING DAILY JOB, copyright belongs to S.H." )
with open( "loginData.txt", mode='w', encoding='utf-8' ) as f:
user = input( 'Please Enter Your Username: ' )
pw = input( 'Then Please Enter Your Password: ' )
from_addr = input( 'Please Enter Your sendmail address: ' )
authorization_code = input( 'Then Please Enter Your sendmail Authorization code: ' )
to_addr = input( 'Then Please Enter Your receivemail address: ' )
f.write( user + '\n' )
f.write( pw + '\n' )
f.write( from_addr + '\n' )
f.write( authorization_code + '\n' )
f.write( to_addr + '\n' )
f.close()
return user, pw, from_addr, authorization_code, to_addr
def login(user, pw, browser):
browser.get( url )
browser.implicitly_wait( 10 )
# 填写用户名密码
username = browser.find_element_by_id( 'username' )
password = browser.find_element_by_id( 'password' )
username.clear()
password.clear()
username.send_keys( user )
password.send_keys( pw )
# 点击登录
login_button = browser.find_element_by_class_name( 'auth_login_btn' )
login_button.submit()
# 检查是否无text按钮
def check(text, browser):
buttons = browser.find_elements_by_tag_name( 'button' )
for button in buttons:
if button.get_attribute( "textContent" ).find( text ) >= 0:
return True
return False
# 发送打卡邮件(暂发件箱只支持QQ邮箱,收件箱不限)
def automail(from_addr, authorization_code, to_addr, mailText):
# 发信服务器
smtp_server = 'smtp.qq.com'
# 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码
msg = MIMEText( mailText, 'plain', 'utf-8' )
# 邮件头信息
msg['From'] = Header( from_addr )
msg['To'] = Header( to_addr )
msg['Subject'] = Header( 'autoLogin' )
# 开启发信服务,这里使用的是加密传输
server = smtplib.SMTP_SSL( smtp_server )
server.connect( smtp_server, 465 )
# 登录发信邮箱
server.login( from_addr, authorization_code )
# 发送邮件
server.sendmail( from_addr, to_addr, msg.as_string() )
# 关闭服务器
server.quit()
if __name__ == "__main__":
user, pw, from_addr, authorization_code, to_addr = enterUserPW()
localtime = time.localtime( time.time() )
set_minite = localtime.tm_min # 首次登陆的分钟时刻,代表以后每次在此分钟时刻打卡
set_hour = localtime.tm_hour # 首次登陆的时钟时刻,代表以后每次在此时钟时刻打卡
if set_hour > 9:
set_hour = 7 # 如果首次登录超过上午10点,则以后默认在7点钟打卡
first_time = True
while True:
try:
# 登录打卡一次试一试
browser = webdriver.Firefox( executable_path='./geckodriver', options=firefox_opt )
print( "------------------浏览器已启动----------------------" )
login( user, pw, browser )
browser.implicitly_wait( 10 )
time.sleep( 10 )
# 确认是否打卡成功
# 的确无新增按钮
dailyDone = not check( "新增", browser )
if dailyDone is True and check( "退出", browser ) is True: # 今日已完成打卡
sleep_time = (set_hour + 24 - time.localtime( time.time() ).tm_hour) * 3600 + (
set_minite - time.localtime( time.time() ).tm_min) * 60
writeLog(
"下次打卡时间:明天" + str( set_hour ) + ':' + str( set_minite ) + "," + "即" + str( sleep_time ) + 's后' )
browser.quit()
automail( from_addr, authorization_code, to_addr, "今日打卡完毕。\n下次打卡时间:明天" + str( set_hour ) + ':' + str( set_minite ) + "。" )
print( "------------------浏览器已关闭----------------------" )
time.sleep( sleep_time )
elif dailyDone is False: # 今日未完成打卡
# 点击报平安
buttons = browser.find_elements_by_css_selector( 'button' )
for button in buttons:
if button.get_attribute( "textContent" ).find( "新增" ) >= 0:
button.click()
browser.implicitly_wait( 10 )
# 输入温度37°
inputfileds = browser.find_elements_by_tag_name( 'input' )
for i in inputfileds:
if i.get_attribute( "placeholder" ).find( "请输入当天晨检体温" ) >= 0:
i.click()
i.send_keys( str( random.randint( 365, 370 ) / 10.0 ) )
# 确认并提交
buttons = browser.find_elements_by_tag_name( 'button' )
for button in buttons:
if button.get_attribute( "textContent" ).find( "确认并提交" ) >= 0:
button.click()
buttons = browser.find_elements_by_tag_name( 'button' )
button = buttons[-1]
# 提交
if button.get_attribute( "textContent" ).find( "确定" ) >= 0:
button.click()
dailyDone = True # 标记已完成打卡
writeLog( "打卡成功" )
break
break
break
browser.quit()
print( "------------------浏览器已关闭----------------------" )
time.sleep( 10 ) # 昏睡10s 为了防止网络故障未打上卡
else:
browser.quit()
print( "------------------网站出现故障----------------------" )
print( "------------------浏览器已关闭----------------------" )
time.sleep( 300 ) # 昏睡5min 为了防止网络故障未打上卡
except Exception as r:
print( "未知错误 %s" % (r) )
time.sleep( 10 ) # 昏睡10s