File size: 2,010 Bytes
b650bb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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);
})();