diff options
| author | Fuwn <[email protected]> | 2025-09-07 03:44:41 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-09-07 03:44:41 -0700 |
| commit | 722c9e322b0dfb2be361e8c91692077f6eaa07f0 (patch) | |
| tree | c5076c1ca1e95125fe1f5b5ebae10ce5a674e5c0 /src | |
| parent | fix: Add rate-limit mitigation (diff) | |
| download | umabotdiscord-722c9e322b0dfb2be361e8c91692077f6eaa07f0.tar.xz umabotdiscord-722c9e322b0dfb2be361e8c91692077f6eaa07f0.zip | |
style(reddit): Fix lints
Diffstat (limited to 'src')
| -rw-r--r-- | src/reddit.ts | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/src/reddit.ts b/src/reddit.ts index 5c5ae0b..aaadcd2 100644 --- a/src/reddit.ts +++ b/src/reddit.ts @@ -35,34 +35,41 @@ export interface RedditResponse { type SortType = 'hot' | 'top'; -async function fetchWithRetry(url: string, maxRetries: number = 3): Promise<Response> { +async function fetchWithRetry( + url: string, + maxRetries: number = 3, +): Promise<Response> { for (let attempt = 0; attempt < maxRetries; attempt++) try { - await new Promise(resolve => setTimeout(resolve, Math.random() * 1000 + 500)); - + await new Promise((resolve) => + setTimeout(resolve, Math.random() * 1000 + 500), + ); + const response = await fetch(url, { headers: { - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', - 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', + 'User-Agent': + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', + Accept: + 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate, br', - 'DNT': '1', - 'Connection': 'keep-alive', + DNT: '1', + Connection: 'keep-alive', 'Upgrade-Insecure-Requests': '1', }, }); - + return response; } catch (error) { if (attempt === maxRetries - 1) throw error; - + const delay = Math.pow(2, attempt) * 1000 + Math.random() * 1000; console.log(`Attempt ${attempt + 1} failed, retrying in ${delay}ms ...`); - await new Promise(resolve => setTimeout(resolve, delay)); + await new Promise((resolve) => setTimeout(resolve, delay)); } - + throw new Error('Max retries exceeded'); } @@ -78,14 +85,21 @@ export async function fetchRedditPosts( try { const error = await response.text(); - - if (error.includes('You\'ve been blocked by network security') || - error.includes('blocked by network security')) - throw new Error('Reddit is blocking requests due to network security. This may be due to rate limiting or bot detection. Please try again later.'); + + if ( + error.includes("You've been blocked by network security") || + error.includes('blocked by network security') + ) + throw new Error( + 'Reddit is blocking requests due to network security. This may be due to rate limiting or bot detection. Please try again later.', + ); if (error) errorText = `${errorText} \n\n ${error}`; } catch (err) { - if (err instanceof Error && err.message.includes('blocked by network security')) + if ( + err instanceof Error && + err.message.includes('blocked by network security') + ) throw err; } |