From 803c61620e645b6f6f3971d578dcd8f3480d9c3d Mon Sep 17 00:00:00 2001 From: karashiiro <49822414+karashiiro@users.noreply.github.com> Date: Fri, 7 Jul 2023 16:09:08 -0700 Subject: [PATCH] feat: throttle requests on 429 --- package.json | 2 +- src/api.ts | 34 ++++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index d7f055a3..5b2c865e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@the-convocation/twitter-scraper", - "version": "0.3.0", + "version": "0.3.1", "main": "dist/_module.js", "repository": "https://github.com/the-convocation/twitter-scraper.git", "author": "karashiiro <49822414+karashiiro@users.noreply.github.com>", diff --git a/src/api.ts b/src/api.ts index 524fcd10..d10da3ec 100644 --- a/src/api.ts +++ b/src/api.ts @@ -25,23 +25,29 @@ export async function requestApi( await auth.installTo(headers, url); let res: Response; - try { - res = await fetch(url, { - method, - headers, - }); - } catch (err) { - if (!(err instanceof Error)) { - throw err; + do { + try { + res = await fetch(url, { + method, + headers, + }); + } catch (err) { + if (!(err instanceof Error)) { + throw err; + } + + return { + success: false, + err: new Error('Failed to perform request.'), + }; } - return { - success: false, - err: new Error('Failed to perform request.'), - }; - } + await updateCookieJar(auth.cookieJar(), res.headers); - await updateCookieJar(auth.cookieJar(), res.headers); + if (res.status === 429) { + await new Promise((resolve) => setTimeout(resolve, 100)); + } + } while (res.status === 429); if (!res.ok) { return {