Skip to content
Permalink
Browse files

Rewrite Library Catalogue Encore (zotero#1482)

  • Loading branch information...
adam3smith authored and zuphilip committed Nov 25, 2017
1 parent 80bd37d commit 3b08a480de0b7391fa279c7281b6f597256ee5b6
Showing with 145 additions and 173 deletions.
  1. +145 −173 Library Catalog (Encore).js
@@ -3,20 +3,19 @@
"label": "Library Catalog (Encore)",
"creator": "Sebastian Karcher",
"target": "/iii/encore/(record|search)",
"minVersion": "1.0",
"minVersion": "3.0",
"maxVersion": "",
"priority": 270,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsb",
"lastUpdated": "2014-08-26 03:59:48"
"browserSupport": "gcsibv",
"lastUpdated": "2017-11-25 15:51:49"
}

/*
***** BEGIN LICENSE BLOCK *****
Encore Library Catalog Translator
Copyright © 2011 Sebastian Karcher and CHNM
Copyright © 2017 Sebastian Karcher
This file is part of Zotero.
@@ -36,204 +35,177 @@
***** END LICENSE BLOCK *****
*/

// attr()/text() v2
function attr(docOrElem,selector,attr,index){var elem=index?docOrElem.querySelectorAll(selector).item(index):docOrElem.querySelector(selector);return elem?elem.getAttribute(attr):null;}function text(docOrElem,selector,index){var elem=index?docOrElem.querySelectorAll(selector).item(index):docOrElem.querySelector(selector);return elem?elem.textContent:null;}

