Skip to content

Commit

Permalink
feat: throttle requests on 429 (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
karashiiro authored Jul 7, 2023
1 parent 4744540 commit 43686d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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>",
Expand Down
34 changes: 20 additions & 14 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,29 @@ export async function requestApi<T>(
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 {
Expand Down

0 comments on commit 43686d2

Please sign in to comment.