diff --git a/api/v1/src/shikimori-proxy.ts b/api/v1/src/shikimori-proxy.ts index 7773194..b0663e0 100644 --- a/api/v1/src/shikimori-proxy.ts +++ b/api/v1/src/shikimori-proxy.ts @@ -9,31 +9,48 @@ serve(async (req) => { return new Response("Missing search param", { status: 400 }); } - const targetUrl = `https://shikimori.one/api/animes?search=${encodeURIComponent(search)}&limit=1${year ? `&year=${year}` : ""}`; + const baseHeaders = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", + "Accept": "application/json" + }; + + const animeUrl = `https://shikimori.one/api/animes?search=${encodeURIComponent(search)}&limit=1${year ? `&year=${year}` : ""}`; try { - const shikiRes = await fetch(targetUrl, { - headers: { - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", - "Accept": "application/json" - } + const animeRes = await fetch(animeUrl, { headers: baseHeaders }); + const animeData = await animeRes.json(); + const anime = animeData?.[0]; + + if (!anime || !anime.id) { + return new Response(JSON.stringify({ score: "N/A", characters: [] }), { + headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } + }); + } + + const rolesUrl = `https://shikimori.one/api/animes/${anime.id}/roles`; + const rolesRes = await fetch(rolesUrl, { headers: baseHeaders }); + const roles = await rolesRes.json(); + + const mainCharacters = roles + .filter((c) => c.roles.includes("Main")) + .slice(0, 5) + .map((c) => ({ + name: c.character.russian, + url: `https://shikimori.one${c.character.url}` + })); + + const result = { + score: anime.score || "N/A", + characters: mainCharacters + }; + + return new Response(JSON.stringify(result), { + headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } }); - const data = await shikiRes.text(); - - return new Response(data, { - status: 200, - headers: { - "Content-Type": "application/json", - "Access-Control-Allow-Origin": "*" - } - }); - } catch (err) { - return new Response(JSON.stringify({ error: "Shikimori fetch failed" }), { - status: 500, - headers: { - "Content-Type": "application/json" - } + } catch { + return new Response(JSON.stringify({ score: "N/A", characters: [] }), { + headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } }); } });