This repository has been archived by the owner on Mar 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathustcmis.py
262 lines (242 loc) · 8.78 KB
/
ustcmis.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
from datetime import date, timedelta
from newknn import Captcha
import random
weeks_name = dict(zip(range(1, 8), ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU']))
def decode_place_time(mess):
content = {}
mess = mess.split(';')
for item in mess:
item = item.split(u':')
if len(item) < 2:
return {}
place = item[0]
if item[1].startswith(u'单') or item[1].startswith(u'双'):
other = item[1][0] + u'周'
week = item[1][1]
else:
other = ''
week = item[1][0]
if item[1].find(u'晚上') != -1:
classes = ['11']
else:
left = item[1].find('(')
right = item[1].find(')')
classes = item[1][left + 1: right].split(',')
for i in classes:
info = {
'place': place,
'week': week,
'class_no': i,
'other': other
}
content[len(content)] = info
return content
def next_weekday(d, weekday):
days_ahead = weekday - d.weekday()
if days_ahead < 0:
days_ahead += 7
return d + timedelta(days_ahead)
def ical(classes, class_day_time, semester_date, semester):
vcalendar = '''BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VTIMEZONE
TZID:Asia/Shanghai
X-LIC-LOCATION:Asia/Shanghai
BEGIN:STANDARD
TZOFFSETFROM:+0800
TZOFFSETTO:+0800
TZNAME:CST
DTSTART:19700101T000000
END:STANDARD
END:VTIMEZONE
%s
END:VCALENDAR
'''
event = '''BEGIN:VEVENT
DTSTART;TZID=Asia/Shanghai:%(class_firstday)sT%(class_start_time)s
DTEND;TZID=Asia/Shanghai:%(class_firstday)sT%(class_end_time)s
RRULE:FREQ=WEEKLY;UNTIL=%(semester_end_date)sT235900;BYDAY=%(class_week)s
DESCRIPTION:%(class_info)s
LOCATION:%(class_location)s
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:%(class_name)s
TRANSP:OPAQUE
END:VEVENT
'''
events = ''
for index, items in classes.iteritems():
for i, item in items[u'上课时间地点'].iteritems():
try:
firstday = next_weekday(semester_date[semester][0],
int(item['week']) - 1).strftime('%Y%m%d')
except:
continue
info = {
'class_firstday': firstday,
'class_start_time': class_day_time[item['class_no']][0],
'class_end_time': class_day_time[item['class_no']][1],
'semester_end_date':
semester_date[semester][1].strftime('%Y%m%d'),
'class_week': weeks_name[int(item['week'])],
'class_info': ' '.join([items[u'教师'], item['other']]),
'class_location': item['place'],
'class_name': items[u'课程名称']
}
events += event % info
vcalendar = vcalendar % events
return vcalendar
class USTCMis:
url = 'http://mis.teach.ustc.edu.cn/'
class_day_time = {
'1': ['075000', '083500'],
'2': ['084000', '092500'],
'3': ['094500', '103000'],
'4': ['103500', '112000'],
'5': ['112500', '121000'],
'6': ['140000', '144500'],
'7': ['145000', '153500'],
'8': ['155500', '164000'],
'9': ['164500', '173000'],
'10': ['173500', '182000'],
'11': ['193000', '201500'],
'12': ['202000', '210500'],
'13': ['211000', '215500']
}
semester_date = {
'20121': [date(2012, 9, 3), date(2013, 1, 25)],
'20122': [date(2013,2, 25), date(2013, 6, 28)],
'20123': [date(2013, 7, 1), date(2013, 8, 9)],
'20131': [date(2013, 9, 2), date(2014, 1, 17)],
'20132': [date(2014, 2, 17), date(2014, 6, 20)],
'20133': [date(2014, 6, 23), date(2014, 8, 1)],
'20141': [date(2014, 9, 1), date(2015, 1, 30)],
'20142': [date(2015, 3, 1), date(2015, 6, 21)]
}
captcha = Captcha()
def __init__(self):
self.s = requests.Session()
self.login_status = False
# Login
def get_check_code(self):
self.s.post(USTCMis.url + 'userinit.do', data={'userbz': 's'})
r = self.s.get(USTCMis.url + 'randomImage.do')
img = r.content
return img
def login(self, user_code, pwd):
img = self.get_check_code()
check_code = self.captcha.hack(img)
login_info = {
'userbz': 's',
'hidjym': '',
'userCode': user_code,
'passWord': pwd,
'check': check_code
}
self.s.post(USTCMis.url + 'login.do', data=login_info)
self.check_login()
if not self.login_status:
filename = str(random.randint(1,1000)) + check_code + ".jpg"
print filename
with open(filename, 'w') as f:
f.write(img)
return self.login_status
def check_login(self):
r = self.s.get(USTCMis.url + 'init_xk_ts.do')
self.login_status = (r.text.find(u'所在院系') != -1)
return self.login_status
# Grade
def get_grade_semester_list(self):
if not self.login_status or not self.check_login():
return {'error': 'Not Login'}
r = self.s.get(USTCMis.url + 'initquerycjxx.do')
soup = BeautifulSoup(r.text)
content = []
options = soup.find_all('option')
for i in options:
content.append([i.attrs['value'], i.text])
return content
def get_grade(self, semester=''):
if not self.login_status or not self.check_login():
return {'error': 'Not Login'}
query_data = {
'xuenian': semester,
'px': 1,
'zd': 0
}
r = self.s.post(USTCMis.url + 'querycjxx.do', data=query_data)
soup = BeautifulSoup(r.text)
content = {}
tables = [i.find_all('td') for i in soup.find_all('table')]
basic = [i.string.strip() for i in tables[0]]
content['basic'] = dict(zip(basic[::2], basic[1::2]))
detail_key = [i.string.strip() for i in tables[1]]
detail_item = [i.string.strip() for i in tables[2]]
detail = {}
for i in xrange(len(detail_item) / len(detail_key)):
n = len(detail_key)
item = detail_item[i * n: i * n + n]
detail[i] = dict(zip(detail_key, item))
content['detail'] = detail
return content
# Timetable
def get_timetable_semester_list(self):
if not self.login_status or not self.check_login():
return {'error': 'Not Login'}
r = self.s.get(USTCMis.url + 'initxkjgquery.do')
soup = BeautifulSoup(r.text)
content = []
options = soup.find_all('option')
for i in options:
if len(i.attrs['value']):
content.append([i.attrs['value'], i.text])
return content
def get_timetable(self, semester):
if not self.login_status or not self.check_login():
return {'error': 'Not Login'}
query_data = {'selxnxq': semester}
r = self.s.post(USTCMis.url + 'xkjgquery.do?tjfs=1', data=query_data)
soup = BeautifulSoup(r.text)
table = soup.find_all('table', id='jcxxtable0')[0]
tds = [i.string.strip() for i in table.find_all('td')]
info_key = tds[0:8]
info_item = [tds[i:i + 8] for i in xrange(8, len(tds), 8)]
classes = [dict(zip(info_key, item)) for item in info_item]
content = dict(zip(range(len(info_item)), classes))
for i in content.keys():
mess = content[i][u'上课时间地点']
content[i][u'上课时间地点'] = decode_place_time(mess)
return content
def get_ical(self, semester):
if not self.login_status or not self.check_login():
return 'error'
classes = self.get_timetable(semester)
return ical(classes, USTCMis.class_day_time, USTCMis.semester_date,
semester)
# Class Operation
def insert_class(self, param_str):
param_key = ['tag', 'actionname', 'xnxq', 'kcid', 'kcbjbh', 'kclb',
'kcsx', 'sjpdm', 'kssjdm', 'cxck', 'zylx', 'gxkfl',
'xlh', 'qsz', 'jzz']
param_list = param_str.split(',')
param = dict(zip(param_key,param_list))
data = {
"xnxq": param.get('xnxq'),
"kcbjbh": param.get('kcbjbh'),
"kcid": param.get('kcid'),
"kclb": param.get('kclb'),
"kcsx": param.get('kcsx'),
"cxck": param.get('cxck'),
"zylx": param.get('zylx'),
"gxkfl": param.get('gxkfl'),
"xlh": param.get('xlh'),
"sjpdm": param.get('sjpdm'),
"kssjdm": param.get('kssjdm')
}
self.s.get(USTCMis.url + 'xkgcinsert.do',data=data)