Zmiana iframe listy kanałow na pobierane lokalnie i parsowane
This commit is contained in:
42
src/pages/api/jambox/[id].ts
Normal file
42
src/pages/api/jambox/[id].ts
Normal 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" }
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user