wolfyflutter

google advance image search [google] [search] [image] [official api]

Jun 20th, 2025 (edited)
684
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.94 KB | Source Code | 1 0
  1. /*
  2.     base   : https://www.google.com/advanced_image_search
  3.     fungsi : buat cari gambar makek search enginie
  4.              google search,
  5.     by     : wolep
  6.     update : 21 Juli 2025 11.00 WITA
  7.     note   : biasa, single function, 1 param query
  8.              return objek json. easy to use, flexible,
  9.              handle return and error the way you like
  10.              uwu uwu uwu. skrep kalau rusak kasih tau
  11.              gw ya :v atau lu benerin sendiri :v
  12.     nodejs : v24.2.0
  13. */
  14.  
  15. const googleSearchImage = async (query) => {
  16.  
  17.     if (!query) throw Error(`kata pencarian tidak boleh kosong`)
  18.     const usp = {
  19.         "as_st": "y",
  20.         "as_q": query,
  21.         "as_epq": "",
  22.         "as_oq": "",
  23.         "as_eq": "",
  24.         "imgsz": "l", // image size result. (emptry string = any size) available size : l m i qsvga vga svga xga 2mp 4mp 6mp 8mp 10mp 12mp 15mp 20mp 40mp 70mp
  25.         "imgar": "",
  26.         "imgcolor": "",
  27.         "imgtype": "jpg", // image format (empty sting = any format) available format : jpg gif png bmp svg webp ico craw
  28.         "cr": "",
  29.         "as_sitesearch": "",
  30.         "as_filetype": "",
  31.         "tbs": "",
  32.         "udm": "2"
  33.     }
  34.  
  35.     const headers = {
  36.         "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"
  37.     }
  38.  
  39.     const response = await fetch("https://www.google.com/search?" + new URLSearchParams(usp).toString(), {
  40.         headers
  41.     })
  42.  
  43.     if (!response.ok) throw Error(`gagal hit api ${response.status} ${response.statusText}\n${await response.text() || null}`)
  44.  
  45.     const html = await response.text()
  46.     const match = html.match(/var m=(.*?);var a=m/)?.[1] || null
  47.     if (!match) throw Error("no match found!")
  48.     const json = JSON.parse(match)
  49.     const images = Object.entries(json).filter(v => v[1]?.[1]?.[3]?.[0]).map(v =>
  50.     ({
  51.         title: v[1]?.[1]?.[25]?.[2003]?.[3] || null,
  52.         imageUrl: v[1][1][3][0] || null,
  53.         height: v[1][1][3][1] || null,
  54.         width: v[1][1][3][2] || null,
  55.         imageSize: v[1]?.[1]?.[25]?.[2000]?.[2] || null,
  56.         referer: v[1]?.[1]?.[25]?.[2003]?.[2] || null,
  57.         aboutUrl: v[1]?.[1]?.[25]?.[2003]?.[33] || null
  58.     })
  59.     )
  60.  
  61.     if (!images.length) throw Error(`hasil pencarian ${query} kosong.`)
  62.     images.pop() // buang element akhir
  63.     return { total: images.length, images }
  64.  
  65. }
  66.  
  67. // cara pakai
  68. googleSearchImage("furina fanart")
  69.     .then(console.log)
  70.     .catch(err => console.log(err.message))
  71.  
  72. /* output (kalau lu search yang aneh aneh 18+ gitu baru metadatanya lengkap jir, gak null)
  73. {
  74.   total: 100,
  75.   images: [
  76.     {
  77.       title: null,
  78.       imageUrl: 'https://upload-os-bbs.hoyolab.com/upload/2023/11/26/173296285/c4e9249c76199448936878fb2990dc4f_6233603105822018002.jpg',
  79.       height: 1080,
  80.       width: 764,
  81.       imageSize: null,
  82.       referer: null,
  83.       aboutUrl: null
  84.     }, ... (99 objek lainnya)
  85.     ]
  86. */
  87.  
Add Comment
Please, Sign In to add comment