-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk_task.js
More file actions
36 lines (31 loc) · 1.23 KB
/
Copy pathbulk_task.js
File metadata and controls
36 lines (31 loc) · 1.23 KB
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
import { readFile } from "node:fs/promises";
const apiKey = process.env.TGVALIDATOR_API_KEY;
if (!apiKey) throw new Error("Set TGVALIDATOR_API_KEY");
const baseUrl = process.env.API_BASE_URL || "https://tgvalidator.com";
const product = process.env.PRODUCT || "tg_active_batch";
const country = process.env.COUNTRY || "US";
const path = process.env.FILE || "numbers.txt";
const form = new FormData();
form.set("product", product);
form.set("country", country);
form.set("file", new Blob([await readFile(path)]), "numbers.txt");
const created = await fetch(`${baseUrl}/api/v1/bulk-tasks`, {
method: "POST",
headers: { "X-API-Key": apiKey },
body: form,
});
if (!created.ok) throw new Error(await created.text());
const taskId = (await created.json()).data.id;
console.log("submitted", taskId);
// 轮询间隔不得短于 30 秒,这是服务端的契约而不是建议。
for (;;) {
await new Promise((r) => setTimeout(r, 30_000));
const status = await fetch(`${baseUrl}/api/v1/bulk-tasks/${taskId}`, { headers: { "X-API-Key": apiKey } });
if (!status.ok) throw new Error(await status.text());
const data = (await status.json()).data;
console.log(data.status);
if (data.status !== "processing") {
console.log(data);
break;
}
}