Something went wrong. Try again.
Utilities for Regreso clients 🔧
productivity llm utils regreso
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import { parse } from "node-html-parser";
type WebDetailsResult = { url: string | undefined; title: (string | undefined)[]; description: (string | undefined)[]; error?: string;};
export async function getWebDetails(url: string): Promise<WebDetailsResult> { const htmlResponse = await fetch( url.startsWith("http") ? url : "https://" + url, { signal: AbortSignal.timeout(1200) }, ) .then((res) => res.text()) .catch((_err) => { return "Failed to fetch URL"; });
if (htmlResponse === "Failed to fetch URL") { return { error: htmlResponse, url: url.startsWith("http") ? url : "https://" + url, title: ["New Destination"], description: [""], }; }
const doc = parse(htmlResponse); const title = [ doc.querySelector("title")?.text, doc.querySelector('meta[property="og:title"]')?.getAttribute("content"), doc.querySelector('meta[name="twitter:title"]')?.getAttribute("content"), ]; const description = [ doc.querySelector('meta[name="description"]')?.getAttribute("content"), doc .querySelector('meta[property="og:description"]') ?.getAttribute("content"), doc .querySelector('meta[name="twitter:description"]') ?.getAttribute("content"), ];
return { url: url.startsWith("http") ? url : "https://" + url, title, description, };}