From 3b64a9c0a2234e3674514b4474ebf517927f92be Mon Sep 17 00:00:00 2001 From: cwc1222 Date: Thu, 16 May 2024 17:08:14 -0400 Subject: [PATCH] wip: Added wiki sparql support --- shells/prebuild.ts | 21 ++++---- src/lib/core/types.ts | 2 +- src/lib/core/wiki.ts | 72 +++++++++++++++++++++++++ src/routes/articles/+page.svelte | 2 +- src/routes/articles/[slug]/+page.svelte | 2 +- 5 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 src/lib/core/wiki.ts diff --git a/shells/prebuild.ts b/shells/prebuild.ts index f48c7bb..e99b8b6 100644 --- a/shells/prebuild.ts +++ b/shells/prebuild.ts @@ -1,6 +1,6 @@ import Frontmatter from 'front-matter'; import { readdirSync, readFileSync } from 'node:fs'; -import { Glob } from "bun"; +import { Glob } from 'bun'; import type { MarkdownMetaData } from '../src/lib/core/types'; const createRssFeed = async ( @@ -36,18 +36,21 @@ const createRssFeed = async ( const file = Bun.file('package.json'); const packageJson = await file.json(); -const glob = new Glob("src/lib/markdown/articles/**/*.md"); -let articles: {slug: string, attributes: MarkdownMetaData}[] = []; -for await (const file of glob.scan(".")) { - const splitedPath = file.split('/') +const glob = new Glob('src/lib/markdown/articles/**/*.md'); +let articles: { slug: string; attributes: MarkdownMetaData }[] = []; +for await (const file of glob.scan('.')) { + const splitedPath = file.split('/'); const rawCtn = readFileSync(file).toString(); const fm = Frontmatter(rawCtn); const attributes = fm.attributes; - articles = [...articles, { - slug: splitedPath[splitedPath.length-1].split('.')[0], - attributes: attributes - }] + articles = [ + ...articles, + { + slug: splitedPath[splitedPath.length - 1].split('.')[0], + attributes: attributes + } + ]; } articles.sort((a, b) => { diff --git a/src/lib/core/types.ts b/src/lib/core/types.ts index 30e1c2d..c20441c 100644 --- a/src/lib/core/types.ts +++ b/src/lib/core/types.ts @@ -20,7 +20,7 @@ export type MenuInfo = { export type MarkdownMetaData = { title: string; description: string; - lang: "en-us" | "zh-tw" | "es-py"; + lang: 'en-us' | 'zh-tw' | 'es-py'; createdAt: Date; updatedAt?: Date; tags: Array; diff --git a/src/lib/core/wiki.ts b/src/lib/core/wiki.ts new file mode 100644 index 0000000..9719c3a --- /dev/null +++ b/src/lib/core/wiki.ts @@ -0,0 +1,72 @@ +class SPARQLQueryDispatcher { + #endpoint: string = 'https://query.wikidata.org/sparql'; + #headers = { Accept: 'application/sparql-results+json' }; + + public async queryCountryInfo(wikiCountryId: string) { + const sparqlQuery = ` + SELECT + ?continentLabel + ?countryLabel + ?capitalLabel + ?coordinates + (year(?inception) AS ?inception_year) + ?hdi + ?population + ?life_expectancy + (?gdp_usd / ?population AS ?gdp_per_capita_usd) + (year(?gpd_date) AS ?gdp_year) + (GROUP_CONCAT(DISTINCT ?timezoneLabel; separator = ", ") AS ?timezones) + (GROUP_CONCAT(DISTINCT ?currencyCode; separator = ", ") AS ?currencies) + (GROUP_CONCAT(DISTINCT ?official_lang_lables; separator = ", ") AS ?langs) + (GROUP_CONCAT(DISTINCT ?voltage; separator = ", ") AS ?voltages) + (GROUP_CONCAT(DISTINCT ?frequency; separator = ", ") AS ?frequencies) + (GROUP_CONCAT(DISTINCT ?plugTypeLabel; separator = ", ") AS ?plugTypes) + WHERE { + ?country wdt:P31 wd:Q6256. # is instance of country + ?country wdt:P17 wd:${wikiCountryId}. # country is Q733 Paraguay + + Optional { + ?country wdt:P30 ?continent. + ?continent rdfs:label ?continentLabel filter (lang(?continentLabel) = "en"). + ?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en"). + ?country wdt:P36 ?capital. + ?capital rdfs:label ?capitalLabel filter (lang(?capitalLabel) = "en"). + + ?country p:P421/ps:P421 ?timezone. + ?timezone rdfs:label ?timezoneLabel filter (lang(?timezoneLabel) = "en"). + ?country wdt:P625 ?coordinates. + ?country wdt:P571 ?inception. + ?country wdt:P1081 ?hdi. + ?country wdt:P1082 ?population. + ?country wdt:P2250 ?life_expectancy. + + ?country wdt:P37 ?official_lang. + #?official_lang wdt:P218 ?official_lang_code. + ?official_lang rdfs:label ?official_lang_lables filter (lang(?official_lang_lables) = "en"). + + # Currencies + ?country wdt:P38 ?currency. + ?currency wdt:P498 ?currencyCode. + + # Nominal GDP + ?country wdt:P2131 ?gdp_usd. + ?gdpStmt ps:P2131 ?gdp_usd ; + pq:P585 ?gpd_date . + FILTER(?gpd_date = MAX(?gpd_date)) + + # Domestic electricity and Plug type + ?country p:P2884/ps:P2884 ?voltage. # i'm not sure about the divider operator + ?country p:P2884 ?voltageStatement. + ?voltageStatement pq:P2144 ?frequency. + ?country wdt:P2853 ?plugType. + ?plugType rdfs:label ?plugTypeLabel filter (lang(?plugTypeLabel) = "en"). + } + SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } + } + GROUP BY ?continentLabel ?countryLabel ?capitalLabel ?coordinates ?inception ?hdi ?population ?life_expectancy ?gdp_usd ?gpd_date + + `; + const fullUrl = `${this.#endpoint}?query=${encodeURIComponent(sparqlQuery)}`; + return fetch(fullUrl, { headers: this.#headers }).then((body) => body.json()); + } +} diff --git a/src/routes/articles/+page.svelte b/src/routes/articles/+page.svelte index bfe1beb..09f8f29 100644 --- a/src/routes/articles/+page.svelte +++ b/src/routes/articles/+page.svelte @@ -18,7 +18,7 @@ {#each data.articles as a}
{#await import(`$lib/markdown/articles/${a.slug}/cover-1024x512.webp`) then { default: src }} - article cover + article cover {/await}

{a.attributes.title}

diff --git a/src/routes/articles/[slug]/+page.svelte b/src/routes/articles/[slug]/+page.svelte index f246120..1abb323 100644 --- a/src/routes/articles/[slug]/+page.svelte +++ b/src/routes/articles/[slug]/+page.svelte @@ -28,7 +28,7 @@ content="The article, {data.article.attributes.title}, of cwc1222's blog" /> - +