Ayogiakbar

Nano Banana

Sep 9th, 2025 (edited)
726
1
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require("axios");
  2. const { v4: uuidv4 } = require("uuid");
  3.  
  4. const BASE_URL = "https://visualgpt.io/api/v1/prediction";
  5. function generateCookie() {
  6.   const gaId = Math.floor(Math.random() * 1e10);
  7.   const gaSession = `GA1.1.${gaId}.${Date.now()}`;
  8.   const sboxGuid = Buffer.from(`${Date.now()}|${Math.random()}`).toString("base64");
  9.   const crispSession = `session_${uuidv4()}`;
  10.   const anonId = uuidv4();
  11.  
  12.   return `_ga=${gaSession}; _ga_PZ8PZQP57J=GS2.1.s${Date.now()}$o1$g0$t${Date.now()}$j60$l0$h0; sbox-guid=${sboxGuid}; crisp-client%2Fsession%2F${uuidv4()}=${crispSession}; anonymous_user_id=${anonId}`;
  13. }
  14.  
  15.  
  16.  
  17. function buildHeaders() {
  18.     const anonId = uuidv4();
  19.     const cookie = generateCookie();
  20.  
  21.     return {
  22.         post: {
  23.             "user-agent":
  24.                 "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
  25.             "content-type": "application/json; charset=UTF-8",
  26.             origin: "https://visualgpt.io",
  27.             referer: "https://visualgpt.io/ai-models/nano-banana",
  28.             cookie
  29.         },
  30.         get: {
  31.             "user-agent":
  32.                 "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
  33.             accept: "application/json, text/plain, */*",
  34.             origin: "https://visualgpt.io",
  35.             referer: "https://visualgpt.io/ai-models/nano-banana",
  36.             cookie
  37.         }
  38.     };
  39. }
  40.  
  41. async function generateImage(imageUrls, prompt) {
  42.     if (!prompt || typeof prompt !== "string") {
  43.         throw new Error("❌ Prompt kosong atau tidak valid");
  44.     }
  45.     if (!Array.isArray(imageUrls) || imageUrls.length < 1 || imageUrls.length > 2) {
  46.         throw new Error("❌ Harus kirim array berisi 1 atau 2 URL gambar");
  47.     }
  48.     if (imageUrls.some((u) => !u || typeof u !== "string")) {
  49.         throw new Error("❌ URL gambar tidak valid: " + JSON.stringify(imageUrls));
  50.     }
  51.  
  52.     const headers = buildHeaders();
  53.     let body = {
  54.         image_urls: imageUrls,
  55.         type: 61,
  56.         user_prompt: prompt,
  57.         sub_type: 2,
  58.         aspect_ratio: "",
  59.         num: ""
  60.     };
  61.  
  62.     if (imageUrls.length === 1) {
  63.         body.max_images = 1;
  64.         body.size = "";
  65.     }
  66.  
  67.     const postRes = await axios.post(`${BASE_URL}/handle`, body, {
  68.         headers: headers.post
  69.     });
  70.  
  71.     if (!postRes.data || postRes.data.code !== 100000) {
  72.         throw new Error("❌ Handle gagal: " + JSON.stringify(postRes.data));
  73.     }
  74.  
  75.     const sessionId = postRes.data.data.session_id;
  76.     const start = Date.now();
  77.  
  78.     while (true) {
  79.         await new Promise((r) => setTimeout(r, 3000));
  80.  
  81.         if (Date.now() - start > 60000) {
  82.             throw new Error("❌ Timeout menunggu hasil");
  83.         }
  84.  
  85.         const getRes = await axios.get(
  86.             `${BASE_URL}/get-status?session_id=${sessionId}`,
  87.             { headers: headers.get }
  88.         );
  89.  
  90.         if (getRes.data?.data?.status === "succeeded") {
  91.             const results = getRes.data.data.results;
  92.             if (!Array.isArray(results) || results.length === 0) {
  93.                 throw new Error("❌ Tidak ada hasil dari VisualGPT");
  94.             }
  95.  
  96.             const finalUrls = results.flatMap(r => r.urls || []);
  97.             return finalUrls;
  98.         }
  99.     }
  100. }
  101.  
  102. module.exports = { generateImage };
  103.  
Comments
Add Comment
Please, Sign In to add comment