/*** @ Base: https://magicstudio.com/ @ Author: Shannz @ Note: Using catbox.moe for image output because the raw output is a buffer not json. ***/ import axios from 'axios'; import FormData from 'form-data'; import fs from 'fs'; import path from 'path'; import { v4 as uuidv4 } from 'uuid'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); function generateClientId(length = 40) { const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; let id = ''; for (let i = 0; i < length; i++) { id += chars.charAt(Math.floor(Math.random() * chars.length)); } return id; } export async function magicstudio(prompt) { const anonymousUserId = uuidv4(); const requestTimestamp = (Date.now() / 1000).toFixed(3); const clientId = generateClientId(); const tempPath = path.join(__dirname, 'temp_image.jpg'); const formData = new FormData(); formData.append('prompt', prompt); formData.append('output_format', 'bytes'); formData.append('user_profile_id', 'null'); formData.append('anonymous_user_id', anonymousUserId); formData.append('request_timestamp', requestTimestamp); formData.append('user_is_subscribed', 'false'); formData.append('client_id', clientId); const config = { method: 'POST', url: 'https://ai-api.magicstudio.com/api/ai-art-generator', headers: { ...formData.getHeaders(), 'User-Agent': 'ScRaPe/9.9 (KaliLinux; Nusantara Os; My/Shannz)', 'Accept': 'application/json, text/plain, */*', 'sec-ch-ua-mobile': '?1', 'sec-ch-ua-platform': '"Android"', 'origin': 'https://magicstudio.com', 'sec-fetch-site': 'same-site', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': 'empty', 'referer': 'https://magicstudio.com/ai-art-generator/', 'accept-language': 'en-US,en;q=0.9' }, responseType: 'arraybuffer', data: formData }; try { const response = await axios.request(config); fs.writeFileSync(tempPath, response.data); const uploadForm = new FormData(); uploadForm.append('reqtype', 'fileupload'); uploadForm.append('fileToUpload', fs.createReadStream(tempPath)); const upload = await axios.post('https://catbox.moe/user/api.php', uploadForm, { headers: uploadForm.getHeaders() }); fs.unlinkSync(tempPath); return upload.data; } catch (error) { console.error('Error :', error); return 'Gagal membuat atau mengupload gambar.'; } }