Spaces:
Running
Running
require("dotenv/config"); | |
const fs = require("fs"); | |
const fetch = require("node-fetch"); | |
const { promisify } = require("util"); | |
const key = "Abr0M3fvrtOxT09MS1ZwmUq"; // kunci untuk ambil token dari endpoint CORS | |
// Ambil daftar akun dari file ./account | |
const accounts = fs | |
.readFileSync("./account", "utf8") | |
.split("\n") | |
.map((a) => a.trim()) | |
.filter((a) => a && !a.includes("!")); | |
// Rebuild via Hugging Face API | |
const rebuildViaApi = async (username, token) => { | |
const repo = process.env.REPO_NAME; | |
console.log(`π Rebuilding via API for ${username}/${repo}`); | |
const res = await fetch( | |
`https://huggingface.co/api/spaces/${username}/${repo}/restart?factory=true`, | |
{ | |
method: "POST", | |
headers: { | |
Authorization: `Bearer ${token}`, | |
}, | |
} | |
); | |
if (!res.ok) { | |
const txt = await res.text(); | |
throw new Error( | |
`Gagal restart ${username}/${repo} β ${res.status}: ${txt}` | |
); | |
} | |
console.log(`β API restart success for ${username}/${repo}`); | |
}; | |
// Ambil token dari endpoint cors akun | |
const getToken = async (username) => { | |
const url = `https://${username}-cors.hf.space/ev?key=${key}&q=process.env.TOKEN`; | |
const token = await fetch(url).then((res) => res.text()); | |
return token.trim(); | |
}; | |
// Main Executor | |
(async () => { | |
const customAccounts = process.argv.slice(2); | |
const targetAccounts = customAccounts.length > 0 ? customAccounts : accounts; | |
const mode = process.env.MODE || "push"; | |
console.log(`π§© Mode: ${mode}`); | |
console.log(`π€ Target accounts:`, targetAccounts); | |
for (const account of targetAccounts) { | |
try { | |
const token = await getToken(account); | |
if (!token || token.length < 20) { | |
throw new Error(`Token invalid for ${account}`); | |
} | |
await rebuildViaApi(account, token); | |
console.log(`β Success for ${account}`); | |
} catch (err) { | |
console.error(`β Failed for ${account}:`, err.message); | |
} | |
} | |
setTimeout(() => process.exit(), 3000); | |
})(); | |