From a82c35be9ac62798b5827754a52643003877e209 Mon Sep 17 00:00:00 2001 From: Chris Wood Date: Fri, 3 Feb 2023 22:22:58 +0000 Subject: [PATCH] Fix Surrey; Sutton has moved to LUCi (#29) * West Sussex moved to Arena v7 * Haringey move to Enterprise * East Lothian has moved to Spydus * Falkirk has moved to Spydus * North Lincolnshire move to Arena v7 * Southampton has removed individual libraries * Fix Surrey and Sutton --- connectors/luci.js | 116 +++++++++++++++++++++++++++++++++++++++ data/data.json | 11 +--- tests/enterprise.test.js | 1 - tests/index.js | 2 + tests/luci.test.js | 3 + tests/tests.json | 6 +- 6 files changed, 127 insertions(+), 12 deletions(-) create mode 100644 connectors/luci.js create mode 100644 tests/luci.test.js diff --git a/connectors/luci.js b/connectors/luci.js new file mode 100644 index 0000000..b7177f9 --- /dev/null +++ b/connectors/luci.js @@ -0,0 +1,116 @@ +const request = require('superagent'); +const common = require('./common'); + +console.log('luci connector loading...') + +/** + * Gets the object representing the service + * @param {object} service + */ +exports.getService = (service) => common.getService(service) + +getLibrariesInternal = async function (service) { + const agent = request.agent() + const response = { + libraries: [] + }; + + try { + let resp = await agent.get(`${service.Url}bookshelf`).timeout(20000); + const frontEndId = /\/_next\/static\/([^\/]+)\/_buildManifest.js/gm.exec(resp.text)[1]; + + resp = await agent.get(`${service.Url}_next/data/${frontEndId}/user/register.json`).timeout(20000); + const libraries = resp.body.pageProps.patronFields.find(x => x.code == 'patron_homeLocation').optionList; + + for(let library of libraries) { + response.libraries.push({ + name: library.value.trim(), + code: library.key.trim() + }); + } + + } catch (e) { + response.exception = e + } + + return response +} + +/** + * Gets the libraries in the service based upon possible search and filters within the library catalogue + * @param {object} service + */ +exports.getLibraries = async function (service) { + const responseLibraries = common.initialiseGetLibrariesResponse(service) + const libs = await getLibrariesInternal(service); + + responseLibraries.exception = libs.exception; + responseLibraries.libraries = libs.libraries.map(x => x.name) + + return common.endResponse(responseLibraries) +} + +/** + * Retrieves the availability summary of an ISBN by library + * @param {string} isbn + * @param {object} service + */ +exports.searchByISBN = async function (isbn, service) { + const responseHoldings = common.initialiseSearchByISBNResponse(service) + + try { + const agent = request.agent(); + let resp = await agent.get(`${service.Url}bookshelf`).timeout(20000); + + const appId = /\?appid=([a-f0-9\-]+)/gm.exec(resp.text)[1]; + + resp = await agent + .post(`${service.Url}api/manifestations/searchresult`) + .send({ + searchTerm: isbn, + searchTarget: '', + searchField: '', + sortField: 'any', + searchLimit: '196', + offset: 0, + facets: [ + { + Name: 'LANGUAGE', + Selected: ['ENG'] + } + ], + count: 40 + }) + .set('Content-Type', 'application/json') + .set('solus-app-id', appId) + .timeout(20000); + + + const result = resp.body.records.find(x => x.isbnList.includes(isbn)); + + if (!result || result.eContent) + return common.endResponse(responseHoldings); + + responseHoldings.id = result.recordID; + responseHoldings.url = `${service.Url}manifestations/${result.recordID}`; + + resp = await agent.get(`${service.Url}api/record?id=${result.recordID}&source=ILSWS`).timeout(20000); + let libraries = resp.body.data.copies.map(x => x.location.locationName); + + // Get unique library values. + libraries = libraries.filter((v, i, s) => s.indexOf(v) === i); + + for (let library of libraries) { + responseHoldings.availability.push({ + library, + available: resp.body.data.copies.filter(x => x.location.locationName == library && x.available).length, + unavailable: resp.body.data.copies.filter(x => x.location.locationName == library && !x.available).length + }); + } + } + catch(e) { + responseHoldings.exception = e + } + + return common.endResponse(responseHoldings) +} diff --git a/data/data.json b/data/data.json index 0a4a5ea..b7924f2 100644 --- a/data/data.json +++ b/data/data.json @@ -1545,19 +1545,14 @@ "Available" ], "TitleDetailUrl": "search/detailnonmodal.detail.detailavailabilityaccordions:lookuptitleinfo/ent:[ITEMID]/ILS/0/true/true", - "Url": "https://llc.ent.sirsidynix.net.uk/client/en_GB/sutton/", + "Url": "https://tlc.ent.sirsidynix.net.uk/client/en_GB/surrey/", "LibraryNameFilter": "Surrey" }, { "Name": "Sutton", "Code": "E09000029", - "Type": "enterprise", - "Available": [ - "Available" - ], - "TitleDetailUrl": "search/detailnonmodal.detail.detailavailabilityaccordions:lookuptitleinfo/ent:[ITEMID]/ILS/0/true/true", - "Url": "https://llc.ent.sirsidynix.net.uk/client/en_GB/sutton/", - "LibraryNameFilter": "Sutton" + "Type": "luci", + "Url": "https://libraries.sutton.gov.uk/" }, { "Name": "Abertawe - Swansea", diff --git a/tests/enterprise.test.js b/tests/enterprise.test.js index 6aee88d..a34c743 100644 --- a/tests/enterprise.test.js +++ b/tests/enterprise.test.js @@ -63,7 +63,6 @@ test('E10000028 - Staffordshire', async () => await index.runTest('Staffordshire test('S12000030 - Stirling', async () => await index.runTest('Stirling'), 300000); test('E08000024 - Sunderland', async () => await index.runTest('Sunderland'), 300000); test('E10000030 - Surrey', async () => await index.runTest('Surrey'), 300000); -test('E09000029 - Sutton', async () => await index.runTest('Sutton'), 300000); test('W06000011 - Abertawe - Swansea', async () => await index.runTest('Abertawe - Swansea'), 300000); test('E06000034 - Thurrock', async () => await index.runTest('Thurrock'), 300000); test('W06000020 - Tor-faen - Torfaen', async () => await index.runTest('Tor-faen - Torfaen'), 300000); diff --git a/tests/index.js b/tests/index.js index 15047a8..893b32d 100644 --- a/tests/index.js +++ b/tests/index.js @@ -2,6 +2,8 @@ const index = require('../index'); const tests = require('./tests.json'); const librariesIgnoreList = [ + // Only has "All Locations" option + 'Southampton' ]; exports.runTest = async (service) => { diff --git a/tests/luci.test.js b/tests/luci.test.js new file mode 100644 index 0000000..1382352 --- /dev/null +++ b/tests/luci.test.js @@ -0,0 +1,3 @@ +const index = require('.'); + +test('E09000029 - Sutton', async () => await index.runTest('Sutton'), 300000); diff --git a/tests/tests.json b/tests/tests.json index d7cc461..7683596 100644 --- a/tests/tests.json +++ b/tests/tests.json @@ -1757,9 +1757,9 @@ "Name": "Sutton", "ISBNs": [ "9780747532743", - "9780141187761", - "9780141439518", - "9780521618748", + "9781405807043", + "9781405862462", + "9781904271338", "9780007371464" ] },