summaryrefslogtreecommitdiff
path: root/src/lib/services/wiki.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/services/wiki.ts')
-rw-r--r--src/lib/services/wiki.ts244
1 files changed, 244 insertions, 0 deletions
diff --git a/src/lib/services/wiki.ts b/src/lib/services/wiki.ts
new file mode 100644
index 0000000..fe9f61d
--- /dev/null
+++ b/src/lib/services/wiki.ts
@@ -0,0 +1,244 @@
+import wiki, {
+ type eventOptions,
+ type eventResult,
+ type fcOptions,
+ type geoOptions,
+ type randomFormats,
+} from "wikipedia";
+import { handlePromise } from "../utils";
+
+export async function fetchWordInWiki(s: string) {
+ const params = new URLSearchParams();
+ params.append("action", "parse");
+ params.append("page", s);
+ params.append("format", "json");
+ params.append("prop", "templates|text");
+ params.append("formatversion", "2");
+
+ const p = params.toString();
+ const url = `https://en.wiktionary.org/w/api.php?${p}`;
+ const res = await fetch(url);
+ console.log(res.headers.get("content-type"));
+ const j = await res.json();
+ return j.parse.text as string;
+}
+
+export async function* readWiktionaryDump() {
+ const file = Bun.file(
+ "/home/y/code/prosody/resources/wiktionary/raw-wiktextract-data.jsonl",
+ );
+ const reader = file
+ .stream()
+ .pipeThrough(new TextDecoderStream("utf-8"))
+ .getReader();
+ let remainder = "";
+ while (true) {
+ const { value, done } = await reader.read();
+ if (done) break;
+ let lines = (remainder + value).split(/\r?\n/);
+ remainder = lines.pop() || "";
+ for (const line of lines) yield line;
+ }
+ if (remainder) yield remainder;
+}
+
+function fixToday(today: eventResult) {
+ const empty: any = [];
+ today.selected = today.selected
+ ? Array.isArray(today.selected)
+ ? today.selected
+ : empty
+ : empty;
+ today.births = today.births
+ ? Array.isArray(today.births)
+ ? today.births
+ : empty
+ : empty;
+ today.deaths = today.deaths
+ ? Array.isArray(today.deaths)
+ ? today.deaths
+ : empty
+ : empty;
+ today.events = today.events
+ ? Array.isArray(today.events)
+ ? today.events
+ : empty
+ : empty;
+ today.holidays = today.holidays
+ ? Array.isArray(today.holidays)
+ ? today.holidays
+ : empty
+ : empty;
+ return today;
+}
+
+export async function fetchWikipedia(lang: string) {
+ // LANG: 2 letters! or en-gb zh-hans etc.
+ try {
+ // const languages = await wiki.languages();
+ const setLang = wiki.setLang(lang);
+ const ff = wiki.featuredContent();
+ const rr = wiki.random();
+ const tt = wiki.onThisDay();
+ const [featured, today, random] = await Promise.all([ff, tt, rr]);
+ return { ok: { lang, setLang, featured, random, today: fixToday(today) } };
+ } catch (e) {
+ return { error: `${e}` };
+ }
+}
+export async function fetchWikipediaPage(title: string) {
+ // LANG: 2 letters! or en-gb zh-hans etc.
+ try {
+ // const languages = await wiki.languages();
+ const ppage = wiki.page(title);
+ const pinfo = wiki.infobox(title);
+ const pintro = wiki.intro(title);
+ const pimages = wiki.images(title);
+ const psummary = wiki.summary(title);
+ const phtml = wiki.html(title);
+ const pmobileHtml = wiki.mobileHtml(title);
+ const ppdf = wiki.pdf(title);
+ const pcontent = wiki.content(title);
+ const pcategories = wiki.categories(title);
+ const prelated = wiki.related(title);
+ const pmedia = wiki.media(title);
+ const plinks = wiki.links(title);
+ const preferences = wiki.references(title);
+ const pcoordinates = wiki.coordinates(title);
+ const plangLinks = wiki.langLinks(title);
+ const ptables = wiki.tables(title);
+ const pcitation = wiki.citation(title);
+
+ const [
+ page,
+ info,
+ intro,
+ images,
+ summary,
+ html,
+ mobileHtml,
+ pdf,
+ content,
+ categories,
+ related,
+ media,
+ links,
+ references,
+ coordinates,
+ langLinks,
+ tables,
+ citation,
+ ] = await Promise.allSettled([
+ ppage,
+ pinfo,
+ pintro,
+ pimages,
+ psummary,
+ phtml,
+ pmobileHtml,
+ ppdf,
+ pcontent,
+ pcategories,
+ prelated,
+ pmedia,
+ plinks,
+ preferences,
+ pcoordinates,
+ plangLinks,
+ ptables,
+ pcitation,
+ ]);
+ return {
+ ok: {
+ page: handlePromise(page),
+ info: handlePromise(info),
+ intro: handlePromise(intro),
+ images: handlePromise(images),
+ media: handlePromise(media),
+ summary: handlePromise(summary),
+ html: handlePromise(html),
+ mobileHtml: handlePromise(mobileHtml),
+ pdf: handlePromise(pdf),
+ content: handlePromise(content),
+ categories: handlePromise(categories),
+ related: handlePromise(related),
+ links: handlePromise(links),
+ references: handlePromise(references),
+ coordinates: handlePromise(coordinates),
+ langLinks: handlePromise(langLinks),
+ tables: handlePromise(tables),
+ citation: handlePromise(citation),
+ },
+ };
+ } catch (e) {
+ return { error: `${e}` };
+ }
+}
+export async function fetchWikipediaSearch(query: string) {
+ // LANG: 2 letters! or en-gb zh-hans etc.
+ try {
+ // const languages = await wiki.languages();
+ const psearchResults = wiki.search(query);
+ const pcitation = wiki.citation(query);
+ const psuggestions = wiki.suggest(query);
+ const pautoComplete = wiki.autocompletions(query);
+ const [searchResults, citation, suggestions, autoComplete] =
+ await Promise.all([
+ psearchResults,
+ pcitation,
+ psuggestions,
+ pautoComplete,
+ ]);
+ return { ok: { searchResults, citation, suggestions, autoComplete } };
+ } catch (e) {
+ return { error: `${e}` };
+ }
+}
+export async function fetchWikipediaGeoSearch({
+ latitude,
+ longitude,
+ options,
+}: {
+ latitude: number;
+ longitude: number;
+ options?: geoOptions;
+}) {
+ // LANG: 2 letters! or en-gb zh-hans etc.
+ try {
+ // const languages = await wiki.languages();
+ const searchResults = await wiki.geoSearch(latitude, longitude, options);
+ return { ok: searchResults };
+ } catch (e) {
+ return { error: `${e}` };
+ }
+}
+export async function fetchWikipediaFeatured(opts: fcOptions) {
+ // LANG: 2 letters! or en-gb zh-hans etc.
+ try {
+ // const languages = await wiki.languages();
+ const featured = await wiki.featuredContent(opts);
+ return { ok: featured };
+ } catch (e) {
+ return { error: `${e}` };
+ }
+}
+export async function fetchWikipediaDate(opts: eventOptions) {
+ // LANG: 2 letters! or en-gb zh-hans etc.
+ try {
+ // const languages = await wiki.languages();
+ const date = await wiki.onThisDay(opts);
+ return { ok: fixToday(date) };
+ } catch (e) {
+ return { error: `${e}` };
+ }
+}
+export async function fetchWikipediaRandom(opts?: randomFormats) {
+ // LANG: 2 letters! or en-gb zh-hans etc.
+ try {
+ // const languages = await wiki.languages();
+ const random = await wiki.random(opts);
+ return { ok: random };
+ } catch (e) {
+ return { error: `${e}` };
+ }
+}