import { APIGatewayProxyEventV2, APIGatewayProxyResultV2, Context, } from "https://deno.land/x/lambda/mod.ts"; import { DOMParser, Element, } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts"; async function getThumUrlAndTitle(url: string): Promise { const res = await fetch(url); const doc = new DOMParser().parseFromString(await res.text(), "text/html"); if (doc == null) { throw new Error("document not found"); } const node = doc.querySelectorAll('meta[property="og:image"]')[0] as Element; const thumbUrl = node.getAttribute("content"); if (thumbUrl == null) { throw new Error("thumbnailUrl not found"); } // タイトルがないことはない const titleNode = doc.querySelectorAll('head > title')[0] as Element; return [thumbUrl, titleNode.textContent]; } // deno-lint-ignore require-await export async function handler( _event: APIGatewayProxyEventV2, _context: Context, ): Promise { const [thumbnailUrl, title] = await getThumUrlAndTitle(_event.queryStringParameters.watchUrl); return { statusCode: 200, headers: { "content-type": "application/json;charset=utf8" }, body: JSON.stringify({ "thumbnailUrl": `${thumbnailUrl}`, "title": `${title}` }), }; }