This repository has been archived by the owner on Jan 3, 2020. It is now read-only.
forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary Catalog (Polaris).js
177 lines (144 loc) · 4.37 KB
/
Library Catalog (Polaris).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
{
"translatorID": "12541207-ed80-4b59-9d46-fafa3aa61f7f",
"label": "Library Catalog (Polaris)",
"creator": "Aurimas Vinckevicius",
"target": "/polaris/search/(searchresults|title)\\.aspx\\?",
"minVersion": "3.0",
"maxVersion": "",
"priority": 250,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2015-02-19 04:55:29"
}
/*
Polaris Library Catalog Translator
Copyright (C) 2015 Aurimas Vinckevicius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** Sample catalogs
* http://pac.kings.edu/polaris/
* http://librarycatalog.eugene-or.gov/Polaris/
* http://www.clan.lib.nv.us/polaris/
*/
function detectWeb(doc, url) {
if (url.indexOf('title.aspx') != -1 && getPos(url) !== null) {
return getItemType(doc);
}
if (url.indexOf('searchresults.aspx') != -1 && getSearchResults(doc, true)) {
return 'multiple'
}
}
/**
* Extract pos value from URL
*/
function getPos(url) {
var m = url.match(/[?&]pos=(\d+)/);
if (!m) return null;
return m[1];
}
function getItemType(doc) {
var type = ZU.xpath(doc, '//td[@class="nsm-full-label" and starts-with(text(),"Format")]/following-sibling::td');
if (!type.length) return false;
if (type.length != 1) {
Zotero.debug("Item type detection matched multiple nodes!!");
}
type = type[0].textContent.toLowerCase();
if (/\b(?:book|printed music)\b/.test(type)) {
return 'book';
}
if (/\b(?:sound recording|music)\b/.test(type)) {
return 'audioRecording';
}
if (/\b(?:dvd|videorecording)\b/.test(type)) {
return 'film';
}
if (/\b(?:map)\b/.test(type)) {
return 'map';
}
return 'document';
}
function getSearchResults(doc, checkOnly) {
var titles = doc.getElementsByClassName('nsm-brief-primary-title-group');
var items = {},
found = false;
for (var i=0; i<titles.length; i++) {
var link = titles[i].getElementsByTagName('a')[0];
if (!link) continue;
var title = ZU.trimInternal(link.textContent);
var pos = getPos(link.href);
if (!title || pos == null) continue;
if (checkOnly) return true;
found = true;
items[pos] = title;
}
return checkOnly ? false : items;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) == 'multiple') {
Z.selectItems(getSearchResults(doc), function(items) {
if (!items) return true;
var pos = [];
for (var i in items) {
pos.push(i);
}
scrape(pos);
})
} else {
scrape([getPos(url)])
}
}
function scrape(pos) {
pos = pos.map(function(p) {
return '/polaris/search/components/ajaxMARC.aspx?fp=0&pos=' + p;
});
var translator = Zotero.loadTranslator("import");
// MARC
translator.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973");
translator.getTranslatorObject(function(marc) {
ZU.processDocuments(pos, function(doc) {
var record = new marc.record();
var lines = doc.getElementsByTagName('tr');
for (var i=0; i<lines.length; i++) {
var tag = lines[i].children[0];
var ind = lines[i].children[1];
var data = lines[i].children[2];
if (!data) continue; // must have all three or else... something went wrong
tag = tag.textContent.trim();
ind = ind.textContent;
switch(tag) {
case 'LDR':
record.leader = data.textContent;
continue;
case 'FMT': // Not sure if this is relevant to Polaris
case '':
continue;
default:
var textData = '';
for (var j=0; j<data.childNodes.length; j++) {
var child = data.childNodes[j];
if (child.classList && child.classList.contains('marc_sub')) {
textData += marc.subfieldDelimiter
+ child.textContent.trim().substr(1); // Drop $
} else {
textData += child.textContent;
}
}
record.addField(tag, ind, textData);
}
}
var newItem = new Zotero.Item();
record.translate(newItem);
newItem.complete();
})
});
}