|
| 1 | +import http.client |
| 2 | +import urllib.request |
| 3 | +import urllib.parse |
| 4 | + |
| 5 | +module = 'cs3230' |
| 6 | +euzernaem = '' |
| 7 | +parsewerd = '' |
| 8 | + |
| 9 | +def getContents(): |
| 10 | + global module, euzernaem, parsewerd |
| 11 | + conn = http.client.HTTPSConnection('libbrs.nus.edu.sg') |
| 12 | + page = '/infogate/loginAction.do?execution=login' |
| 13 | + conn.request('GET', page) |
| 14 | + |
| 15 | + resp = conn.getresponse() |
| 16 | + conn.close() |
| 17 | + cookie = resp.getheader('Set-Cookie') |
| 18 | + sessionid = cookie[:cookie.find(';')] |
| 19 | + cookie = sessionid |
| 20 | + print(cookie) |
| 21 | + |
| 22 | + headers = { |
| 23 | + "Content-Type" : "application/x-www-form-urlencoded", |
| 24 | + "Cookie" : cookie |
| 25 | + } |
| 26 | + headersGet = { |
| 27 | + "Cookie" : cookie |
| 28 | + } |
| 29 | + |
| 30 | + params = 'userid='+euzernaem+'&password='+parsewerd+'&domain=NUSSTU&key=blankid%2BRESULT%2BEXAM%2B'+module |
| 31 | + |
| 32 | + |
| 33 | + conn = http.client.HTTPSConnection('libbrs.nus.edu.sg') |
| 34 | + conn.request("POST", page, params, headers) |
| 35 | + resp = conn.getresponse() |
| 36 | + conn.close() |
| 37 | + |
| 38 | + conn = http.client.HTTPConnection('libbrs.nus.edu.sg:8080') |
| 39 | + page = '/infogate/jsp/login/success.jsp;jsessionid='+sessionid+'?exe=ResultList' |
| 40 | + conn.request("GET", page, params, headersGet) |
| 41 | + conn.close() |
| 42 | + |
| 43 | + conn = http.client.HTTPConnection('libbrs.nus.edu.sg:8080') |
| 44 | + page = '/infogate/searchAction.do?execution=ResultList' |
| 45 | + params = 'database=EXAM&searchstring='+module+'&d=' |
| 46 | + conn.request("POST", page, params, headers) |
| 47 | + resp = conn.getresponse() |
| 48 | + data = resp.read() |
| 49 | + conn.close() |
| 50 | + |
| 51 | + data = str(data) |
| 52 | + downloadall(data, headers, headersGet) |
| 53 | + |
| 54 | +def printNice(params): |
| 55 | + for key in params: |
| 56 | + print(str(key) + ' = ' + str(params[key])) |
| 57 | + |
| 58 | +def downloadall(data, headers, headersGet): |
| 59 | + params = getParams(data) |
| 60 | + #printNice(params) |
| 61 | + maxDocIndex = int(params['maxNo']) |
| 62 | + params['maxDocIndex'] = params['maxNo'] |
| 63 | + |
| 64 | + for i in range(1,maxDocIndex+1): |
| 65 | + conn = http.client.HTTPConnection('libbrs.nus.edu.sg:8080') |
| 66 | + page = '/infogate/searchAction.do?execution=ViewSelectedResultListLong' |
| 67 | + params['preSelectedId'] = i |
| 68 | + params['exportids'] = i |
| 69 | + conn.request("POST", page, urllib.parse.urlencode(params), headers) |
| 70 | + resp = conn.getresponse() |
| 71 | + data = resp.read() |
| 72 | + conn.close() |
| 73 | + data = str(data) |
| 74 | + |
| 75 | + pdfIndex = data.find('View attached PDF file') |
| 76 | + if pdfIndex == -1: |
| 77 | + continue |
| 78 | + pdfIndex = data.rfind('href=', 0, pdfIndex) |
| 79 | + openquotes = data.find('"', pdfIndex) |
| 80 | + closequotes = data.find('"', openquotes+1) |
| 81 | + page = page[:page.rfind('/')+1] + data[openquotes+1:closequotes] |
| 82 | + |
| 83 | + titleIndex = data.find('title=', pdfIndex) |
| 84 | + if titleIndex == -1: |
| 85 | + continue |
| 86 | + openquotes = data.find('"', titleIndex) |
| 87 | + closequotes = data.find('"', openquotes+1) |
| 88 | + title = data[openquotes+1: closequotes] |
| 89 | + |
| 90 | + conn = http.client.HTTPConnection('libbrs.nus.edu.sg:8080') |
| 91 | + conn.request("GET", page, None, headersGet) |
| 92 | + resp = conn.getresponse() |
| 93 | + data = resp.read() |
| 94 | + |
| 95 | + conn.close() |
| 96 | + |
| 97 | + title = title[title.find('file')+5:] |
| 98 | + print('Writing ' + title) |
| 99 | + f = open(title, 'wb+') |
| 100 | + f.write(data) |
| 101 | + f.close() |
| 102 | + |
| 103 | + |
| 104 | +def getParams(data): |
| 105 | + start = data.find('databasenamesasstring') |
| 106 | + start = data.rfind('<', 0, start) |
| 107 | + end = data.find('<select', start) |
| 108 | + |
| 109 | + params = { |
| 110 | + 'databasenamesasstring' : 'Examination Papers Database', |
| 111 | + 'searchid':'-6901505210342489183', |
| 112 | + 'f':'list', |
| 113 | + 'b':'1', |
| 114 | + 'p':'1', |
| 115 | + 'd':'EXAM', |
| 116 | + 'u':'dummy', |
| 117 | + 'r':'', |
| 118 | + 'l':'20', |
| 119 | + 'n':'', |
| 120 | + 'nn':'', |
| 121 | + 'historyid':'1', |
| 122 | + 'maxDocIndex':'11', |
| 123 | + 'preSelectedId':'1,', #id |
| 124 | + 'maxNo':'11', |
| 125 | + 'sPage1':'1', |
| 126 | + 'pageNo1':'1', |
| 127 | + 'exportids':'1', #id |
| 128 | + 'maxNo':'11', |
| 129 | + 'sPage2':'1', |
| 130 | + 'pageNo2':'1', |
| 131 | + 'paraid[0]':'PGH2', |
| 132 | + 'parashortname[0]':'FACU', |
| 133 | + 'paravalue[0]':'', |
| 134 | + 'paraid[1]':'PGH3', |
| 135 | + 'parashortname[1]':'SUBJ', |
| 136 | + 'paravalue[1]':'', |
| 137 | + 'paraid[2]':'PGH5', |
| 138 | + 'parashortname[2]':'CNAM', |
| 139 | + 'paravalue[2]':'' |
| 140 | + } |
| 141 | + |
| 142 | + start = data.find('name=', start, end) |
| 143 | + while start != -1: |
| 144 | + openquotes = data.find('"', start, end) |
| 145 | + closequotes = data.find('"', openquotes+1, end) |
| 146 | + name = data[openquotes+1:closequotes] |
| 147 | + start = data.find('value=', start, end) |
| 148 | + openquotes = data.find('"', start, end) |
| 149 | + closequotes = data.find('"', openquotes+1, end) |
| 150 | + value = data[openquotes+1:closequotes] |
| 151 | + params[name] = value |
| 152 | + |
| 153 | + #print (name + ' ==> ' + value) |
| 154 | + start = data.find('name=', start, end) |
| 155 | + |
| 156 | + return params |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == '__main__': |
| 160 | + getContents() |
| 161 | + |
0 commit comments