| 11 | const chatId = context.chatId || message.key.remoteJid; |
| 12 | const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms)); |
| 13 | const apiCallWithRetry = async (url, maxRetries = 3, baseDelay = 2000) => { |
| 14 | for (let attempt = 1; attempt <= maxRetries; attempt++) { |
| 15 | try { |
| 16 | await wait(1000); |
| 17 | const response = await axios.get(url, { |
| 18 | timeout: 60000, |
| 19 | headers: { 'User-Agent': 'Mozilla/5.0' } |
| 20 | }); |
| 21 | return response; |
| 22 | } |
| 23 | catch (error) { |
| 24 | const isRateLimited = error.response?.status === 429 || |
| 25 | error.code === 'ECONNABORTED' || |
| 26 | error.code === 'ETIMEDOUT'; |
| 27 | if (attempt === maxRetries) |
| 28 | throw error; |
| 29 | if (isRateLimited) { |
| 30 | const delay = baseDelay * Math.pow(2, attempt - 1); |
| 31 | console.log(`Retrying in ${delay}ms... (${attempt}/${maxRetries})`); |
| 32 | await wait(delay); |
| 33 | } |
| 34 | else { |
| 35 | throw error; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | }; |
| 40 | const downloadFileWithProgress = async (url, filepath, maxRetries = 3) => { |
| 41 | for (let attempt = 1; attempt <= maxRetries; attempt++) { |
| 42 | try { |