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
let { twitterAPIDown } = await import("https://esm.town/v/stevekrouse/twitterAPIDown");
import process from "node:process";
import { msHour } from "https://esm.town/v/stevekrouse/msHour";
import { twitterSearch } from "https://esm.town/v/stevekrouse/twitterSearch";
// let's poll the elevated v2 twitter api
// until it's down, and store the state in
// @stevekrouse.twitterAPIDown ({ down: boolean, reason: string})
export async function checkIfTwitterAPIIsDown() {
try {
// query for something we know will return results
const results = await twitterSearch({
query: '"hello"',
start_time: new Date(Date.now() - 1 * msHour),
bearerToken: process.env.twitter,
});
if (results.length) {
// if we get results, the API is still up
twitterAPIDown = {
down: false,
reason: `search returned ${results.length} results at ${new Date()}`,
};
} else {
// no results = API down
twitterAPIDown = {
down: true,
reason: `search returned ${results.length} results at ${new Date()}`,
};
}
} catch (e) {
// if there's an error, the API is also likely down
twitterAPIDown = {
down: false,
reason: `search errored ${e.message} at ${new Date()}`,
};
throw e; // so it shows up in tracing as an error
}
}