-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheute-vor.ts
More file actions
168 lines (137 loc) · 4.65 KB
/
heute-vor.ts
File metadata and controls
168 lines (137 loc) · 4.65 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { LOCATIONS } from "./locations.js";
const API_BASE = "https://api.wikimedia.org/feed/v1/wikipedia/de/onthisday/selected";
const USER_AGENT = "bot-heute-vor/1.0 (https://github.com; bot@example.com)";
const TOOT_DELAY_MS = 1_000*60*75; // 75 Minuten
interface WikiPage {
title: string;
content_urls: {
desktop: { page: string };
};
normalizedtitle: string;
}
interface SelectedEvent {
text: string;
year: number;
pages: WikiPage[];
}
interface ApiResponse {
selected: SelectedEvent[];
}
function findLocationHashtags(text: string): string[] {
return LOCATIONS
.filter((location) => text.includes(location))
.map((location) =>
`#${location.replace(/[\s-]/g, "").replace(/[äöüÄÖÜ]/g, (c) => ({ ä: "a", ö: "o", ü: "u", Ä: "A", Ö: "O", Ü: "U" })[c] ?? c).replace(/ß/g, "ss")}`,
);
}
const today = new Date();
const mm = String(today.getMonth() + 1).padStart(2, "0");
const dd = String(today.getDate()).padStart(2, "0");
const currentYear = new Date().getFullYear();
const monthName = today.toLocaleString("de-DE", { month: "long" });
const wikiDate = `${today.getDate()}._${monthName}`;
async function fetchOnThisDay(): Promise<SelectedEvent[]> {
const url = `${API_BASE}/${mm}/${dd}`;
const res = await fetch(url, {
headers: { "User-Agent": USER_AGENT },
});
if (!res.ok) {
throw new Error(`API-Fehler: ${res.status} ${res.statusText}`);
}
const data = (await res.json()) as ApiResponse;
return data.selected;
}
const MASTODON_LINK_LENGTH = 23;
const MAX_TOOT_LENGTH = Number(process.env["MAX_TOOT_LENGTH"]) || 500;
function mastodonLength(text: string): number {
// Mastodon counts every URL (http:// or https://) as 23 characters
return text.replace(/https?:\/\/\S+/g, "x".repeat(MASTODON_LINK_LENGTH)).length;
}
function buildTootParts(events: SelectedEvent[]): string[] {
const lines = events
.sort((a, b) => b.year - a.year)
.map((event) => {
const yearsAgo = currentYear - event.year;
const links = event.pages
.filter((p) => p.content_urls?.desktop?.page && !p.title.startsWith("Datei:"))
.map((p) => p.content_urls.desktop.page);
const text = event.text
.replace(/\u00AD/g, "") // soft hyphens
.replace(/\u200B/g, ""); // zero-width spaces
const locationTags = findLocationHashtags(text).join(" ");
const hashtags = locationTags ? `${locationTags} #HeuteVor #Wikipedia` : "#HeuteVor #Wikipedia";
const base = `Vor ${yearsAgo} Jahren: ${text}`;
let toot = `${base}\n${hashtags}`;
const includedLinks: string[] = [];
for (const link of links) {
includedLinks.push(link);
const candidate = `${base}\n${includedLinks.join("\n")}\n${hashtags}`;
if (mastodonLength(candidate) <= MAX_TOOT_LENGTH) {
toot = candidate;
} else {
break;
}
}
return toot;
});
return lines;
}
interface MastodonStatus {
id: string;
url: string;
content: string;
created_at: string;
}
async function postToMastodon(
status: string,
inReplyToId?: string
): Promise<MastodonStatus> {
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 url = `${instanceUrl}/api/v1/statuses`;
const body: Record<string, string> = { status, visibility: "public", language: "de" };
if (inReplyToId) {
body["in_reply_to_id"] = inReplyToId;
}
const res = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!res.ok) {
const text = await res.text();
throw new Error(`Mastodon-Fehler: ${res.status} ${res.statusText}\n${text}`);
}
return (await res.json()) as MastodonStatus;
}
async function main() {
const events = await fetchOnThisDay();
if (events.length === 0) {
console.log("Keine Ereignisse für heute gefunden.");
return;
}
const parts = buildTootParts(events);
let lastId: string | undefined;
for (let i = 0; i < parts.length; i++) {
const part = parts[i]!;
console.log(part);
console.log(`--- Zeichenanzahl: ${part.length} ---\n`);
const posted = await postToMastodon(part, lastId);
lastId = posted.id;
console.log(`Toot gepostet: ${posted.url}\n`);
console.log(`Warte ${TOOT_DELAY_MS}ms...\n`);
await new Promise((resolve) => setTimeout(resolve, TOOT_DELAY_MS));
}
}
main().catch((err) => {
console.error("Fehler:", err);
process.exit(1);
});