| import axios from "axios"; |
| import { USER_AGENT, BASE_URL } from "../config.js"; |
| import { getCache, setCache } from "./cache.js"; |
|
|
| const client = axios.create({ |
| timeout: 20000, |
| headers: { |
| "User-Agent": USER_AGENT, |
| "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", |
| "Accept-Language": "id-ID,id;q=0.9,en;q=0.8", |
| "Referer": BASE_URL + "/", |
| }, |
| }); |
|
|
| export async function fetchHtml(url, { useCache = true, retries = 2 } = {}) { |
| if (useCache) { |
| const cached = getCache(url); |
| if (cached) return cached; |
| } |
|
|
| let lastErr; |
| for (let attempt = 0; attempt <= retries; attempt++) { |
| try { |
| const res = await client.get(url); |
| if (useCache) setCache(url, res.data); |
| return res.data; |
| } catch (err) { |
| lastErr = err; |
| if (attempt < retries) { |
| await new Promise((r) => setTimeout(r, 600 * (attempt + 1))); |
| } |
| } |
| } |
| throw lastErr; |
| } |
|
|