This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
forked from xbb1973/USTC-ncov-AutoReport
-
Notifications
You must be signed in to change notification settings - Fork 92
/
report.py
executable file
·123 lines (115 loc) · 4.77 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
# encoding=utf8
import requests
import json
import time
import datetime
import pytz
import re
import sys
import argparse
from bs4 import BeautifulSoup
class Report(object):
def __init__(self, stuid, password, data_path, jinji):
self.stuid = stuid
self.password = password
self.data_path = data_path
self.jinji = jinji
def report(self):
loginsuccess = False
retrycount = 5
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, "r+") as f:
data = f.read()
data = json.loads(data)
jinji = self.jinji
jinji = json.loads(jinji)
data = {**data, **jinji}
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)
data = session.get("https://weixine.ustc.edu.cn/2020").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
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=https%3A%2F%2Fweixine.ustc.edu.cn%2F2020%2Fcaslogin"
session = requests.Session()
session.cookies.clear()
response = session.get(url)
CAS_LT = BeautifulSoup(response.text, 'lxml').find(attrs={'id': 'CAS_LT'}).get('value')
data = {
'model': 'uplogin.jsp',
'CAS_LT': CAS_LT,
'service': 'https://weixine.ustc.edu.cn/2020/caslogin',
'warn': '',
'showCode': '',
'username': self.stuid,
'password': str(self.password),
'button': '',
}
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)
parser.add_argument('jinji', help='紧急联系人', type=str)
args = parser.parse_args()
autorepoter = Report(stuid=args.stuid, password=args.password, data_path=args.data_path, jinji=args.jinji)
count = 5
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)