From: Dan Scott Date: Sat, 24 Feb 2018 15:31:07 +0000 (-0500) Subject: Realtime Wikipedia description lookups X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=refs%2Fheads%2Fwikidata_music_card_refined;p=contrib%2FConifer.git Realtime Wikipedia description lookups These are English-only but provide the first sentence of the Wikipedia description of the performer, as well as a link to the related Wikipedia page. Signed-off-by: Dan Scott --- diff --git a/Open-ILS/web/js/ui/default/opac/wikidata_music_card.js b/Open-ILS/web/js/ui/default/opac/wikidata_music_card.js index 40d7e73e5a..eac375f4da 100644 --- a/Open-ILS/web/js/ui/default/opac/wikidata_music_card.js +++ b/Open-ILS/web/js/ui/default/opac/wikidata_music_card.js @@ -160,18 +160,38 @@ musician.appendChild(wdd); } + // Get the description from Wikipedia if (r.hasOwnProperty('wplink')) { + wpapi = 'https://en.wikipedia.org/w/api.php?origin=*&format=json&action=query&prop=extracts&explaintext=true&exintro=true&titles='; var wplink = r.wplink.value; // Strip the WP title from the link - // https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext=true&exintro=true&titles=CANO - var description = ''; // extract text from wplink - var wdd = document.createElement('div'); - var wddl = document.createElement('label'); - wddl.innerText = description; - wdd.appendChild(wddl); - musician.appendChild(wdd); - } + wptitle = wplink.substring(wplink.lastIndexOf('/') + 1); + var req = new window.XMLHttpRequest(); + req.open('GET', wpapi + wptitle); + req.onload = function (evt) { + var r = JSON.parse(req.response); + var k = Object.getOwnPropertyNames(r.query.pages)[0]; + var description = r.query.pages[k].extract; + + // Just the first line will do + var linefeed = description.indexOf('\n'); + var period = description.indexOf('. '); + if (linefeed > 0) { + description = description.substring(0, linefeed); + } else if (period > 0) { + description = description.substring(0, period + 1); + } + var wdd = document.createElement('p'); + var wpa = document.createElement('a'); + wpa.href = wplink; + wpa.innerText = '(Wikipedia)'; + wdd.innerText = description + ' '; + wdd.appendChild(wpa); + musician.appendChild(wdd); + } + req.send(); + } function addWDValue(property, propertyLabel, label, isLink, linkFormatter) { var value = '';