-
Notifications
You must be signed in to change notification settings - Fork 0
/
school.py
256 lines (222 loc) · 8.22 KB
/
school.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
__author__ = 'halfcrazy'
#!usr/bin/env python
#-*- coding: utf-8 -*-
import sys
sys.path.insert(0, 'libs')
from bs4 import BeautifulSoup
import urllib2
import urllib
import gzip
import cookielib
import json
import StringIO
import string
import re
from Recognize import recognize, binary
#-----------------------------------------------------------------------------
# Login inhttp://cityjw.dlut.edu.cn:7001
def Login2school(username, password):
# Enable cookie support for urllib2
url = 'http://cityjw.dlut.edu.cn:7001/ACTIONLOGON.APPPROCESS?mode=4'
cookiejar = cookielib.CookieJar()
urlopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(urlopener)
urlopener.addheaders.append(
('Referer', r'http://cityjw.dlut.edu.cn:7001/ACTIONLOGON.APPPROCESS?mode=3'))
urlopener.addheaders.append(
('Accept-Language', r'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3'))
urlopener.addheaders.append(('Accept-Encoding', r'gzip,deflate'))
urlopener.addheaders.append(('Host', r'http://cityjw.dlut.edu.cn:7001'))
urlopener.addheaders.append(
('User-Agent', r'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0'))
urlopener.addheaders.append(('Connection', 'Keep-Alive'))
# print 'crawler Login......'
imgurl = r'http://cityjw.dlut.edu.cn:7001/ACTIONVALIDATERANDOMPICTURE.APPPROCESS'
# download captcha
isDownOK, img = DownloadFile(imgurl, urlopener)
# recognize the captcha
authcode = recognize(binary(img))
# Send login/password to the site and get the session cookie
values = {'WebUserNO': username, 'Password': password,
'Agnomen': authcode, 'submit.x': 0, 'submit.y': 0}
urlcontent = urlopener.open(
urllib2.Request(url, urllib.urlencode(values).replace('%2A', '*')))
page = urlcontent.read()
page = unpack(page, urlcontent)
page = page.decode('gb2312').encode('utf-8')
# Make sure we are logged in, check the returned page content
if 'ACTIONLOGOUT' not in page:
# print 'Login failed with username=%s, password=%s and authcode=%s' \
#% (username, password, authcode)
return None, urlopener
else:
# print 'Login succeeded! with username=%s, password=%s and authcode=%s' \
# % (username, password, authcode)
pattern = re.compile(r'<td align="left">(.*)</td>')
name = pattern.search(page).groups()
name = name[0]
return name, urlopener
#------------------------------------------------------------------------------
# unzip module
def unpack(page, urlcontent):
if urlcontent.headers.get('content-encoding') == 'deflate':
try:
page = zlib.decompress(page, -zlib.MAX_WBITS)
except zlib.error:
page = zlib.decompress(page)
elif urlcontent.headers.get('content-encoding') == 'gzip':
obj = StringIO.StringIO(page)
page = gzip.GzipFile(fileobj=obj, mode="r").read()
return page
#------------------------------------------------------------------------------
# Grade module
def grades(username, password, term):
name, urlopener = Login2school(username, password)
if name == None:
output = {}
output["status"] = "error"
json_string = json.dumps(output, indent=4, ensure_ascii=False)
return False, json_string
else:
score_url = 'http://cityjw.dlut.edu.cn:7001/ACTIONQUERYSTUDENTSCORE.APPPROCESS'
score_resp = urlopener.open(
urllib2.Request(score_url, urllib.urlencode({'YearTermNO': term})))
scorehtml = score_resp.read()
scorehtml = unpack(scorehtml, score_resp)
json_string = generate_grades_json(scorehtml, term)
return True, json_string.encode('utf-8')
#------------------------------------------------------------------------------
# Schedule module
def schedule(username, password):
name, urlopener = Login2school(username, password)
if name == None:
output = {}
output["status"] = "error"
json_string = json.dumps(output, indent=4, ensure_ascii=False)
return False, json_string.encode('uft-8')
else:
schedule_url = 'http://cityjw.dlut.edu.cn:7001/ACTIONQUERYSTUDENTSCHEDULEBYSELF.APPPROCESS'
schedule_resp = urlopener.open(schedule_url)
# .decode('gb2312').encode('utf-8')
schedulehtml = schedule_resp.read()
# .decode('gb2312').encode('utf-8')
schedule = unpack(schedulehtml, schedule_resp)
# print schedule
json_string = generate_shedule_json(schedule)
return True, json_string.encode('utf-8')
def post(url, data):
req = urllib2.Request(url)
data = urllib.urlencode(data)
#enable cookie
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
response = opener.open(req, data)
return response.read()
def teacher_schedule(id):
data={
"DepartmentNO":None,
"StaffroomNO":None,
"TeacherNO":id
}
url="http://cityjw.dlut.edu.cn:7001/ACTIONQUERYTEACHERSCHEDULEBYPUBLIC.APPPROCESS"
page=post(url,data)
json_string=generate_shedule_json(page)
return True,json_string.encode('utf-8')
#------------------------------------------------------------------------------
# Generate grades json
def generate_grades_json(scorehtml, term):
soup = BeautifulSoup(scorehtml, "lxml")
ctt = soup.get_text("|", strip=True)[220:-24]
ctt = ctt.replace("|", "\n")[346:]
it = ctt.split('\n')
output = {}
courseinfo = []
subdict = {}
cnt = 0
for i in it:
if cnt == 0:
cnt = cnt + 1
continue
if cnt == 1:
subdict["category"] = i
elif cnt == 2:
subdict["id"] = i
elif cnt == 3:
subdict["name"] = i
elif cnt == 4:
subdict["examining_method"] = i
elif cnt == 5:
subdict["credit_hours"] = i
elif cnt == 6:
subdict["credit"] = i
elif cnt == 7:
subdict["general_grades"] = i
elif cnt == 8:
subdict["final_grades"] = i
elif cnt == 9:
subdict["average_grades"] = i
cnt = cnt + 1
# print i
if cnt == 10:
courseinfo.append(subdict)
subdict = {}
cnt = 1
# print
output["status"] = "ok"
output["term"] = term
output["info"] = courseinfo
json_string = json.dumps(output, indent=4, ensure_ascii=False)
return json_string
#-----------------------------------------------------------------------------
# Generate schedule json
def generate_shedule_json(page):
page = page.replace('<', ' <')
soup = BeautifulSoup(page, "lxml")
tr_tag = soup.find_all('tr', limit=14)
# print tr_tag
arr = []
arr.append(tr_tag[8].get_text()) # 1-2
arr.append(tr_tag[9].get_text()) # 3-4
arr.append(tr_tag[10].get_text()) # 5-6
arr.append(tr_tag[11].get_text()) # 7-8
arr.append(tr_tag[12].get_text()) # 9-10
arr.append(tr_tag[13].get_text()) # 11-12
output = {}
info = []
for row in arr:
subdict = {}
lst = map(string.strip, row.split('\n'))
# lst=row.split('\n')
subdict['monday'] = lst[2]
subdict['tuesday'] = lst[3]
subdict['wednesday'] = lst[4]
subdict['thursday'] = lst[5]
subdict['friday'] = lst[6]
subdict['saturday'] = lst[7]
subdict['sunday'] = lst[8]
info.append(subdict)
output['info'] = info
json_string = json.dumps(output, indent=4, ensure_ascii=False)
return json_string
#-----------------------------------------------------------------------------
# Download from fileUrl then save to fileToSave
# Note: the fileUrl must be a valid file
def DownloadFile(fileUrl, urlopener):
isDownOk = False
output = StringIO.StringIO()
try:
if fileUrl:
output.write(urlopener.open(urllib2.Request(fileUrl)).read())
isDownOK = True
else:
isDownOk = False
except:
isDownOK = False
return isDownOK, output
def testlogin(username, password):
name, urlopener = Login2school(username, password)
if name == None:
print "failed"
else:
print "success"
if __name__ == "__main__":
pass