-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
407 lines (364 loc) · 15.8 KB
/
app.js
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* Module dependencies.
*/
var express = require('express')
, jsdom = require('jsdom')
, request = require('request')
, url = require('url')
, querystring = require('querystring')
, feedparser = require('feedparser');
var app = express();
function getParameterByName(name, url)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(url);
if(results === null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
app.get('/', function (req, res){
res.end('Hi!');
});
app.get('/search', function (req, res) {
var searchQuery = req.query["query"];
var searchType = req.query["queryType"];
var accessCode = req.query["accessCode"];
var pagination = req.query["page"];
var scanStart = req.query["scanStart"]; //where the next page should start
var alephCookie = req.query["alephCookie"];
var uriBase = 'http://apps.appl.cuny.edu:83/F/';
var whichLibrary = req.query["school"];
var barcode = req.query["barcode"];
console.log(req.searchQuery);
if (searchType == 'All Fields' || accessCode || barcode){
if (accessCode){
var func = '?func=find-acc&acc_sequence=' + accessCode + '&local_base=' + (whichLibrary || 'Hunter');
}
else if (barcode){
func = '?func=find-c&ccl_term=BAR%3D' + barcode;
}
else{
func = '?func=find-e&adjacent=N&find_scan_code=FIND_WRD&request=' + searchQuery.replace(' ', '+') + '&Search=+Search+&local_base=' + (whichLibrary || 'Hunter');
};
if (pagination > 1 && alephCookie){
uriBase = uriBase + (alephCookie);
var page = 1 + (20 * (pagination - 1 ));
func = '?func=short-jump&jump=' + page;
}
var options = {uri: uriBase + func};
request(options, function(error, response, body) {
// debugger;
if (error && response.statusCode !== 200) {
console.log(error);
}
jsdom.env({
html: body,
scripts: [
'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'
]
}, function (err, window) {
// load jquery
var $ = window.jQuery;
var cookieTable = $('table')[2];
var mine = $(cookieTable).find('td')[2];
var cookieSource = $($(mine).find('form')).attr('action') || '/ ';
var cookie = cookieSource.substr(cookieSource.lastIndexOf("/") + 1);
var resultsTable = $('table')[4];
var rows = $(resultsTable).children('tr');
allBooks = [];
rows.each(function (i, item) {
var image = $(item).children('td')[2];
$($(image).children('script')).remove();
//Get rid of follow-on requests
var imageSrc = $(image).find('img');
if ($(imageSrc).attr('src') == 'src') $(imageSrc).removeAttr('src');
var author = $(item).children('td')[3];
var title = $(item).children('td')[4];
$($(title).children('script')).remove();
var year = $(item).children('td')[5];
var resourceType = $(item).children('td')[6];
var holdingsAndLink = $(item).children('td')[7];
var holdingLibrary = $(holdingsAndLink).find('a');
var everyLibrary = [];
holdingLibrary.each(function (i, item) { //Getting all the different sublibraries that have an item
var newLink = $(item).attr('href');
var holdUrlObj = url.parse(newLink, true);
var subLibrary = {
libraryCode : holdUrlObj.query['sub_library'],
docNumber : holdUrlObj.query['doc_number']
};
everyLibrary.push(subLibrary);
});
var link = $(holdingLibrary).attr('href');
allBooks[i] = {
"title": $(title).text().trim(),
"author": $(author).text().trim(),
"year": $(year).text().trim(),
"resourceType": $(resourceType).text().trim(),
"library": $(holdingLibrary).text().trim()
};
if (image !== undefined)
allBooks[i].thumbnail = $(imageSrc).attr('src');
if (whichLibrary != 'U-CUN01' && link !== undefined){
var urlObj = url.parse(link, true);
allBooks[i].docNumber = urlObj.query['doc_number'];
allBooks[i].libraryCode = urlObj.query['sub_library'];
// allBooks[i].url = link;
};
whichLibrary == 'U-CUN01' ? allBooks[i].holdingLibraries = everyLibrary : allBooks[i].holdingLibraries = undefined;
});
res.cookie('AlephSession', cookie, { maxAge: 900000, httpOnly: false});
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
allBooks.shift();
finalJSON = {
alephCookie: cookie,
allBooks: allBooks
};
res.end(JSON.stringify(finalJSON, null, 2));
});
});
}
else{
if (scanStart){ //If we want the next page
var func = '?func=scan&scan_start='+ scanStart +'&scan_code=TTL&scan_op=CONT';
}else{
var func = '?func=find-e&adjacent=N&find_scan_code=SCAN_'+ searchType +'&request=' + searchQuery + '&Search=+Search+&local_base=' + (whichLibrary || 'Hunter');
}
var options = {uri: uriBase + func};
request(options, function(error, response, body) {
// debugger;
console.log(options.uri);
if (error && response.statusCode !== 200) {
console.log(error);
}
jsdom.env({
html: body,
scripts: [
'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'
]
}, function (err, window) {
// load jquery
var $ = window.jQuery;
var cookieTable = $('table')[2];
var mine = $(cookieTable).find('a')[1];
var theUrl = $(mine).attr('href');
// Getting the number for the next page
var nextScanStart = getParameterByName('scan_start', theUrl);
var resultsTable = $('table')[3];
var rows = $(resultsTable).children('tr');
allChoices = [];
rows.each(function (i, item) {
var callNumber = $(item).children('td')[0];
console.log($(callNumber).text());
var name = $(item).children('td')[1];
var link = $(name).find('a').attr('href');
allChoices[i] = {
callNumber: $(callNumber).text().trim(),
name: $(name).text().trim()
};
if (link !== undefined){
var urlObj = url.parse(link, true);
allChoices[i].docNumber = urlObj.query['doc_number'];
allChoices[i].accessCode = urlObj.query['acc_sequence'];
// allChoices[i].url = link;
}
});
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
allChoices.shift();
finalJSON = {
scanStart: nextScanStart,
allBooks: allChoices
};
res.end(JSON.stringify(finalJSON, null, 2));
});
});
}
});
app.get('/marc', function (req, res){
var docNumber = req.query["docNumber"];
var verbose = req.query["verbose"];
var uriBase = 'http://apps.appl.cuny.edu:83/F/';
verbose == '1' ? format = '001' : format = '002'
var options = {
uri: uriBase + '?func=direct&doc_number=' + docNumber + '&format=' + format
};
request(options, function(error, response, body) {
// debugger;
console.log(options.uri);
if (error && response.statusCode !== 200) {
console.log(error);
}
jsdom.env({
html: body,
scripts: [
'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'
]
}, function (err, window) {
// load jquery
var $ = window.jQuery;
$('img[src]').each(function(i,el){
$(el).removeAttr('src');
});
var wholeMarc = {};
var resultsTable = $('table')[4];
console.log($(resultsTable).html());
var rows = $(resultsTable).children('tr');
rows.each(function (i, item) {
var marcLabel = $(item).children('td')[0];
var marcValue = $(item).children('td')[1];
var name = $(marcLabel).text().trim();
var value = $(marcValue).text().trim();
wholeMarc[name] = value;
});
// allRecords.shift();
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
// console.log(wholeMarc);
res.end(JSON.stringify(wholeMarc, null, 2));
});
});
});
app.get('/details', function (req, res) {
var docNumber = req.query["docNumber"];
var library = req.query["library"];
if (library == 'none' || library===undefined){
var options = {
uri: 'http://apps.appl.cuny.edu:83/F/?func=item-global&doc_library=CUN01&doc_number=' + docNumber
};
}
else {
var options = {
uri: 'http://apps.appl.cuny.edu:83/F/?func=item-global&doc_library=CUN01&doc_number=' + docNumber + '&year=&volume=&sub_library=' + library
};
};
request(options, function(error, response, body) {
// debugger;
console.log(options.uri);
if (error && response.statusCode !== 200) {
console.log(error);
}
jsdom.env({
html: body,
scripts: [
'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'
]
}, function (err, window) {
// load jquery
var $ = window.jQuery;
$('img[src]').each(function(i,el){
$(el).removeAttr('src');
});
var allRecords = [];
var resultsTable = $('table')[7];
var citation = $('table')[2];
var rows = $(resultsTable).children('tr');
rows.each(function (i, item) {
var expLibrary = $(item).children('td')[1];
var expLocation = $(item).children('td')[2];
var callNumber = $(item).children('td')[3];
var description = $(item).children('td')[4];
var status = $(item).children('td')[5];
var dueDate = $(item).children('td')[6];
var dueHour = $(item).children('td')[7];
// var requestUrl = 'http://apps.appl.cuny.edu:83/F/?func=title-request-form&bib_doc_number=' + docNumber;
allRecords[i] = {
"citation": $(citation).html().trim(),
"library": $(expLibrary).html(),
"location": $(expLocation).html(),
"callNumber": $(callNumber).html(),
"description": $(description).html(),
"status": $(status).html(),
"dueDate": $(dueDate).html(),
"dueHour": $(dueHour).html(),
// "requestUrl": requestUrl,
"docNumber": docNumber
};
if (allRecords[i].location){
if (allRecords[i].location == 'WEB Resource' || allRecords[i].location.indexOf("WEB") != -1 ){
var newTable = $('table')[3];
var rows = $(newTable).children('tr');
rows.each(function (j,item){
var test = $(item).children('td')[1];
if ($(test).html().indexOf("Hunter") != -1 || $(test).html().indexOf("CUNY") != -1
|| $(test).html().indexOf("for all") != -1 || $(test).html().indexOf("Free") != -1){
var href = $(test).find('a').attr('href');
if (href === undefined) {
var nextRow = $(newTable).children('tr')[1];
var nextTd = $(nextRow).children('td')[1];
href = $(nextTd).find('a').attr('href');
}
var start = 'javascript.open_window('.length;
var linky = href.substring(start, href.length - 2);
var composed = '<a href='+ linky +'>Access online</a>';
allRecords[i].location = composed;
};
});
};
};
});
allRecords.shift();
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
// console.log(allRecords);
res.end(JSON.stringify(allRecords, null, 2));
});
});
});
app.get('/feed', function (req, res) {
var allNews = new Array();
var news = feedparser.parseUrl('http://feeds2.feedburner.com/HunterCollegeLibrariesBlog')
.on('article', function (article) {
var paper = JSON.stringify(article)
allNews.push(paper)
})
.on('complete', function callback (meta, articles){
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
res.end(JSON.stringify(articles))
});
});
app.get('/hours', function (req, res) {
var options = {
uri: 'http://library.hunter.cuny.edu/about/hours',
};
request(options, function(error, response, body) {
// debugger;
console.log(options.uri)
if (error && response.statusCode !== 200) {
console.log(error);
}
jsdom.env({
html: body,
scripts: [
'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'
]
}, function (err, window) {
// load jquery
var $ = window.jQuery;
var hoursTable = $('table')[0];
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
res.end($(hoursTable).html());
});
});
});
var port = process.env.PORT || 3002;
app.listen(port, '0.0.0.0');
console.log('Server running at http://127.0.0.1:' + (port || '3002'));