goenning / google-indexing-script

Script to get your site indexed on Google in less than 48 hours
https://seogets.com
MIT License
6.95k stars 481 forks source link

RateLimited during url inspection #45

Open maximepvrt opened 7 months ago

maximepvrt commented 7 months ago

Currently, when conducting an inspection for indexing using the tool, I encountered an obstacle. It appears that there is a rate limit restriction of 2000 calls per day (https://support.google.com/webmasters/thread/240916045/429-quota-issue-googleapis-com-v1-urlinspection-index-inspect?hl=en), which significantly hampers the inspection process.

👍 Done, here's the status of all 14784 pages:
🚦 RateLimited: 14784 pages
{
  "error": {
    "code": 429,
    "message": "Quota exceeded for sc-domain:xxxxxxxxx.fr.",
    "status": "RESOURCE_EXHAUSTED"
  }
}

Proposed Enhancement

lundcm commented 6 months ago

I have the following locally as a bit of a hack but it works for my purposes. This is in utils.ts:

export async function fetchRetry(url: string, options: RequestInit, retries: number = 5) {
  try {
    const response = await fetch(url, options);
    if (response.status >= 500) {
      const body = await response.text();
      throw new Error(`Server error code ${response.status}\n${body}`);
    }

    if (response.status === 429) {
      console.log("Rate limited. Retrying in 5 seconds...");
      // Retry after 5 seconds if rate limited
      await new Promise((resolve) => setTimeout(resolve, 5000));

      // Retry the request ignoring the retries limit
      return fetchRetry(url, options, retries);
    }
    return response;
  } catch (err) {
    if (retries <= 0) {
      throw err;
    }
    return fetchRetry(url, options, retries - 1);
  }
}
muditjuneja commented 6 months ago

This works like a charm for me!! Thanks @lundcm