From 0bc8aee9ff2d14aadfb02db0be0f490cd41b1ca4 Mon Sep 17 00:00:00 2001 From: Jisu Kim Date: Fri, 25 Feb 2022 20:47:54 +0900 Subject: [PATCH] add index.html --- index.html | 51 +++++++++++++++++++++++++++++++++++ main.ts | 78 +++++++++++++++++++++++++++++++++--------------------- 2 files changed, 99 insertions(+), 30 deletions(-) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..3422143 --- /dev/null +++ b/index.html @@ -0,0 +1,51 @@ + + + + Voca + + + + + +
+
+ 시험 난이도 +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ +
+ +
+
+ + diff --git a/main.ts b/main.ts index 839e2f3..d678cdf 100644 --- a/main.ts +++ b/main.ts @@ -11,16 +11,18 @@ type Result = { answer: string; }; -async function generateVoca(level: number, day: number) { +async function generateVoca(levels: number[], day: number) { const re = /

(?\d+?)\. (?.+?)<\/p>.*?(?.+?)<\/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, - }), - ); } }