wikitxt.ts
· 678 B · TypeScript
Ham
import { fetch } from "https://esm.town/v/std/fetch";
import { fold } from "https://esm.town/v/jordan/fold";
export const wikitxt = async (req: Request) => {
const url = new URL(req.url), article = url.pathname.replace("/", ""), width = parseInt(url.searchParams.get("width"));
const wiki = await fetch(`https://en.wikipedia.org/w/api.php?action=query&format=json&titles=${article}&prop=extracts&explaintext`).then(res => res.json());
if (!wiki.query) return new Response("Please input a correct title")
const pages = wiki.query.pages;
const txt = pages[Object.keys(pages)[0]].extract;
const res = width ? fold(txt, width) : fold(txt);
return new Response(res);
}
| 1 | import { fetch } from "https://esm.town/v/std/fetch"; |
| 2 | import { fold } from "https://esm.town/v/jordan/fold"; |
| 3 | |
| 4 | export const wikitxt = async (req: Request) => { |
| 5 | const url = new URL(req.url), article = url.pathname.replace("/", ""), width = parseInt(url.searchParams.get("width")); |
| 6 | const wiki = await fetch(`https://en.wikipedia.org/w/api.php?action=query&format=json&titles=${article}&prop=extracts&explaintext`).then(res => res.json()); |
| 7 | if (!wiki.query) return new Response("Please input a correct title") |
| 8 | const pages = wiki.query.pages; |
| 9 | const txt = pages[Object.keys(pages)[0]].extract; |
| 10 | const res = width ? fold(txt, width) : fold(txt); |
| 11 | return new Response(res); |
| 12 | } |