Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0 results #7

Open
aidulcandra opened this issue Jan 11, 2025 · 4 comments
Open

0 results #7

aidulcandra opened this issue Jan 11, 2025 · 4 comments

Comments

@aidulcandra
Copy link

It returns an empty array

@Akif9748
Copy link
Owner

I have no problems with test code. Can you give information about your OS, location. And when you try directly look at http://www.google.com/search?udm=2&tbm=isch&q=akif9748 , can you reach the site?

@aidulcandra
Copy link
Author

OS: FreeBSD
My server location: Poland
Can I reach the site? Yes

@Drack112
Copy link

Drack112 commented Jan 12, 2025

This is essentially Google returning a 429 (Too Many Requests) response. Unfortunately, the package doesn't include a method for caching results or bypassing Google's rate limit.

However, you can improve the caching system to avoid hitting the rate limit. I was using the lru-cache package to make requests, retrieve music artist icons, and display them in a Next.js app.

import { LRUCache } from 'lru-cache'

class Cache<K extends {} = string, V extends {} = string> {
  private cache: LRUCache<K, V>
  constructor() {
    this.cache = new LRUCache({
      max: 50,
      ttl: 1000 * 60 * 60 * 12,
    })
  }

  get(key: K) {
    const value = this.cache.get(key)

    return value
  }

  set(key: K, value: V) {
    this.cache.set(key, value)
  }

  has(key: K) {
    return this.cache.has(key)
  }
}

export const cache = new Cache()
async function getImage(query: string, index = 0) {
  const cacheKey = `${query}-${index}`
  const cachedResult = cache.get(cacheKey)

  if (cachedResult) {
    return cachedResult
  }

  const results = await gis(query, { query: { safe: 'on' } })
  console.log(`Found ${results.length} results for ${query}.`)

  const result = results[Math.min(index, results.length - 1)]

  if (result?.url) {
    const cleanUrl = result.url.replace(/&w=\d+&q=\d+/g, '')
    cache.set(cacheKey, cleanUrl)
    return cleanUrl
  }

  return undefined
}

@Akif9748
Copy link
Owner

cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants