Skip to content

Latest commit

 

History

History
91 lines (71 loc) · 3.82 KB

get_entities.md

File metadata and controls

91 lines (71 loc) · 3.82 KB

Get entities

Summary

By ids

associated Wikibase API doc: wbgetentities

const url = wbk.getEntities({
  ids: [ 'Q1', 'Q5', 'Q571' ],
  languages: [ 'en', 'fr', 'de' ], // returns all languages if not specified
  props: [ 'info', 'claims' ], // returns all props if not specified
  format: 'xml', // default: json
  redirections: false // default: true
})

Example using fetch to make the HTTP request:

const url = wbk.getEntities({
  ids: [ 'Q647268', 'Q771376', 'Q860998', 'Q965704' ],
  language: user.language
})
const { entities } = await fetch(url).then(res => res.json())
// Do your thing with those entities data)

Get many entities by ids

Above 50 ids, wbk.getEntities will warn you that your request won't be fully fullfiled by Wikidata API due to its limitations policy. You can use wbk.getManyEntities instead to generate several request urls to work around this limitation:

The arguments API is the same as getEntities:

const urls = wbk.getManyEntities({
  ids: [ 'Q1', 'Q2', 'Q3', ..., 'Q123' ],
  languages: [ 'en', 'fr', 'de' ],
  props: [ 'info', 'claims' ],
  format: 'json',
  redirections: false // default: true
})

but it returns an array of urls instead.

⚠️ This limitation policy was probably there for a reason, right? This should be the exception, make sure to set an interval between your requests (500ms, 1s?), and if you really need a lot of entities, consider using dumps: there are great tools to work with those too! ;)

By id and revision

At some point in your love story with Wikidata, you might end up needing to access data from an entity at a different revision than the current one, so there's a function for that:

const url = wbk.getEntityRevision({ id: 'Q3548931', revision: 775908525 })

The revision id can be obtained using wbk.getRevisions: look for the revid.

The returned data can then be simplified as for normal entity data.

By Sitelinks

associated Wikibase API doc: wbgetentities

Typical usecase: you're working with a list of Wikipedia articles in a given language and would like to move to Wikidata for all the awesomeness it provides: you can fetch the entities on Wikidata using the Wikipedia articles titles:

const url = wbk.getEntitiesFromSitelinks({ titles: 'Hamburg' })
//=> 'https://www.wikidata.org/w/api.php?action=wbgetentities&titles=Hamburg&sites=enwiki&format=json'

const url = wbk.getEntitiesFromSitelinks({ titles: [ 'Hamburg', 'London', 'Lisbon' ] })
// => 'https://www.wikidata.org/w/api.php?action=wbgetentities&titles=Hamburg%7CLyon%7CBerlin&sites=enwiki&format=json'

By default, it looks in the English Wikipedia, but we can change that:

const url = wbk.getEntitiesFromSitelinks({
  titles: 'Hamburg',
  sites: 'enwikivoyage', // default: enwiki
  languages: [ 'en', 'fr', 'de' ],
  props: [ 'info', 'claims' ],
  format: 'json', // default: json
  redirections: false // default: true
})