function detectWeb(doc, url){
var bibIdRe = new RegExp("encore/record");
if (bibIdRe.test(url)){
return "book";
}

var bibIdSearch = new RegExp("encore/search");
if (bibIdSearch.test(url)){
function detectWeb(doc, url) {
if (url.includes("encore/record")) {
return "book";
} else if (url.includes("encore/search")) {
return "multiple";
}
}




function doWeb(doc, url) {
var uri = doc.location.href;
var newUri;
// load translator for MARC
var translator = Zotero.loadTranslator("import");
translator.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973");
translator.getTranslatorObject(function(marc) {
if (detectWeb(doc, url) == "book") {
newUri = uri.replace(/\?/, "?marcData=Y&");
pageByPage(marc, [newUri]);
} else { // Search results page
// Require link to match this
var tagRegexp = new RegExp();
tagRegexp.compile('^https?://[^/]+/search\\??/[^/]+/[^/]+/[0-9]+\%2C[^/]+/frameset');

var urls = new Array();
var availableItems = {};
var firstURL = false;

var tableRows = doc.evaluate('//td[@class="browseResultContent" or @class="itemTitleCell"]|//div[contains(@class,"searchResult") and contains(@class, "Browse")] ',
doc, null, XPathResult.ANY_TYPE, null);
// Go through table rows
var i = 0;
while(tableRow = tableRows.iterateNext()) {
// get link
var links = ZU.xpath(tableRow, './/*[@class="dpBibTitle"]/span/a');
if (links.length==0) links = ZU.xpath(tableRow, './/*[@class="dpBibTitle"]/a');
for (var i=0; i<links.length; i++) {
if(availableItems[links[i].href]) {
continue;
}
if (links[i].textContent.match(/\w+/)){
availableItems[links[i].href] = links[i].textContent.trim();}
}
};
Zotero.selectItems(availableItems, function (items) {
if(!items) {
return true;
}

var newUrls = new Array();
for(var itemURL in items) {
newUrls.push(itemURL.replace("?", "?marcData=Y&"));
}
pageByPage(marc, newUrls);
});
}
});
}



//functions:
function scrape(marc, newDoc) {

var xpath = '//pre/text()';
if (newDoc.evaluate(xpath, newDoc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
var elmts = newDoc.evaluate(xpath, newDoc, null, XPathResult.ANY_TYPE, null);
var useNodeValue = true;
} else {
var elmts = newDoc.evaluate('//pre', newDoc, null, XPathResult.ANY_TYPE, null);
var useNodeValue = false;
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = doc.querySelectorAll('a[id^=recordDisplayLink2Component]');
for (let i = 0; i < rows.length; i++) {
let href = createMarcURL(rows[i].href);
let title = ZU.trimInternal(rows[i].textContent);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
return found ? items : false;
}

var elmt;
while(elmt = elmts.iterateNext()) {
if (useNodeValue) {
var text = elmt.nodeValue;
} else {
var text = elmt.textContent;
}
var newItem = new Zotero.Item();
var record = new marc.record();

var linee = text.split("\n");
for (var i=0; i<linee.length; i++) {
if(!linee[i]) {
continue;
}

linee[i] = linee[i].replace(/[\xA0_\t]/g, " ");
var value = linee[i].substr(7);

if(linee[i].substr(0, 6) == " ") {
// add this onto previous value
tagValue += value;
} else {
if(linee[i].substr(0, 6) == "LEADER") {
// trap leader
record.leader = value;
} else {
if(tagValue) { // finish last tag
tagValue = tagValue.replace(/\|(.)/g, marc.subfieldDelimiter+"$1");
if(tagValue[0] != marc.subfieldDelimiter) {
tagValue = marc.subfieldDelimiter+"a"+tagValue;
}

// add previous tag
record.addField(tag, ind, tagValue);
}

var tag = linee[i].substr(0, 3);
var ind = linee[i].substr(4, 2);
var tagValue = value;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) == "multiple") {
Zotero.selectItems(getSearchResults(doc, false), function(items) {
if (!items) {
return true;
}
}
if(tagValue) {
tagValue = tagValue.replace(/\|(.)/g, marc.subfieldDelimiter+"$1");
if(tagValue[0] != marc.subfieldDelimiter) {
tagValue = marc.subfieldDelimiter+"a"+tagValue;
var articles = [];
for (var i in items) {
articles.push(i);
}

// add previous tag
record.addField(tag, ind, tagValue);
}

record.translate(newItem);
//the library catalogue name isn't perfect, but should be unambiguous.
var domain = newDoc.location.href.match(/https?:\/\/([^/]+)/);
newItem.repository = domain[1].replace(/encore\./, "")+" Library Catalog";
// there is too much stuff in the note field - or file this as an abstract?
newItem.notes = [];
newItem.complete();
scrape(articles);
});
} else {
var marcURL = createMarcURL(url);
scrape([marcURL]);
}
}

function pageByPage(marc, urls) {
Zotero.Utilities.processDocuments(urls, function(newDoc) {
scrape(marc, newDoc);
}, function() { Zotero.done() });
function createMarcURL(url) {
//construct the marc URL
return url.replace(/\?/, "?marcData=Y&");
}

/** BEGIN TEST CASES **/
function scrape(marcURL) {
for (let i = 0; i < marcURL.length; i++) {
//Z.debug(marcURL[i])
// the library catalogue name isn't perfect, but should be unambiguous.
var domain = marcURL[i].match(/https?:\/\/([^/]+)/);
ZU.doGet(marcURL[i], function(text) {
var translator = Zotero.loadTranslator("import");
translator.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973");
translator.getTranslatorObject(function(marc) {
var record = new marc.record();
var newItem = new Zotero.Item();
text = text.replace(/^\n/mg, '') // skip empty lines
.replace(/\s?\n\s+/gm, ' '); // delete line breaks when only needed for displaying long lines
//Z.debug(text);
var line = text.split("\n");
for (var j = 0; j < line.length; j++) {
line[j] = line[j].replace(/[\xA0_\t]/g, " ");
var value = line[j].substr(7);
if (line[j].substr(0, 6) == "LEADER") {
// trap leader
record.leader = value;
} else {
var tag = line[j].substr(0, 3);
var ind = line[j].substr(4, 2);
if (value) {
value = value.replace(/\|(.)/g, marc.subfieldDelimiter + "$1");
if (value[0] != marc.subfieldDelimiter) {
value = marc.subfieldDelimiter + "a" + value;
}
record.addField(tag, ind, value);
}
}
}

record.translate(newItem);
newItem.repository = domain[1].replace(/encore\./, "");
// there is too much stuff in the note field - or file this as an abstract?
newItem.notes = [];
newItem.complete();
});
});
}
}/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://encore.colorado.edu/iii/encore/search?formids=target&lang=eng&suite=def&reservedids=lang%2Csuite&submitmode=&submitname=&target=thelen&Search.x=0&Search.y=0",
"items": "multiple"
},
{
"type": "web",
"url": "http://encore.coalliance.org/iii/encore/search/C|Sthelen|Orightresult|U1?lang=eng",
"items": "multiple"
},
{
"type": "web",
"url": "http://encore.colorado.edu/iii/encore/record/C__Rb5910060__Sthelen__P0%2C3__Orightresult__X4?lang=eng&suite=cobalt",
"url": "http://sallypro.sandiego.edu/iii/encore/record/C__Rb4044553__Stesting__P0%2C6__Orightresult__U__X6?lang=eng&suite=cobalt",
"items": [
{
"itemType": "book",
"title": "Evaluation and testing in nursing education",
"creators": [
{
"firstName": "Marcel",
"lastName": "Thelen",
"creatorType": "editor"
"firstName": "Marilyn H.",
"lastName": "Oermann",
"creatorType": "author"
},
{
"firstName": "F.",
"lastName": "Steurs",
"creatorType": "editor"
"firstName": "Kathleen B.",
"lastName": "Gaberson",
"creatorType": "author"
}
],
"notes": [],
"date": "2017",
"ISBN": "9780826194886",
"abstractNote": "Considered the \"gold standard' for evaluation and testing in nursing education, this classic text helps educators to assess the level of learning achieved in the classroom, clinical settings, and online. The fifth edition helps teachers to keep pace with new learning dynamics through expanded coverage of essential concepts in assessment, evaluation, and testing in a wider variety of learning environments. It presents new content on evaluation in online programs and testing, and features a new chapter on using simulation for assessment and high stakes evaluations. Also included is updated information on clinical evaluation and program evaluation along with current research featuring new examples and tools. The fifth edition expands content on standardized tests, including how to write test items for licensure and certification exam prep, and provides new information on developing rubrics for assessing written assignments. -- Provided by publisher",
"callNumber": "RT73.7 .O47 2017",
"edition": "Fifth edition",
"extra": "OCLC: 957077777",
"libraryCatalog": "sallypro.sandiego.edu",
"numPages": "403",
"place": "New York, NY",
"publisher": "Springer Publishing Company, LLC",
"attachments": [],
"tags": [
"Language and languages",
"Terms and phrases"
{
"tag": "Ability testing"
},
{
"tag": "Ability testing"
},
{
"tag": "Examinations"
},
{
"tag": "Examinations"
},
{
"tag": "Nursing"
},
{
"tag": "Nursing"
},
{
"tag": "Nursing"
},
{
"tag": "Nursing"
},
{
"tag": "Nursing"
},
{
"tag": "Nursing"
},
{
"tag": "Study and teaching Evaluation"
},
{
"tag": "Study and teaching Evaluation"
}
],
"seeAlso": [],
"attachments": [],
"ISBN": "9789027223371",
"title": "Terminology in everyday life",
"place": "Amsterdam, The Netherlands : Philadelphia, Pa",
"publisher": "John Benjamins Pub. Co",
"date": "2010",
"numPages": "271",
"series": "Terminology and lexicography research and practice",
"seriesNumber": "v. 13",
"callNumber": "P305 .T4437 2010",
"libraryCatalog": "colorado.edu Library Catalog"
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://sallypro.sandiego.edu/iii/encore/search/C__Rb3558162__Stesting__Orightresult__U__X6?lang=eng&suite=cobalt#resultRecord-b3558162",
"items": "multiple"
}
]
/** END TEST CASES **/
/** END TEST CASES **/

0 comments on commit 3b08a48

Please sign in to comment.
You can’t perform that action at this time.