import { MediaUpload } from "@/models/media"; function fetchPixivData(url: string): Promise { // https://www.pixiv.net/artworks/143552616 const match = url.match(/\/artworks\/(\d+)/); if (!match) { throw new Error("Invalid Pixiv URL"); } const artworkId = match[1]; return fetch(`https://www.phixiv.net/api/info?id=${artworkId}&language=ko`) .then((response) => { if (!response.ok) { throw new Error(`Failed to fetch Pixiv data: ${response.status} ${response.statusText}`); } return response.json(); }) .then((data) => { // if #R-18 in tags, throw error if (data.tags && data.tags.some((tag: string) => tag.includes("R-18"))) { throw new Error("Pixiv artwork is marked as R-18"); } return data; }) .then((data) => { if (data.error) { throw new Error(`Pixiv API error: ${data.message}`); } return data; }); } async function checkPixivData(url: string, selected: Array) { const match = url.match(/\/artworks\/(\d+)/); if (!match) { throw new Error("Invalid Pixiv URL"); } const artworkId = match[1]; const selectedIndices = selected .map((isSelected, index) => (isSelected ? index : -1)) .filter((index) => index >= 0); if (selectedIndices.length === 0) { return false; } const existing = await MediaUpload.findOne({ "tweet.id": artworkId, mediaIndex: { $in: selectedIndices }, }); return existing !== null; } export { checkPixivData, fetchPixivData };