Skip to content

wip: Added wiki sparql support #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions shells/prebuild.ts
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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<MarkdownMetaData>(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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
Expand Down
72 changes: 72 additions & 0 deletions src/lib/core/wiki.ts
Original file line number Diff line number Diff line change
@@ -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());
}
}
2 changes: 1 addition & 1 deletion src/routes/articles/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{#each data.articles as a}
<div class="card mb-3">
{#await import(`$lib/markdown/articles/${a.slug}/cover-1024x512.webp`) then { default: src }}
<img {src} width="1024" height="512" class="card-img-top" alt="article cover">
<img {src} width="1024" height="512" class="card-img-top" alt="article cover" />
{/await}
<div class="card-body">
<h2 class="card-title fs-4 text-uppercase">{a.attributes.title}</h2>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/articles/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
content="The article, {data.article.attributes.title}, of cwc1222's blog"
/>
<meta name="author" content="cwc1222" />
<meta http-equiv="content-language" content="{data.article.attributes.lang}">
<meta http-equiv="content-language" content={data.article.attributes.lang} />
</svelte:head>

<div>
Expand Down