1

I've been playing around with the Wikidata API for a while. I know how to get item (entities) by name and by their Q number. but I can't seem to figure out how to get their properties right. What I'm looking for is P214 (viaf identifiers for authors)

For example, i'm already using this query to get an author by his full name

https://www.wikidata.org/w/api.php?action=wbsearchentities&search=Douglas%20Adams&language=en

returns an array of pages names and linked to pages that have the name "Douglas Adams" on them. but no properties.

then I can use the list of Q id to query for properties, like so

https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q42&language=en 

the viaf property and it's value for Q42 are there. but this is such an unwieldy approach, because I'm getting a list of results from the first query, then I have to iterate over them and query each for properties for that one I'm looking for.

tl;dr : is there a simpler way to get an item properties list by item name ?

Termininja
  • 6,620
  • 12
  • 48
  • 49
svarog
  • 9,477
  • 4
  • 61
  • 77
  • This makes it only a little simpler, but if you know what property you want, use wbgetclaims instead of wbgetentities. E.g. https://www.wikidata.org/w/api.php?action=wbgetclaims&format=json&entity=Q42&property=P214 – Ainali Oct 15 '15 at 20:10
  • I've already tried that, you would have to ensure your Q contains P214 for your query to work. Because I need to get `Q` and then `viafId` by name search, I need to first verify the Q (or list of Q's if the result is so kind) even has P214 before continuing. – svarog Oct 15 '15 at 20:44
  • You cannot get the VIAF identifier from an item that does not have P214. So the query will work for all items that you are interested in getting results from, it does not need to be verified before querying for the value. Example: Q1 does not have P214, so there is no VIAF identifier to get. The query https://www.wikidata.org/w/api.php?action=wbgetclaims&format=json&entity=Q1&property=P214 will tell you that. – Ainali Oct 17 '15 at 09:09
  • I know, I return an empty array/object if it doesn't. – svarog Oct 17 '15 at 09:44

2 Answers2

1

One indirect approach that might work is to pull down a comprehensive list of all Q-items containing P213 - there's about a million, so it'll be a few MB - at the start of your session, and then for each search, check to see if any of your search results are on that local list. If they are, go ahead and make a further API call to look at the details - and if not, you won't be interested, so you can skip them.

This won't get you any VIAF items added during the session, but the day-to-day change is relatively minor so this shouldn't be a major concern.

Andrew is gone
  • 286
  • 1
  • 5
  • Luckily, Wikidata provides the means for [Database dumps](https://www.wikidata.org/wiki/Wikidata:Database_download) so that's a quite good alternative solution. – svarog Oct 21 '15 at 17:11
1

I eventually figured out how to get an entity's property list by name using the wbgetentities action. I totally missed out that I can do a lookup by titles and sites parameters to get an entity rather than just searching it by it's QID using the same action.

what pointed me towards this solution was this example (hidden at the bottom of the page):

api.php?action=wbgetentities&sites=enwiki&titles=Berlin&languages=en

a small down side is that I must also provide the sites parameter, but I can live with that

EDIT:

The SPARQL API was just what I needed all along!!!

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/> 
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?viaf WHERE {
  wd:Q42 wdt:P214 ?viaf   
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
}

now i'm getting all P214 (viaf) for Q42 (adams), replacing wd:Q42 with a plain object symbol ?o, will get me all entities with a VIAF ID property and it's value.

svarog
  • 9,477
  • 4
  • 61
  • 77