wolfyflutter

39. www.craiyon.com [ai] [image] [search]

Jul 28th, 2025 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.06 KB | Source Code | 0 0
  1. /*
  2.   base   : https://www.craiyon.com/
  3.   fungsi : buat nyari gambar ai generated
  4.   by     : wolep
  5.   node   : v24.4.0
  6.   note   : result nya gw filter yg pro,
  7.            dan gw random biar gk bosen :v
  8. */
  9.  
  10. const aiImageSearch = async function (search) {
  11.     // validate param
  12.     if (typeof (search) !== "string" || !search.trim().length) throw Error(`search harus string dan gak boleh kosong!`)
  13.  
  14.     // url
  15.     const api = new URL(`https://api2.craiyon.com`)
  16.     api.pathname = (`/search`)
  17.     const searchParams = new URLSearchParams({
  18.         "text": search,
  19.         "max_results": "60" // dah cukup kayanya ini :v
  20.     })
  21.     api.search = searchParams
  22.  
  23.     // headers
  24.     const headers = {
  25.         "accept-encoding": "gzip, deflate, br, zstd",
  26.         "cookie": "_ga=GA1.2.1185680231.1753717473;"
  27.  
  28.     }
  29.  
  30.     // hit
  31.     const response = await fetch(api, { headers })
  32.     if (!response.ok) throw Error(`${response.status} ${response.statusText}\n${await response.text()}`)
  33.     let json = await response.json()
  34.  
  35.     // cek if images empty
  36.     if (!json.images.length) throw Error(`tidak ada gambar dari pencarian ${search}`)
  37.  
  38.     // polish value ✨🌻
  39.  
  40.     // filter images pro only
  41.     const proImages = json.images.filter(v => v.pro)
  42.     if (proImages.length) {
  43.         json.images = proImages
  44.     } else {
  45.         console.log(`gak ada gambar pro`)
  46.     }
  47.  
  48.     // added origin for download url value
  49.     const proImages_fixUrl = json.images.map(v => {
  50.         const url = `https://media.craiyon.com`
  51.         v.image_id = `${url}/${v.image_id}`
  52.         return v
  53.     })
  54.  
  55.     // shuffle the result with Fisher–Yates shuffle
  56.     // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
  57.     function shuffleArray(array) {
  58.         for (let i = array.length - 1; i >= 1; i--) {
  59.             const j = Math.floor(Math.random() * (i + 1));
  60.             [array[i], array[j]] = [array[j], array[i]];
  61.         }
  62.         return array;
  63.     }
  64.     const proImages_fixUrl_shuffle = shuffleArray(proImages_fixUrl)
  65.  
  66.     json.images = proImages_fixUrl_shuffle
  67.     return json
  68. }
  69.  
  70. // cara pakai
  71. aiImageSearch("neko anime camisole")
  72.     .then(data => {
  73.         // kalian serialize sendiri ya hehe, contoh output liat di bawah
  74.         console.log(data)
  75.     })
  76.     .catch(error => {
  77.         // handle error sesuka hati
  78.         console.log(`gagal mencari gambar AI\n${error.message}`)
  79.     })
  80.  
  81.  
  82. /* output
  83. {
  84.   "images": [
  85.     {
  86.       "alt": "Girl with pink hair, cat ears, and black mask.",
  87.       "ar": "1:1",
  88.       "category": [
  89.         "illustration",
  90.         "girl",
  91.         "digital art",
  92.         "anime"
  93.       ],
  94.       "height": 1024,
  95.       "id": "OmzphzIDRF6gsUc225DEmQ",
  96.       "image_id": "https://media.craiyon.com/2025-04-19/QuhymYUATh220qlOFWoDcg.webp",
  97.       "img_id": "2025-04-19/QuhymYUATh220qlOFWoDcg.webp",
  98.       "negative_prompt": "",
  99.       "pro": true,
  100.       "prompt": "a girl with animal ears and a black mask",
  101.       "style": "illustration",
  102.       "watermark": true,
  103.       "width": 1024
  104.     },
  105.    
  106.     ..... (value lainnya)
  107.    
  108.  
  109. */
Add Comment
Please, Sign In to add comment