-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
35 lines (30 loc) · 951 Bytes
/
library.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
/**
* Find a book in the Liverpool library catalogue
* Written by Adrian McEwen <info@mcqn.com> @amcewen
*/
var url = require("url");
var request = require("request");
var cheerio = require('cheerio');
var LIBRARY_URL = "http://capitadiscovery.co.uk/liverpool/items?query=";
// Look up the book given by isbn in the Liverpool library catalogue.
// If the book is found, call success_callback with the URL to the
// book's page in the catalogue. If it's not found, call the error_callback
function lookup(isbn, callback) {
request(LIBRARY_URL + isbn, function(err, response, body) {
if (err) {
callback(err, undefined);
return;
}
$ = cheerio.load(body);
var r = $("#searchResults .item");
if (r.length) {
var b = r.find("a[itemprop=name]");
callback(null, url.resolve(LIBRARY_URL, b.attr("href")));
} else {
callback(null, undefined);
}
});
}
module.exports = {
lookup: lookup
}