const ARCHIVE_API_URL = "https://archive.org/wayback/available?url="; const ARCHIVE_SAVE_URL = "https://web.archive.org/save/"; /** * Check if a given URL exists in Archive.org. * @param {string} url - The URL to check. * @returns {Promise} - The archive URL if found, otherwise null. */ async function getArchiveUrl(url) { try { const response = await fetch(`${ARCHIVE_API_URL}${encodeURIComponent(url)}`); if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`); const data = await response.json(); return data.archived_snapshots?.closest?.url || null; } catch (error) { console.error(`❌ Archive.org API error: ${error.message}`); return null; } } /** * Request Archive.org to save the given URL. * @param {string} url - The URL to archive. * @returns {Promise} - The permalink of the archived page if successful, otherwise null. */ async function saveToArchive(url) { try { const response = await fetch(`${ARCHIVE_SAVE_URL}${encodeURIComponent(url)}`, { method: "POST" }); if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`); return response.url.includes("/save/") ? null : response.url; } catch (error) { console.error(`❌ Failed to save URL to Archive.org: ${error.message}`); return null; } } module.exports = { getArchiveUrl, saveToArchive };