1
0
mirror of https://github.com/vbalien/voca.git synced 2025-12-06 11:26:21 +09:00

add index.html

This commit is contained in:
2022-02-25 20:47:54 +09:00
parent dcfd123385
commit 0bc8aee9ff
2 changed files with 99 additions and 30 deletions

78
main.ts
View File

@@ -11,16 +11,18 @@ type Result = {
answer: string;
};
async function generateVoca(level: number, day: number) {
async function generateVoca(levels: number[], day: number) {
const re =
/<p class="word">(?<id>\d+?)\. (?<word>.+?)<\/p>.*?<span class="af_answer">(?<answer>.+?)<\/span>/gis;
const res = await fetch(
`https://www.hackers.co.kr/?c=s_toeic/new_voca_toeic_testpaper/toeic_study/new_paper&mode=new_view&level=${level}&level_type=&lang_text=2&question=1000&day3=${day}&day4=${day}&day_auto=N&index=1`,
`https://www.hackers.co.kr/?c=s_toeic/new_voca_toeic_testpaper/toeic_study/new_paper&mode=new_view&level=${
levels.join(",")
}&level_type=&lang_text=2&question=1000&day3=${day}&day4=${day}&day_auto=N&index=1`,
);
const body = (await res.text());
const body = await res.text();
const matches = [...body.matchAll(re)].map((m) => ({
...m.groups as unknown as Result,
...(m.groups as unknown as Result),
id: Number.parseInt(m.groups?.id as string),
}));
@@ -52,15 +54,15 @@ async function generateVoca(level: number, day: number) {
yCursor = 1;
}
page.drawText(`${data.id}. ${data.word}`, {
x: margin + (page.getWidth() / 2 * col),
y: page.getHeight() - (fontSize + margin) * (yCursor),
x: margin + (page.getWidth() / 2) * col,
y: page.getHeight() - (fontSize + margin) * yCursor,
size: fontSize,
font: customFont,
color: rgb(0, 0, 0),
});
page.drawText("_________________", {
x: margin + (page.getWidth() / 2 * col) + 140,
y: page.getHeight() - (fontSize + margin) * (yCursor) - 3,
x: margin + (page.getWidth() / 2) * col + 140,
y: page.getHeight() - (fontSize + margin) * yCursor - 3,
size: fontSize,
font: customFont,
color: rgb(0, 0, 0),
@@ -81,23 +83,23 @@ async function generateVoca(level: number, day: number) {
yCursor = 1;
}
page.drawText(`${data.id}. ${data.word}`, {
x: margin + (page.getWidth() / 2 * col),
y: page.getHeight() - (fontSize + margin) * (yCursor),
x: margin + (page.getWidth() / 2) * col,
y: page.getHeight() - (fontSize + margin) * yCursor,
size: fontSize,
font: customFont,
color: rgb(0, 0, 0),
});
page.drawText("_________________", {
x: margin + (page.getWidth() / 2 * col) + 140,
y: page.getHeight() - (fontSize + margin) * (yCursor) - 3,
x: margin + (page.getWidth() / 2) * col + 140,
y: page.getHeight() - (fontSize + margin) * yCursor - 3,
size: fontSize,
font: customFont,
color: rgb(0, 0, 0),
});
page.drawText(data.answer, {
x: margin + (page.getWidth() / 2 * col) + 140,
y: page.getHeight() - (fontSize + margin) * (yCursor),
size: fontSize,
x: margin + (page.getWidth() / 2) * col + 140,
y: page.getHeight() - (fontSize + margin) * yCursor,
size: 8,
font: customFont,
color: rgb(1, 0, 0),
});
@@ -121,25 +123,41 @@ for await (const conn of server) {
async function serveHttp(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn);
for await (const requestEvent of httpConn) {
const url = new URL(requestEvent.request.url);
const { searchParams, pathname } = new URL(requestEvent.request.url);
if (pathname.startsWith("/generate")) {
if (!searchParams.has("level") || !searchParams.has("day")) {
requestEvent.respondWith(
new Response("Not Found", {
status: 404,
}),
);
continue;
}
const levels = searchParams.getAll("level").map((l) =>
Number.parseInt(l)
);
const day = Number.parseInt(searchParams.get("day")!) ?? 1;
const data = await generateVoca(levels, day);
if (!url.searchParams.has("level") || !url.searchParams.has("day")) {
requestEvent.respondWith(
new Response("Not Found", {
status: 404,
new Response(data, {
status: 200,
headers: {
"Content-Disposition": 'attachment; filename="voca.pdf"',
},
}),
);
} else if (pathname.startsWith("/") || pathname.startsWith("/index.html")) {
const file = await Deno.readFile("./index.html");
requestEvent.respondWith(
new Response(file, {
headers: {
"content-type": "text/html",
},
}),
);
return;
}
const level = Number.parseInt(url.searchParams.get("level")!) ?? 7;
const day = Number.parseInt(url.searchParams.get("day")!) ?? 1;
const data = await generateVoca(level, day);
requestEvent.respondWith(
new Response(data, {
status: 200,
}),
);
}
}