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

feat: throttle requests on 429 #12

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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