Public
Script
Readme

This val queries Wikidata for basic information on "countries" – name, map data, and flag data. It is cached.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { sparql } from "https://esm.town/v/jdan/sparql";
import { blob } from "https://esm.town/v/std/blob?v=10";
interface Entry {
country: string;
countryLabel: string;
mapLabel: string;
flagLabel: string;
}
const CACHE_KEY = "cache-jdan/countryData";
export async function countryData(): Promise<Entry[]> {
const cached = await blob.getJSON(CACHE_KEY);
if (cached) {
return cached;
}
const res = await sparql(`
SELECT DISTINCT ?country ?countryLabel ?mapLabel ?flagLabel
WHERE
{
{ ?country wdt:P31 wd:Q6256 } # Countries
UNION { ?country wdt:P31 wd:Q3624078 } # Soverign states
MINUS { ?country wdt:P31 wd:Q3024240 } # Historical countries
MINUS { ?country wdt:P31 wd:Q417175 } # Kingdoms
?country wdt:P41 ?flag.
?country wdt:P3896 ?map.
# When maps are optional, Chinland, Palestine, and Transnistria appear
# OPTIONAL { ?country wdt:P3896 ?map }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?countryLabel
`);
const values = res.results.bindings.map(({ country, countryLabel, mapLabel, flagLabel }) => {
return {
country: country.value,
countryLabel: countryLabel.value,
mapLabel: mapLabel.value,
flagLabel: flagLabel.value,
};
});
await blob.setJSON(CACHE_KEY, values);
return values;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
February 4, 2024