Created
April 15, 2014 12:33
-
-
Save anonymous/10728761 to your computer and use it in GitHub Desktop.
Opals Zotero Translator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"translatorID": "83501b8c-1033-4722-ae50-a77d67271ef7", | |
"label": "Library Catalog (OPALS)", | |
"creator": "Opals", | |
"target": "", | |
"minVersion": "1.0.0", | |
"maxVersion": "", | |
"priority": 100, | |
"inRepository": true, | |
"translatorType": 4, | |
"browserSupport": "gcsib", | |
"lastUpdated": "2014-04-11 15:55:46" | |
} | |
//============================================================================== | |
// Opals Translator | |
//============================================================================== | |
function detectWeb(doc, url) { | |
if(url.match(/bin\/eqmnt\/|bin\/txtbk\//)) | |
return""; | |
var ridList=getRidTitleList(doc); | |
var count =ridList.length; | |
if(count==1) return "book"; | |
else if(count>1) return "multiple"; | |
} | |
//============================================================================== | |
function doWeb(doc, url) { | |
var titles=getRidTitleList(doc); | |
var items={}; | |
for(var i=0; i<titles.length;i++){ | |
items[titles[i].rid]=titles[i].title; | |
} | |
if(titles.length==1){ | |
scrape(titles[0].rid,url); | |
} | |
else if(titles.length >1){ | |
Zotero.selectItems(items, function (items) { | |
if (!items) { | |
return true; | |
} | |
for (var rid in items) { | |
scrape(rid,url); | |
} | |
}); | |
} | |
} | |
//============================================================================== | |
function scrape(rid,url){ | |
var marcurl="/bin/ajax/getMarc?format=usmarc&rid="+rid; | |
var baseUrl=getUrlBase(url); | |
Zotero.Utilities.HTTP.doGet(marcurl, function (text) { | |
var translator = Zotero.loadTranslator("import"); | |
translator.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973"); | |
translator.setString(text); | |
translator.setHandler("itemDone", function (obj, item) { | |
// adding record URL | |
item.url=baseUrl +"/bin/search/recDetailPage?rid=" + rid; | |
item.complete(); | |
}); | |
translator.translate(); | |
}); | |
} | |
//============================================================================== | |
function getUrlBase(url){ | |
if(rs =url.match(/(http[s]*:\/\/(.*?))\//)){ | |
return rs[1]; | |
} | |
return null; | |
} | |
//============================================================================== | |
function getRidTitleList(doc){ | |
var items=[]; | |
var inputs = doc.evaluate('//input[@name="bib_rid"]', doc, null, XPathResult.ANY_TYPE, null); | |
while (chkBox = inputs.iterateNext()) { | |
var title=chkBox.getAttribute("bib_title").trim(); | |
var rid=chkBox.value.trim() ; | |
if(rid && rid.match(/^[1-9][0-9]*$/) && title){ | |
items.push({rid:rid,title:title}); | |
} | |
} | |
return items; | |
} | |
//============================================================================== | |
/** BEGIN TEST CASES**/ | |
var testCases=[ | |
{ "type":"web", | |
"url":"http://cogent.opalsinfo.net/bin/search/recDetailPage?rid=68738"; | |
"items": | |
[ | |
{ | |
"itemType": "book", | |
"creators": [ | |
{ | |
"firstName": "Jay", | |
"lastName": "Wertz", | |
"creatorType": "author" | |
} | |
], | |
"notes": [ | |
{ | |
"note": "Item cannot be cataloged" | |
}, | |
{ | |
"note": "Chronicles the history of Native Americans from the Ice Age to the early twenty-first century, and includes more than thirty slip-cased facsimile documents" | |
} | |
], | |
"tags": [], | |
"seeAlso": [], | |
"attachments": [], | |
"ISBN": "159921475X", | |
"title": "The Native American experience", | |
"place": "Guilford, Conn.", | |
"publisher": "Lyons Press", | |
"date": "2008", | |
"callNumber": "000 WER", | |
"url": "http://cogent.opalsinfo.net/bin/search/recDetailPage?rid=68738", | |
"libraryCatalog": "Library Catalog (OPALS)", | |
"accessDate": "CURRENT_TIMESTAMP" | |
} | |
] | |
}, | |
{ "type":"web", | |
"url":"http://cogent.opalsinfo.net/bin/search/recDetailPage?rid=68739"; | |
"items": | |
[ | |
{ | |
"itemType": "book", | |
"creators": [ | |
{ | |
"firstName": "Evan T.", | |
"lastName": "Pritchard", | |
"creatorType": "author" | |
} | |
], | |
"notes": [ | |
{ | |
"note": "Chronicles the history of the Algonquin people in the area that is now New York, discussing the impact their culture had on the development of New York" | |
} | |
], | |
"tags": [ | |
"Algonquian Indians", | |
"Native Americans", | |
"New York (State)" | |
], | |
"seeAlso": [], | |
"attachments": [], | |
/** END TEST CASES**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment