Realtime Wikipedia description lookups wikidata_music_card_refined
authorDan Scott <dscott@laurentian.ca>
Sat, 24 Feb 2018 15:31:07 +0000 (10:31 -0500)
committerDan Scott <dscott@laurentian.ca>
Sat, 24 Feb 2018 15:31:07 +0000 (10:31 -0500)
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 <dscott@laurentian.ca>
Open-ILS/web/js/ui/default/opac/wikidata_music_card.js

index 40d7e73..eac375f 100644 (file)
       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 = '';