Skip to content

Instantly share code, notes, and snippets.

@cKlee
Last active December 18, 2019 08:05
Show Gist options
  • Save cKlee/7120dd3809b1ae1dfddc5702587b2c45 to your computer and use it in GitHub Desktop.
Save cKlee/7120dd3809b1ae1dfddc5702587b2c45 to your computer and use it in GitHub Desktop.
CSV-Download von ZDB-Titeldaten
var KBART = (function ($) {
var isil,
sep,
calls = [],
lines = [],
searchKey,
type,
abort = false,
media_type = {
'A': 'print',
'B': 'audiovisual',
'C': 'braille',
'E': 'microform',
'O': 'online',
'S': 'electronic'
},
fields = [
"publication_title",
"print_identifier",
"online_identifier",
"date_first_issue_online",
"num_first_vol_online",
"num_first_issue_online",
"date_last_issue_online",
"num_last_vol_online",
"num_last_issue_online",
"title_url",
"first_author",
"title_id",
"embargo_info",
"coverage_depth",
"notes",
"publisher_name",
"publication_type",
"date_monograph_published_print",
"date_monograph_published_online",
"monograph_volume",
"monograph_edition",
"first_editor",
"parent_publication_title_id",
"preceding_publication_title_id",
"access_type",
"package_id",
"zdb_id",
"media_type"
];
return {
setIsil: function (i) {
isil = i;
if (-1 !== isil.indexOf('ZDB')) {
this.setSearchKey('psg');
} else {
this.setSearchKey('isil');
}
},
setSeparator: function (s) {
sep = s;
if (s == "\\t") {
sep = '\t'
}
},
setSearchKey: function (k) { searchKey = k },
setType: function (t) { type = t },
addHeader: function () {
lines = [];
addLine(fields);
},
abortRequest: function () {
abort = true;
},
createCSVLink: function () {
$('#csv-ready').hide();
$('#csv-wait').show();
$('#csv-abort').show();
var req = 'https://www.zeitschriftendatenbank.de/api/hydra/?size=20&q=' + encodeURI(searchKey + '=' + isil);
//var req = 'https://zdb.cms-test.sbb.spk-berlin.de/api/?type=4441&size=20&q=' + encodeURI(searchKey + '=' + isil);
if (typeof type !== 'undefined') {
req += encodeURI(' AND frm=' + type);
}
//req += '&callback=?';
calls.push(zdbRequest(req));
}
}
function addLine(line) {
lines.push(line.join(sep));
}
function zdbRequest(req) {
return $.getJSON(req, function (data) {
if (data.hasOwnProperty('view') && data['view'].hasOwnProperty('next') && !abort) {
calls.push(zdbRequest(data['view']['next']));
} else if (data['type'] == 'Error') {
return false;
}
}).done(function (data) {
buildLine(data['member']);
calls.pop();
}).fail(function (jqxhr, textStatus, err) {
let data = JSON.parse(jqxhr.responseText);
let error = data['description'];
$('#csv-ready').html('<b>Es ist ein Fehler aufgetreten: <i>' + error + '</i>. Die Liste konnte nicht erstellt werden. Bitte versuchen Sie es später noch einmal.</b>');
$('#csv-wait').hide();
$('#csv-abort').hide();
$('#csv-ready').show();
}).always(function (data) {
if (0 === data['totalItems']) {
$('#csv-wait').html('<b>Unter dem ISIL ' + isil + ' konnte nichts gefunden werden.</b>');
return false;
}
if (calls.length == 0) {
let data_uri = 'data:text/csv;charset=utf-8,%EF%BB%BF' + encodeURIComponent(lines.join("\n"));
let date = new Date();
let filename = isil + '-' + date.getFullYear() + '_' + date.getMonth() + 1 + '_' + date.getDate() + '-' + date.getTime() + '.csv';
$('#csv-ready').html('<b>Die KBART-Liste wurde erstellt:<br><a class="intern" download="' + filename + '" href="' + data_uri + '">' + filename + '</a></b>');
$('#csv-wait').hide();
$('#csv-abort').hide();
$('#csv-ready').show();
download(data_uri, filename, 'text/csv');
} else {
$('#csv-wait').html('<b>Lade CSV-Liste ... Das kann einen Moment dauern ... Seite ' + data['view']['pageIndex'] + ' von ' + data['view']['numberOfPages'] + '</b>');
};
});
}
function buildLine(records) {
var values = function (kat, codes) {
var vals,
kat_occ = [];
if (typeof kat === 'undefined') {
return [];
}
// for each occurence
kat.forEach(
function (occ) {
vals = {};
// for each sf position
occ.forEach(
function (sf_pos) {
codes.forEach(
function (code) {
if (sf_pos.hasOwnProperty(code)) {
vals[code] = [];
//console.log(code + ': ' + sf_pos[code]);
if (-1 !== sf_pos[code].indexOf(sep)) {
vals[code].push('"' + sf_pos[code].replace(/\\([\s\S])|(")/g, "\\$1$2") + '"');
} else {
vals[code].push(sf_pos[code]);
}
}
}
);
}
);
kat_occ.push(vals);
}
);
//console.log(kat_occ);
return kat_occ;
},
addAtLineIndex = function (line, key, val) {
let index = fields.indexOf(key);
if (typeof val !== 'undefined') {
line[index] = val;
} else {
line[index] = '';
}
}
records.forEach(function (record) {
let rec = record['data'];
let zdbid = record['identifier'];
let rectype_0 = (rec.hasOwnProperty('002@')) ? values(rec['002@'], ['0']) : [];
let rectype = rectype_0[0]['0'][0][0];
if (typeof type !== 'undefined') {
if (-1 == type.indexOf(rectype)) {
return;
}
}
let line = [],
title = (rec.hasOwnProperty('021A')) ? values(rec['021A'], ['a']) : [],
issn = (rec.hasOwnProperty('005A')) ? values(rec['005A'], ['0']) : [],
issn_parallel = (rec.hasOwnProperty('005P')) ? values(rec['005P'], ['S', '0']) : [],
ersch_verlauf = (rec.hasOwnProperty('031N')) ? values(rec['031N'], ['d', 'e', 'j', 'n', 'o', 'k']) : [],
url = (rec.hasOwnProperty('009Q')) ? values(rec['009Q'], ['u', 'x']) : [],
//zdbid = (rec.hasOwnProperty('006Z')) ? values(rec['006Z'], ['0']) : [],
notes = (rec.hasOwnProperty('037A')) ? values(rec['037A'], ['a']) : [],
notes_o = (rec.hasOwnProperty('046E')) ? values(rec['046E'], ['a']) : [],
ort_verlag = (rec.hasOwnProperty('033A')) ? values(rec['033A'], ['p', 'n']) : [];
//person = (rec.hasOwnProperty('028A')) ? values(rec['028A'], ['8']) : [];
// Immer leer
addAtLineIndex(line, "first_author", '');
addAtLineIndex(line, "embargo_info", '');
addAtLineIndex(line, "coverage_depth", '');
addAtLineIndex(line, "date_monograph_published_print", '');
addAtLineIndex(line, "date_monograph_published_online", '');
addAtLineIndex(line, "monograph_volume", '');
addAtLineIndex(line, "monograph_edition", '');
addAtLineIndex(line, "first_editor", '');
addAtLineIndex(line, "parent_publication_title_id", '');
addAtLineIndex(line, "preceding_publication_title_id", '');
addAtLineIndex(line, "access_type", '');
// Default Value
addAtLineIndex(line, "publication_type", 'serial');
addAtLineIndex(line, "package_id", isil);
addAtLineIndex(line, "zdb_id", zdbid);
addAtLineIndex(line, "title_id", zdbid.substring(0, zdbid.length - 3));
addAtLineIndex(line, "media_type", media_type[rectype]);
// Medientyp
// Titel
addAtLineIndex(line, "publication_title", title[0]['a'][0].replace('@', ''));
// ISSN
if (issn.length > 0 && issn[0].hasOwnProperty('0')) {
if ('A' == rectype) {
addAtLineIndex(line, "print_identifier", issn[0]['0'][0]);
addAtLineIndex(line, "online_identifier", '');
} else if ('O' == rectype) {
addAtLineIndex(line, "print_identifier", '');
addAtLineIndex(line, "online_identifier", issn[0]['0'][0]);
} else {
addAtLineIndex(line, "print_identifier", '');
addAtLineIndex(line, "online_identifier", '');
}
} else {
addAtLineIndex(line, "print_identifier", '');
addAtLineIndex(line, "online_identifier", '');
}
if (issn_parallel.length > 0) {
if (-1 !== issn_parallel[0]['S'].indexOf('p')) {
addAtLineIndex(line, "print_identifier", issn_parallel[0]['0'][0]);
} else if (-1 !== issn_parallel[0]['S'].indexOf('o')) {
addAtLineIndex(line, "online_identifier", issn_parallel[0]['0'][0]);
}
}
// Erscheinungsverlauf
if (ersch_verlauf.length > 0) {
if ('O' == rectype) {
// Beginn Jahr
if (ersch_verlauf[0].hasOwnProperty('j')) {
addAtLineIndex(line, "date_first_issue_online", ersch_verlauf[0]['j'][0]);
} else {
addAtLineIndex(line, "date_first_issue_online", '');
}
// Beginn Band
if (ersch_verlauf[0].hasOwnProperty('d')) {
addAtLineIndex(line, "num_first_vol_online", ersch_verlauf[0]['d'][0]);
} else {
addAtLineIndex(line, "num_first_vol_online", '');
}
// Beginn Heft
if (ersch_verlauf[0].hasOwnProperty('e')) {
addAtLineIndex(line, "num_first_issue_online", ersch_verlauf[0]['e'][0]);
} else {
addAtLineIndex(line, "num_first_issue_online", '');
}
// Ende Jahr
if (ersch_verlauf[0].hasOwnProperty('k')) { // immer letzte Gruppe
addAtLineIndex(line, "date_last_issue_online", ersch_verlauf[0]['k'][ersch_verlauf[0]['k'].length - 1]);
} else {
addAtLineIndex(line, "date_last_issue_online", '');
}
// Ende Band
if (ersch_verlauf[0].hasOwnProperty('n')) { // immer letzte Gruppe
addAtLineIndex(line, "num_last_vol_online", ersch_verlauf[0]['n'][ersch_verlauf[0]['n'].length - 1]);
} else {
addAtLineIndex(line, "num_last_vol_online", '');
}
// Ende Heft
if (ersch_verlauf[0].hasOwnProperty('o')) { // immer letzte Gruppe
addAtLineIndex(line, "num_last_issue_online", ersch_verlauf[0]['o'][ersch_verlauf[0]['o'].length - 1]);
} else {
addAtLineIndex(line, "num_last_issue_online", '');
}
}
} else {
addAtLineIndex(line, "date_first_issue_online", '');
addAtLineIndex(line, "num_first_vol_online", '');
addAtLineIndex(line, "num_first_issue_online", '');
addAtLineIndex(line, "date_last_issue_online", '');
addAtLineIndex(line, "num_last_vol_online", '');
addAtLineIndex(line, "num_last_issue_online", '');
}
// URL
if (url.length > 0) {
// some returns false by default
url.some(
function (u) {
if (-1 !== u['x'][0].indexOf('H')) {
addAtLineIndex(line, "title_url", u['u'][0]);
return true; // true exits the some-loop
}
if (-1 !== u['x'][0].indexOf('G')) {
addAtLineIndex(line, "title_url", u['u'][0]);
return true; // true exits the some-loop
}
if (-1 !== u['x'][0].indexOf('A')) {
addAtLineIndex(line, "title_url", u['u'][0]);
return true; // true exits the some-loop
}
if (-1 !== u['x'][0].indexOf('F')) {
addAtLineIndex(line, "title_url", u['u'][0]);
return true; // true exits the some-loop
}
addAtLineIndex(line, "title_url", '');
}
);
} else {
addAtLineIndex(line, "title_url", '');
}
// Verlag
if (ort_verlag.length > 0 && ort_verlag[0].hasOwnProperty('n')) {
addAtLineIndex(line, "publisher_name", ort_verlag[0]['n'][0]);
} else {
addAtLineIndex(line, "publisher_name", '');
}
// Notes
if ('O' == rectype) {
if (notes_o.length > 0) {
addAtLineIndex(line, "notes", notes_o[0]['a'][0]);
} else if (notes.length > 0) {
addAtLineIndex(line, "notes", notes[0]['a'][0]);
} else {
addAtLineIndex(line, "notes", '');
}
} else {
if (notes.length > 0) {
addAtLineIndex(line, "notes", notes[0]['a'][0]);
} else {
addAtLineIndex(line, "notes", '');
}
}
addLine(line);
});
}
})(jQuery);
<script type="application/javascript" src="/fileadmin/user_upload/ZDB/js/csv/csv-zdb.js"></script>
<script type="application/javascript" src="/fileadmin/user_upload/Sigelstelle/js/csv/download.min.js"></script>
<div style="background-color: #f2f2f2;border: 1px solid #cfcecf;padding: 10px 0px 0px 10px; margin-top: 9px;">
<label style="display:inline" for="csv-isil"><b>ISIL:</b></label> <input style="display:inline" type="text" id="csv-isil"size="50">
<p style="margin-bottom:9px"><b>Einschränkung auf Medientyp:</b><br/>
<input style="display:inline" type="radio" id="csv-all" name="csv-type" value="" checked="checked" /> <label style="display:inline" for="csv-all">keine Einschränkung</label><br/>
<input style="display:inline" type="radio" id="csv-print" name="csv-type" value="A" /> <label style="display:inline" for="csv-print">nur Printausgaben</label> <br/>
<input style="display:inline" type="radio" id="csv-online" name="csv-type" value="O" /> <label style="display:inline" for="csv-online">nur Online-Ausgaben</label>
</p>
<p style="margin-bottom:9px"><b>Werte getrennt durch:</b><br/>
<input style="display:inline" type="radio" id="csv-semicolon" name="csv-seperator" value=";" checked="checked" /> <label style="display:inline" for="csv-semicolon">Semikolon</label> <br/>
<input style="display:inline" type="radio" id="csv-comma" name="csv-seperator" value="," /> <label style="display:inline" for="csv-comma">Komma</label> <br/>
<input style="display:inline" type="radio" id="csv-tab" name="csv-seperator" value="\t" /> <label style="display:inline" for="csv-tab">Tab</label>
</p>
</div>
<br>
<button type="button" style="border: 1px solid #356AAC; color: #356AAC; background-color: #e6f2ff; margin-top:9px" id="csv-create">Erstelle CSV-Liste</button>
<div id="csv-wait" style="display: none;margin-top: 9px;"><b>Lade CSV-Liste ... Das kann einen Moment dauern ...</b></div>
<div id="csv-abort" style="display: none;margin-top: 9px;"><button type="button" style="border: 1px solid #356AAC; color: #356AAC; background-color: #e6f2ff; margin-top:9px" id="csv-abort-botton">Erstellung abbrechen</button></div>
<div id="csv-ready" style="display: none;margin-top: 9px;"></div>
<script>
$('#csv-create').click(function() {
let isil = $('#csv-isil').val(),
sep = $('input[name="csv-seperator"]:checked').val(),
//key = $('input[name="csv-mode"]:checked').val(),
type = $('input[name="csv-type"]:checked').val();
KBART.setIsil(isil);
KBART.setSeparator(sep);
//KBART.setSearchKey(key);
if('' !== type) {
KBART.setType(type);
}
KBART.addHeader();
KBART.createCSVLink();
});
$('#csv-abort-botton').click(function() {
KBART.abortRequest();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment