import * as cheerio from "cheerio";
import type { CheerioAPI } from "cheerio";
export const BASE_URL = "https://meupc.net";
const USER_AGENT =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
const FETCH_TIMEOUT = 15_000;
export async function fetchPage(path: string): Promise<CheerioAPI> {
const url = path.startsWith("http") ? path : `${BASE_URL}${path.startsWith("/") ? "" : "/"}${path}`;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT);
try {
const response = await fetch(url, {
headers: {
"User-Agent": USER_AGENT,
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7",
},
signal: controller.signal,
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText} — ${url}`);
}
const html = await response.text();
return cheerio.load(html);
} finally {
clearTimeout(timeout);
}
}
export function absoluteUrl(path: string | undefined | null): string {
if (!path) return "";
if (path.startsWith("http")) return path;
return `${BASE_URL}${path.startsWith("/") ? "" : "/"}${path}`;
}
export function parsePrice(text: string | undefined | null): number | null {
if (!text) return null;
const cleaned = text.replace(/[R$\s.]/g, "").replace(",", ".");
const num = parseFloat(cleaned);
return isNaN(num) ? null : num;
}