-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexamdownloader.py
182 lines (158 loc) · 6 KB
/
examdownloader.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
import httplib
import urllib
import os
class examdownloader(object):
def __init__(self, mode):
self.mode = mode
def getContents(self, module, username, password, destination, downloadEndCallback, updateStatus):
updateStatus('Connecting to NUS Library Portals...')
conn = httplib.HTTPSConnection('libbrs.nus.edu.sg')
page = '/infogate/loginAction.do?execution=login'
conn.request('GET', page)
resp = conn.getresponse()
conn.close()
cookie = resp.getheader('Set-Cookie')
sessionid = cookie[:cookie.find(';')]
cookie = sessionid
headers = {
'Content-Type' : 'application/x-www-form-urlencoded',
'Cookie' : cookie
}
headersGet = {
'Cookie' : cookie
}
params = {
'userid': username,
'password': password,
'domain': 'NUSSTU',
'key': 'blankid+RESULT+EXAM+' + module
}
params = urllib.urlencode(params)
conn = httplib.HTTPSConnection('libbrs.nus.edu.sg')
conn.request('POST', page, params, headers)
resp = conn.getresponse()
data = str(resp.read())
conn.close()
if data.find("login") != -1:
updateStatus("Wrong username/password", "error")
return
conn = httplib.HTTPSConnection('libbrs.nus.edu.sg')
page = '/infogate/jsp/login/success.jsp;jsessionid='+sessionid+'?exe=ResultList'
conn.request('GET', page, params, headersGet)
conn.close()
conn = httplib.HTTPSConnection('libbrs.nus.edu.sg')
page = '/infogate/searchAction.do?execution=ResultList'
params = 'database=EXAM&searchstring='+module+'&d='
conn.request('POST', page, params, headers)
resp = conn.getresponse()
data = resp.read()
conn.close()
data = str(data)
params = self.getParams(data)
maxDocIndex = int(params['maxNo'])
params['maxDocIndex'] = params['maxNo']
pdfs = {}
if maxDocIndex < 1:
updateStatus('No papers available for this module', 'error')
return
for i in range(1, maxDocIndex+1):
conn = httplib.HTTPSConnection('libbrs.nus.edu.sg')
page = '/infogate/searchAction.do?execution=ViewSelectedResultListLong'
params['preSelectedId'] = i
params['exportids'] = i
conn.request('POST', page, urllib.urlencode(params), headers)
resp = conn.getresponse()
data = resp.read()
conn.close()
data = str(data)
pdfIndex = data.find('View attached PDF file')
if pdfIndex == -1:
continue
pdfIndex = data.rfind('href=', 0, pdfIndex)
openquotes = data.find('"', pdfIndex)
closequotes = data.find('"', openquotes+1)
page = page[:page.rfind('/')+1] + data[openquotes+1:closequotes]
titleIndex = data.find('title=', pdfIndex)
if titleIndex == -1:
continue
openquotes = data.find('"', titleIndex)
closequotes = data.find('"', openquotes+1)
title = data[openquotes+1: closequotes]
pdfs[title] = page
counter = 0;
for title, page in pdfs.items():
counter += 1
updateStatus('Downloading ' + str(counter) + ' of ' + str(len(pdfs)))
conn = httplib.HTTPSConnection('libbrs.nus.edu.sg')
conn.request('GET', page, None, headersGet)
resp = conn.getresponse()
data = resp.read()
conn.close()
title = title[title.find('file')+5:]
filename = destination + '/' + title
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
try:
f = open(filename, 'wb')
print('Writing ' + title)
f.write(data)
f.close()
except Exception, e:
updateStatus('Invalid destination', 'error')
return
if 'filename' in vars():
downloadEndCallback(True, filename, counter)
else:
downloadEndCallback(False)
def getParams(self, data):
start = data.find('databasenamesasstring')
start = data.rfind('<', 0, start)
end = data.find('<select', start)
params = {
'databasenamesasstring' : 'Examination Papers Database',
'searchid':'-6901505210342489183',
'f':'list',
'b':'1',
'p':'1',
'd':'EXAM',
'u':'dummy',
'r':'',
'l':'20',
'n':'',
'nn':'',
'historyid':'1',
'maxDocIndex':'11',
'preSelectedId':'1,', #id
'maxNo':'11',
'sPage1':'1',
'pageNo1':'1',
'exportids':'1', #id
'maxNo':'11',
'sPage2':'1',
'pageNo2':'1',
'paraid[0]':'PGH2',
'parashortname[0]':'FACU',
'paravalue[0]':'',
'paraid[1]':'PGH3',
'parashortname[1]':'SUBJ',
'paravalue[1]':'',
'paraid[2]':'PGH5',
'parashortname[2]':'CNAM',
'paravalue[2]':''
}
start = data.find('name=', start, end)
while start != -1:
openquotes = data.find('"', start, end)
closequotes = data.find('"', openquotes+1, end)
name = data[openquotes+1:closequotes]
start = data.find('value=', start, end)
openquotes = data.find('"', start, end)
closequotes = data.find('"', openquotes+1, end)
value = data[openquotes+1:closequotes]
params[name] = value
start = data.find('name=', start, end)
maxNo = data.find("Listing 1 to ");
if maxNo != -1:
maxNo = int(data[maxNo+13:maxNo+15])
params['maxNo'] = maxNo
return params