-
Notifications
You must be signed in to change notification settings - Fork 262
/
report.py
112 lines (103 loc) · 4.26 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
# 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):
self.stuid = stuid
self.password = password
self.data_path = data_path
def report(self):
loginsuccess = False
retrycount = 5
while (not loginsuccess) and retrycount:
session = self.login()
cookies = session.cookies
getform = session.get("http://weixine.ustc.edu.cn/2020")
retrycount = retrycount - 1
if getform.url != "https://weixine.ustc.edu.cn/2020/home":
print("Login Failed! Retry...")
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)
data["_token"]=token
headers = {
'authority': 'weixine.ustc.edu.cn',
'origin': 'http://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': 'http://weixine.ustc.edu.cn/2020/',
'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 = "http://weixine.ustc.edu.cn/2020/daliy_report"
session.post(url, data=data, headers=headers)
data = session.get("http://weixine.ustc.edu.cn/2020").text
soup = BeautifulSoup(data, 'html.parser')
pattern = re.compile("2020-[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=http%3A%2F%2Fweixine.ustc.edu.cn%2F2020%2Fcaslogin"
data = {
'model': 'uplogin.jsp',
'service': 'http://weixine.ustc.edu.cn/2020/caslogin',
'username': self.stuid,
'password': str(self.password),
}
session = requests.Session()
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 = 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)