Zmiana iframe listy kanałow na pobierane lokalnie i parsowane

This commit is contained in:
dm
2025-11-26 09:15:29 +01:00
parent 284009d411
commit 8bf578e6d9
7 changed files with 129 additions and 96 deletions

View File

@@ -0,0 +1,42 @@
import type { APIRoute } from "astro";
import { JSDOM } from "jsdom";
interface Channel {
title: string;
logo: string;
}
const cache = new Map<string, { time: number; data: Channel[] }>();
const CACHE_TIME = 1000 * 60 * 60 * 24 * 30; //miesiąc
export const GET: APIRoute = async ({ params }) => {
const id = params.id!;
const cached = cache.get(id);
if (cached && Date.now() - cached.time < CACHE_TIME) {
return new Response(JSON.stringify(cached.data), {
headers: { "Content-Type": "application/json" }
});
}
const url = `https://www.jambox.pl/iframe-pakiet-logo?p=${id}`;
const resp = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0" } });
const html = await resp.text();
const dom = new JSDOM(html);
const images = [
...dom.window.document.querySelectorAll("img.imagefield-field_logo")
];
const channels = images.map((img) => ({
title: img.getAttribute("alt")?.trim() ?? "",
logo: img.getAttribute("src") ?? "",
}));
cache.set(id, { time: Date.now(), data: channels });
return new Response(JSON.stringify(channels), {
headers: { "Content-Type": "application/json" }
});
};