get_top = (qid, n = 20, lang) => {
const query = `SELECT ?item ?itemLabel ?itemDescription (COUNT(*) AS ?count) WHERE {
?item wdt:P31 wd:Q5;
p:P735 ?statement.
?statement ps:P735 wd:${qid}.
OPTIONAL { ?statement pq:P1545 ?rank. }
BIND(IF(BOUND(?rank),?rank, 0) AS ?rank2)
FILTER(xsd:integer(?rank2) < xsd:integer(2))
?sitelink schema:about ?item.
FILTER (SUBSTR(str(?sitelink), 11, 15) = ".wikipedia.org/") .
SERVICE wikibase:label { bd:serviceParam wikibase:language "${lang},en". }
}
GROUP BY ?item ?itemLabel ?itemDescription
ORDER BY DESC (?count)
LIMIT ${n}`
return fetch(`https://query.wikidata.org/sparql?query=${encodeURIComponent(query)}`, {headers: {accept: "application/sparql-results+json"}})
.then(response => response.json())
.then(
d => d.results.bindings.map(
res => (
{
item: res.item.value,
itemLabel: res.itemLabel.value,
itemDescription: res.itemDescription.value,
count: res.count.value
}
)
)
)
.then(d => aq.from(d))
}