forked from xbb1973/USTC-ncov-AutoReport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.py
144 lines (131 loc) · 5.32 KB
/
report.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
# encoding=utf8
import requests
import json
import time
import datetime
import pytz
import re
import sys
import argparse
from bs4 import BeautifulSoup
from aip import AipOcr
# 定义常量
APP_ID = '24565086'
API_KEY = 'tgkntU18fTayWntdivP5jv4G'
SECRET_KEY = '1GfGV1T0f6AblLSlLTs4KON21IFPIKlH'
class Report(object):
def __init__(self, stuid, password, data_path):
self.stuid = stuid
self.password = password
self.data_path = data_path
def report(self):
loginsuccess = False
retrycount = 2
while (not loginsuccess) and retrycount:
session = self.login()
cookies = session.cookies
getform = session.get("https://weixine.ustc.edu.cn/2020")
retrycount = retrycount - 1
if getform.url != "https://weixine.ustc.edu.cn/2020/home":
print("Login Failed! Retrying...")
else:
print("Login Successful!")
loginsuccess = True
if not loginsuccess:
return False
data = getform.text
data = data.encode('ascii','ignore').decode('utf-8','ignore')
soup = BeautifulSoup(data, 'html.parser')
token = soup.find("input", {"name": "_token"})['value']
with open(self.data_path, "rb") as f:
data = f.read()
data = json.loads(data)
data["_token"]=token
headers = {
'authority': 'weixine.ustc.edu.cn',
'origin': 'https://weixine.ustc.edu.cn',
'upgrade-insecure-requests': '1',
'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'referer': 'https://weixine.ustc.edu.cn/2020/home',
'accept-language': 'zh-CN,zh;q=0.9',
'Connection': 'close',
'cookie': "PHPSESSID=" + cookies.get("PHPSESSID") + ";XSRF-TOKEN=" + cookies.get("XSRF-TOKEN") + ";laravel_session="+cookies.get("laravel_session"),
}
url = "https://weixine.ustc.edu.cn/2020/daliy_report"
resp=session.post(url, data=data, headers=headers,allow_redirects=True)
#data = session.get("https://weixine.ustc.edu.cn/2020").text
data = resp.text
soup = BeautifulSoup(data, 'html.parser')
pattern = re.compile("202[0-9]-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")
token = soup.find(
"span", {"style": "position: relative; top: 5px; color: #666;"})
flag = False
alert_p = soup.find("p")
alert_success = alert_p['class']
if("alert-success" in alert_success):
print(alert_p)
flag = True
'''
if pattern.search(token.text) is not None:
date = pattern.search(token.text).group()
print("Latest report: " + date)
date = date + " +0800"
reporttime = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S %z")
timenow = datetime.datetime.now(pytz.timezone('Asia/Shanghai'))
delta = timenow - reporttime
print("{} second(s) before.".format(delta.seconds))
if delta.seconds < 120:
flag = True'''
if flag == False:
print("Report FAILED!")
else:
print("Report SUCCESSFUL!")
return flag
def login(self):
url = "https://passport.ustc.edu.cn/login?service=http%3A%2F%2Fweixine.ustc.edu.cn%2F2020%2Fcaslogin"
session = requests.Session()
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
image1 = session.get('https://passport.ustc.edu.cn/validatecode.jsp?type=login')
image = image1.content
res=client.basicGeneral(image);
for item in res['words_result']:
number = item['words']
getform = session.get(url)
data = getform.text
data = data.encode('ascii','ignore').decode('utf-8','ignore')
soup = BeautifulSoup(data, 'html.parser')
token = soup.find("input", {"name": "CAS_LT"})['value']
data = {
'model': 'uplogin.jsp',
'service': 'https://weixine.ustc.edu.cn/2020/caslogin',
'username': self.stuid,
'password': str(self.password),
'warn': '',
'showCode': '1',
'button': '',
'LT': number,
'CAS_LT': token
}
session.post(url, data=data)
print("login...")
return session
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='URC nCov auto report script.')
parser.add_argument('data_path', help='path to your own data used for post method', type=str)
parser.add_argument('stuid', help='your student number', type=str)
parser.add_argument('password', help='your CAS password', type=str)
args = parser.parse_args()
autorepoter = Report(stuid=args.stuid, password=args.password, data_path=args.data_path)
count = 2
while count != 0:
ret = autorepoter.report()
if ret != False:
break
print("Report Failed, retry...")
count = count - 1
if count != 0:
exit(0)
else:
exit(-1)