-
Notifications
You must be signed in to change notification settings - Fork 28
/
export_physics_experiment.py
99 lines (89 loc) · 3.89 KB
/
export_physics_experiment.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
# Copyright (C) 2022 by the XiDian Open Source Community.
#
# This file is part of xidian-scripts.
#
# xidian-scripts is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# xidian-scripts is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with xidian-scripts. If not, see <http://www.gnu.org/licenses/>.
import pkg_resources
import subprocess
import sys
import os
try:
pkg_resources.require(('libxduauth', 'icalendar'))
except (pkg_resources.DistributionNotFound, pkg_resources.VersionConflict):
subprocess.check_call([
sys.executable, '-m', 'pip', 'install', 'libxduauth', 'icalendar'
])
try:
import credentials
USERNAME = credentials.PHYSICS_USERNAME
PASSWORD = credentials.PHYSICS_PASSWORD
except ImportError:
USERNAME, PASSWORD = [os.getenv(i) for i in ('PHYSICS_USER', 'PHYSICS_PASS')]
if not USERNAME or not PASSWORD:
print('请设置环境变量 PHYSICS_USER 和 PHYSICS_PASS')
exit(1)
from icalendar import Calendar, Event
from datetime import datetime, timedelta
import requests
import re
expList = []
def get_experiments():
expList = []
ses = requests.session()
ses.post('http://wlsy.xidian.edu.cn/PhyEws/default.aspx',
data={
'__EVENTTARGET': '',
'__EVENTARGUMENT': '',
'__VIEWSTATE': '/wEPDwUKMTEzNzM0MjM0OWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFD2xvZ2luMSRidG5Mb2dpbutGpJNAAaBhxseXkh1n/woLBppW',
'__VIEWSTATEGENERATOR': 'EE008CD9',
'__EVENTVALIDATION': '/wEWBwLsvJu+AgKckJOGDgKD8YXRCQLJ5dDDBAKVx8n1CQKytMi0AQKcg465CqDdcB40IuBzviNuzXl4xNRdD759',
'login1$StuLoginID': USERNAME,
'login1$StuPassword': PASSWORD,
'login1$UserRole': 'Student',
'login1$btnLogin.x': '0',
'login1$btnLogin.y': '0'
})
html = ses.get('http://wlsy.xidian.edu.cn/PhyEws/student/select.aspx')
pattern = re.compile(
r'<td class="forumRow" height="25"><a class="linkSmallBold"(.*?)target="_new">((?!《物理实验》)(?!下载).*?)([0-9]学时)</a></td>'
r'<td class="forumRow" align="center" height="25"><span>[0-9]{1,2}</span></td>'
r'<td class="forumRow" height="25"><span>星期(.*?)((([01][0-9]|2[0-3]):[0-5][0-9])\-(([01][0-9]|2[0-3]):[0-5][0-9]))</span></td>'
r'<td class="forumRow" height="25"><span>([0-9]{1,2}/[0-9]{1,2}/[0-9]{4})</span></td>'
r'<td class="forumRow" align="center" height="25"><span>([A-F]([0-999]{3,3}))</span></td>')
result = pattern.findall(html.text)
for exp in result:
expName = exp[1]
expStart = exp[4]
expEnd = exp[6]
expDate = exp[8]
expClassroom = exp[9]
expList.append([expName, expStart, expEnd, expDate, expClassroom])
return expList
source = input("请确保当前处于校园网或翼讯网络环境下,回车继续...")
expList = get_experiments()
cal = Calendar()
for exp in expList:
e = Event()
e.add("description", exp[0] + ' @ ' + exp[4])
e.add('summary', exp[0] + ' @ ' + exp[4])
start = datetime.strptime(exp[3] + ' ' + exp[1], '%m/%d/%Y %H:%M')
e.add('dtstart', start)
end = datetime.strptime(exp[3] + ' ' + exp[2], '%m/%d/%Y %H:%M')
e.add('dtend', end)
e.add('location', exp[4])
cal.add_component(e)
f = open(USERNAME + '_physicsExperiment.ics', 'wb')
f.write(cal.to_ical())
f.close()
print("物理实验日历文件已保存到 " + USERNAME + '_physicsExperiment.ics')