-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight-repost.ts
More file actions
93 lines (74 loc) · 2.52 KB
/
highlight-repost.ts
File metadata and controls
93 lines (74 loc) · 2.52 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
const instanceUrl = process.env["MASTODON_INSTANCE_URL"];
const accessToken = process.env["MASTODON_ACCESS_TOKEN"];
if (!instanceUrl || !accessToken) {
throw new Error(
"MASTODON_INSTANCE_URL und MASTODON_ACCESS_TOKEN müssen gesetzt sein"
);
}
const headers = { Authorization: `Bearer ${accessToken}` };
interface MastodonAccount {
id: string;
username: string;
}
interface MastodonStatus {
id: string;
url: string;
created_at: string;
content: string;
favourites_count: number;
reblogs_count: number;
}
async function getAccountId(): Promise<string> {
const res = await fetch(`${instanceUrl}/api/v1/accounts/verify_credentials`, {
headers,
});
if (!res.ok) {
throw new Error(`Fehler beim Abrufen des Accounts: ${res.status}`);
}
const account = (await res.json()) as MastodonAccount;
return account.id;
}
async function getTootsFromToday(accountId: string): Promise<MastodonStatus[]> {
const today = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
const res = await fetch(
`${instanceUrl}/api/v1/accounts/${accountId}/statuses?limit=40&exclude_reblogs=true`,
{ headers }
);
if (!res.ok) {
throw new Error(`Fehler beim Abrufen der Toots: ${res.status}`);
}
const statuses = (await res.json()) as MastodonStatus[];
return statuses.filter((s) => s.created_at.startsWith(today));
}
async function reblog(statusId: string): Promise<MastodonStatus> {
const res = await fetch(
`${instanceUrl}/api/v1/statuses/${statusId}/reblog`,
{ method: "POST", headers }
);
if (!res.ok) {
const body = await res.text();
throw new Error(`Reblog-Fehler: ${res.status}\n${body}`);
}
return (await res.json()) as MastodonStatus;
}
async function main() {
const accountId = await getAccountId();
const toots = await getTootsFromToday(accountId);
if (toots.length === 0) {
console.log("Keine Toots von heute gefunden.");
return;
}
const highlight = toots.reduce((best, current) => {
const bestScore = best.favourites_count + best.reblogs_count;
const currentScore = current.favourites_count + current.reblogs_count;
return currentScore > bestScore ? current : best;
});
const score = highlight.favourites_count + highlight.reblogs_count;
console.log(`Highlight: ${highlight.url} (${highlight.favourites_count} Favs + ${highlight.reblogs_count} Boosts = ${score})`);
const reposted = await reblog(highlight.id);
console.log(`Repost erfolgreich: ${reposted.url}`);
}
main().catch((err) => {
console.error("Fehler:", err);
process.exit(1);
});