湖工大课程表获取ics文件.py

湖工大课程表获取ics文件.py

五月 07, 2020
  • 用于获取湖南工业大学课程表并生成ics文件

利用强制教务系统API获取用户信息并处理

https://qzapi.github.tlingc.com/


使用方法

下载源文件以及requirements.txt

运行py文件之前进入requirements.txt所在文件夹执行

1
pip install -r requirements.txt

双击py文件运行

输入学号,密码获取ics文件

当前版本运行速度较慢,代码还会优化


代码

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# @name             HUT_Get_Calendar.ics
# @namespace https://github.com/takanashi-shiro/HUT_Get_Calendar_ics
# @version 1.1.0
# @description 用python提取课表并生成可导入至日历中isc文件
# @author: Takanashi-Shiro


#-*-coding:utf-8-*-
import requests, bs4, re, os
from lxml import etree
import datetime
import os
import time


def find_class(cookie,zc,now_week_date): #获取zc周课程 now_week_date为当前周的第一天日期
url = 'http://218.75.197.123:83/app.do'
header = {
'token':cookie
}
data = {
'method':'getKbcxAzc',
'xh':user,
'xnxqid':'',
'zc':str(zc)
}
response = requests.get(url=url,params=data,headers=header)
soup = bs4.BeautifulSoup(response.text, "html.parser")
soup_str = str(soup)

global course,course_name,course_time_start,course_time_finnal,course_classroom,course_teacher,course_day
s = 0
f = 2147483647

course = []
course_name = []
course_time_start = []
course_time_finnal = []
course_classroom = []
course_teacher = []
course_day = []

for i in range(0,len(soup_str)): #先获取课程的全部信息
if soup_str[i] == '{':
s = i
if soup_str[i] == '}':
f = i
course.append(soup_str[s+1:f])
s = f+1
f = 2147483647

for i in course: #再对每个课程的信息进行分类

fs = str(i).find('jssj')+7
ff = fs+5
course_time_finnal.append(str(i)[fs:fs+2]+str(i)[fs+3:ff])

names = str(i).find('kcmc')+7
namef = str(i).find('kcsj')-3
course_name.append(str(i)[names:namef])

ss = str(i).find('kssj')+7
sf = ss+5
course_time_start.append(str(i)[ss:ss+2]+str(i)[ss+3:sf])

cs = str(i).find('jsmc')+7
cf = str(i).find('jsxm')-3
if str(i)[cs:cf] == 'ul':
course_classroom.append('无/待定')
else:
course_classroom.append(str(i)[cs:cf])

ts = str(i).find('jsxm')+7
tf = len(str(i))-1
course_teacher.append(str(i)[ts:tf])

day = str(i)[str(i).find('kcsj')+7]
course_day.append(day)

tras(now_week_date,1)


def login(): #登入获取cookies
global user
user = input("输入用户名(学号):")
user_password = input("输入密码:")
url = 'http://218.75.197.123:83/app.do?method=authUser&xh='+user+'&pwd='+user_password
response = requests.get(url)

soup = bs4.BeautifulSoup(response.text, "html.parser")
s_soup = str(soup)
success = s_soup[11]

if success == 'f':
os.system('cls')
print("用户名或密码错误,请重试")
return login()
else:
begin = s_soup.find("token")+8
final = s_soup.find("user")-3
cookie = s_soup[begin:final]
return cookie


def get_now_week(cookie): #获取当前日期为第几周
url = 'http://218.75.197.123:83/app.do'
header = {
'token':cookie
}
data = {
'method':'getCurrentTime',
'currDate':datetime.datetime.now().strftime('%Y-%m-%d')
}
response = requests.get(url=url,params=data,headers=header)
soup = bs4.BeautifulSoup(response.text, "html.parser")
soup_str = str(soup)
zc = eval(soup_str[soup_str.find('zc')+4:soup_str.find('e_time')-2])
global s_time
s_time = soup_str[11:15]+soup_str[16:18]+soup_str[19:21] #当前日期所在周的第一天 用于推出从当前周起 以后的所有课程
return zc


def tras(now_week_date,day): #将获取的课程信息转换为ics格式输出
global res
now_day = int(day)
now_time = now_week_date
for i in range(0,len(course)):
if(int(course_day[i])!=now_day): #判断是否是同一天的课程 如果不是就加一天
n = int(course_day[i]) - now_day
now_time = str((datetime.datetime(int(now_time[0:4]),int(now_time[4:6]),int(now_time[6:8])) + datetime.timedelta(days=n)).strftime('%Y%m%d'))
now_day+=n
st = now_time + 'T' +course_time_start[i] + '00'
ft = now_time + 'T' +course_time_finnal[i] +'00'
res += "BEGIN:VCALENDAR\nPRODID:-//Google Inc//Google Calendar 70.9054//EN\nVERSION:2.0\nCALSCALE:GREGORIAN\nMETHOD:PUBLISH\nX-WR-CALNAME:课程表\nX-WR-TIMEZONE:America/New_York\nBEGIN:VEVENT\n"
res += "DTSTART:"+st+'\n'
res += "DTEND:"+ft+'\n'
res += "DTSTAMP:"+st+'\n'
res += "UID:课程表\n"
res += "CREATED:"+st+'\n'
res += "DESCRIPTION:"+course_teacher[i]+'\n'
res += "LAST-MODIFIED:"+st+'\n'
res +="LOCATION:"+course_classroom[i]+'\n'
res += "SEQUENCE:0"+'\n'
res += "STATUS:CONFIRMED"+'\n'
res += "SUMMARY:"+course_name[i]+'\n'
res += "TRANSP:OPAQUE\nEND:VEVENT\nEND:VCALENDAR\n"


def jdt(start,i,len_jdt):
a = '*' * i
b = '.' * (len_jdt - i)
c = (i/len_jdt)*100
dur = time.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end='')
time.sleep(0.1)


if __name__ == "__main__":
cookie = login()
now_week = int(get_now_week(cookie))
cnt = 0
a = open("your_calendar.ics",mode='w',encoding="utf-8")

global res
res = ''

os.system('cls')
s_jdt = time.perf_counter()
print("执行开始".center(50//2,'-'))
cnt = 0
now_jdt = 20-now_week
for i in range(now_week,21): #由于一学期正常最多不超过20周 循环到20周
find_class(cookie,i,s_time) #👇将每次获得的now_week转换成datetime类型 +7天 直接到下一周
s_time = str((datetime.datetime(int(s_time[0:4]),int(s_time[4:6]),int(s_time[6:8])) + datetime.timedelta(days=7)).strftime('%Y%m%d'))
jdt(s_jdt,int(cnt),50)
cnt += 50/now_jdt
print("\n"+"执行结束".center(50//2,'-'))
a.write(res)
a.close
os.system('pause')

Requirements.txt

1
2
3
4
5
beautifulsoup4==4.8.2
bs4==0.0.1
entrypoints==0.3
lxml==4.5.0
requests==2.23.0

初学python,轻喷。