Wikidata:WikidataCon 2017/Notes/WikidataJS: query, parse, and edit Wikidata data in Javascript

From Wikidata
Jump to navigation Jump to search

Title: WikidataJS: query, parse, and edit Wikidata data in Javascript

Note-taker(s): Lucas

Speaker(s)[edit]

Name or username: maxlath

Contact (email, Twitter, etc.): max@maxlath.eu - @maxlath

Useful links:

Abstract[edit]

For the needs of inventaire.io server (Q32844021), we developed a few tools to work with Wikidata data in JavaScript (Q2005), the main results are:

Collaborative notes of the session[edit]

wikidata-sdk and other projects were built because they were necessary to create inventaire.io

show of hands: most attendees know JavaScript

reading wikidata: wikidata-sdk; writing wikidata: wikidata-edit (both on npm)

tomorrow: wikidata-cli (built on wikidata-edit) – be there :)

wikidata-lang: index of language codes

some other minor modules that aren’t the topic of today

quick example: `npm init`, say yes to everything; `npm i wikidata-sdk`, `npm i wikidata-edit` (npm since version 8 adds them as dependency to package.json by default)

  1. !/usr/bin/env js

console.log('hello wikidataJS');

const wdk = require('wikidata-toolkit');

wdk has all the functions the library offers, e. g. searchEntities generates wbsearchentities API URL (does not actually do the api call)

design choice: wikidata-sdk helps you generate URLs, but you have to take care of them yourself (you choose between promises, callbacks, whatever)

using another library: bluereq

const url = wdk.searchEntities('search term');

const promise = breq.get(url); # a bluebird Promise

promise.get('body').then(console.log)

wdk.getEntities: get entities by ID

wdk.getManyEntities: if you know you’ll have more than the default quota of 50 (but be nice to the API! quota was there for a reason ☺), returns array of URLs

const groupedPromise = Promise.all(wdk.getManyEntities([…]).map(url => breq.get(url).get('body')))

wdk.getWikidataIdsFromWikipediaTitles