-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
104 lines (90 loc) · 3.45 KB
/
Copy pathindex.mjs
File metadata and controls
104 lines (90 loc) · 3.45 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const apiKey = process.env.SOCQ_API_KEY;
const baseUrl = (process.env.SOCQ_BASE_URL || "https://api.socq.ai").replace(/\/+$/, "");
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const payloadPath = resolve(process.argv[2] || resolve(root, "payload.example.json"));
const outputPath = resolve(root, "output", "results.json");
if (!apiKey || apiKey === "your-api-key") {
throw new Error("Set SOCQ_API_KEY before running this example.");
}
const sleep = (milliseconds) => new Promise((resolveSleep) => setTimeout(resolveSleep, milliseconds));
async function request(path, init = {}, attempt = 0) {
const response = await fetch(`${baseUrl}${path}`, {
...init,
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
...init.headers,
},
});
const text = await response.text();
let body = null;
try {
body = text ? JSON.parse(text) : null;
} catch {
body = null;
}
if ((response.status === 429 || response.status >= 500) && attempt < 3) {
const retryAfter = Number(response.headers.get("retry-after"));
await sleep(Number.isFinite(retryAfter) ? retryAfter * 1000 : 1000 * 2 ** attempt);
return request(path, init, attempt + 1);
}
if (!response.ok) {
const message = body?.error?.message || body?.message || `HTTP ${response.status}`;
throw new Error(`SocQ request failed: ${message}`);
}
return body;
}
const payload = JSON.parse(await readFile(payloadPath, "utf8"));
const submitted = await request("/v1/threads/posts", {
method: "POST",
body: JSON.stringify(payload),
});
const taskId = submitted?.data?.task_id;
if (!taskId) throw new Error("Submit response did not include data.task_id.");
console.log(`Submitted Threads Posts API task ${taskId}.`);
const deadline = Date.now() + 10 * 60 * 1000;
let task;
while (Date.now() < deadline) {
const response = await request(`/v1/tasks/${encodeURIComponent(taskId)}?limit=100`);
task = response?.data;
if (task?.status === "succeeded") break;
if (task?.status === "failed") {
throw new Error(task.error_message || `SocQ task ${taskId} failed.`);
}
await sleep(2000);
}
if (task?.status !== "succeeded") {
throw new Error(`Timed out waiting for SocQ task ${taskId}.`);
}
const items = [...(task.results?.items || [])];
let cursor = task.results?.next_cursor;
let hasMore = task.results?.has_more === true;
while (hasMore && cursor) {
const query = new URLSearchParams({ limit: "100", cursor });
const page = (await request(`/v1/tasks/${encodeURIComponent(taskId)}?${query}`))?.data;
items.push(...(page?.results?.items || []));
cursor = page?.results?.next_cursor;
hasMore = page?.results?.has_more === true;
}
const deduplicatedItems = [];
const seenItems = new Set();
for (const item of items) {
const key = item?.id ? String(item.id) : JSON.stringify(item);
if (seenItems.has(key)) continue;
seenItems.add(key);
deduplicatedItems.push(item);
}
const output = {
endpoint: "/v1/threads/posts",
task_id: taskId,
status: task.status,
result_count: deduplicatedItems.length,
collected_at: new Date().toISOString(),
items: deduplicatedItems,
};
await mkdir(dirname(outputPath), { recursive: true });
await writeFile(outputPath, `${JSON.stringify(output, null, 2)}\n`);
console.log(`Saved ${deduplicatedItems.length} records to ${outputPath}.`);