-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils.py
112 lines (95 loc) · 3.6 KB
/
utils.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
#coding=UTF-8
import requests
import re
import json
from requests.models import PreparedRequest
"""
Request 的异常处理封装
"""
class Request:
def __init__(self):
requests.adapters.DEFAULT_RETRIES = 5
pass
def Post(self, url, cookies = None, data = None, params = None, allow_redirects = True, timeout = 20, ErrMsg = None):
try:
req = requests.post(
url=url,
headers=ReadJson('./config/network.json')['Headers'],
cookies=cookies,
data=data,
params=params,
allow_redirects=allow_redirects,
timeout=timeout)
req.raise_for_status()
except requests.exceptions.RequestException as e:
if ErrMsg:
print(ErrMsg)
raise SystemExit(e)
return req
def Get(self, url, cookies = None, params = None, allow_redirects = True, timeout = 20, ErrMsg = None):
try:
req = requests.get(
url=url,
headers=ReadJson('./config/network.json')['Headers'],
cookies=cookies,
params=params,
allow_redirects=allow_redirects,
timeout=timeout)
req.raise_for_status()
except requests.exceptions.RequestException as e:
if ErrMsg:
print(ErrMsg)
raise SystemExit(e)
return req
def AddParam(url, params):
req = PreparedRequest()
req.prepare_url(url, params)
return req.url
def ReadJson(filePath):
with open(filePath, "r") as f:
return json.load(f)
def ReadNetWorkJson():
json_data = ReadJson('./config/network.json')
return (json_data['Headers'], json_data['Urls'])
def ReadAccountJson():
json_data = ReadJson('./config/account.json')
username, password, secondMajor = (json_data['UserName'], json_data['PassWord'], json_data['SecondMajor'])
if(username == '' or password == ''):
raise Exception('请在 data/account.json 中输入用户名和密码')
return (username, password, secondMajor)
def ReadLessonJson():
json_data = ReadJson('./config/lesson.json')
lessonList = json_data['LessonID']
if(len(lessonList) == 0):
raise Exception('请在 data/lesson.json 中输入想要选择的课程ID')
return lessonList
def ServiceGetter(service_field):
json_data = ReadJson('./config/service.json')
service_res = json_data[service_field]
return service_res
def PayloadGetter(key):
json_data = ReadJson('./config/payload.json')
return json_data[key]
def MergeCookieJar(cookiejar_list):
cookie_dict = {}
for cookiejar in cookiejar_list:
tmp_dict = requests.utils.dict_from_cookiejar(cookiejar)
cookie_dict.update(tmp_dict)
return requests.utils.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True)
def findClassList(raw_str, lessonId):
res = re.findall(r"\{id:(\d+),no:'(.*?)',name:'(.*?)'", raw_str)
for class_tuple in res:
if lessonId == class_tuple[1]:
return class_tuple
raise Exception("未搜索到课程结果,请检查LessonId")
def isCourseAvailable(raw_str, lessonNo):
res = re.findall(r"'(\d+?)':{sc:(\d+?),lc:(\d+?)}", raw_str)
for class_tuple in res:
if lessonNo == class_tuple[0]:
return int(class_tuple[1]) < int(class_tuple[2])
if __name__ == '__main__':
import time
url = "https://xk.fudan.edu.cn/xk/stdElectCourse.action"
params = {'_':int(time.time())}
res = AddParam(url, params)
import pdb; pdb.set_trace()