This commit is contained in:
Cat /dev/Nulo 2023-09-09 12:22:20 -03:00
parent a38e816152
commit d467fa4f6c
4 changed files with 70 additions and 52 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -1,34 +1,33 @@
{ {
"name": "astrologo", "name": "astrologo",
"version": "0.0.1", "version": "0.0.1",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "vite dev", "dev": "vite dev",
"build": "vite build", "build": "vite build",
"preview": "vite preview", "preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --plugin-search-dir . --check .", "lint": "prettier --plugin-search-dir . --check .",
"format": "prettier --plugin-search-dir . --write ." "format": "prettier --plugin-search-dir . --write ."
}, },
"devDependencies": { "devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0", "@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/kit": "^1.20.4", "@sveltejs/kit": "^1.20.4",
"prettier": "^2.8.0", "prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.10.1", "prettier-plugin-svelte": "^2.10.1",
"svelte": "^4.0.5", "svelte": "^4.0.5",
"svelte-check": "^3.4.3", "svelte-check": "^3.4.3",
"tslib": "^2.4.1", "tslib": "^2.4.1",
"typescript": "^5.0.0", "typescript": "^5.0.0",
"vite": "^4.4.2" "vite": "^4.4.2"
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"bulma": "^0.9.4", "bulma": "^0.9.4",
"date-fns": "^2.30.0", "dayjs": "^1.11.9",
"erina-sign-teller": "^3.4.5", "erina-sign-teller": "^3.4.5",
"unslugify": "^1.0.4", "unslugify": "^1.0.4",
"wikiapi": "^1.19.4", "wikibase-sdk": "^9.2.2"
"wikibase-sdk": "^9.2.2" }
}
} }

View file

@ -1,24 +1,46 @@
import { error } from '@sveltejs/kit'; // import { error } from '@sveltejs/kit';
import Wikiapi from 'wikiapi'; // import Wikiapi from 'wikiapi';
import unslugify from 'unslugify'; import unslugify from 'unslugify';
import { WBK } from 'wikibase-sdk';
import dayjs from 'dayjs';
const wdk = WBK({
instance: 'https://www.wikidata.org',
sparqlEndpoint: 'https://query.wikidata.org/sparql'
});
// month is zero-indexed
export type Birth = { year: number; month: number; day: number }; export type Birth = { year: number; month: number; day: number };
export type Entry = { name: string; birth: Birth };
let cache = new Map<string, Birth>(); let cache = new Map<string, Entry>();
/** @type {import('./$types').PageLoad} */ /** @type {import('./$types').PageLoad} */
export async function load({ params }) { export async function load({ params }) {
const nombre = unslugify(params.slug); const query = unslugify(params.slug);
const wiki = new Wikiapi();
let birth: Birth; if (cache.has(query)) {
if (cache.has(nombre)) { return cache.get(query)!;
birth = cache.get(nombre)!;
} else { } else {
const data = await wiki.data(nombre, 'P569'); // const data = await wiki.data(query, 'P569');
if (data.length === 0) throw error(404, 'Not found'); // if (data.length === 0) throw error(404, 'Not found');
const [year, month, day] = data[0]; // const [year, month, day] = data[0];
birth = { year, month, day };
cache.set(nombre, birth); const url = wdk.getEntitiesFromSitelinks({ titles: [query] });
const json = await fetch(url).then((res) => res.json());
let name = query;
if (json.normalized?.n) {
name = json.normalized.n.to;
}
const entity = json.entities[Object.keys(json.entities)[0]];
const birthday = dayjs(entity.claims?.P569[0]?.mainsnak?.datavalue?.value?.time);
const birth: Birth = {
year: birthday.year(),
month: birthday.month(),
day: birthday.date()
};
const entry: Entry = { name, birth };
cache.set(query, entry);
return entry;
} }
return { nombre: nombre, birth };
} }

View file

@ -3,10 +3,7 @@
import type { Birth } from './+page.server.js'; import type { Birth } from './+page.server.js';
export let data; export let data;
$: birth = data.birth; $: detalleSigno = signo(data.birth);
$: detalleSigno = signo(birth);
const twoDigitNum = (num: number) => (num < 10 ? `0${num}` : num);
const intl = Intl.DateTimeFormat('es', { dateStyle: 'long' }); const intl = Intl.DateTimeFormat('es', { dateStyle: 'long' });
const formatDate = (date: Date) => intl.format(date); const formatDate = (date: Date) => intl.format(date);
@ -18,11 +15,11 @@
<section class="hero"> <section class="hero">
<div class="hero-body"> <div class="hero-body">
<p class="title"> <p class="title">
{data.nombre} es de {detalleSigno.sign} {data.name} es de {detalleSigno.sign}
</p> </p>
<p class="subtitle"> <p class="subtitle">
Cumple el {formatBirth(birth)} Cumple el {formatBirth(data.birth)}
</p> </p>
</div> </div>
</section> </section>