-
Notifications
You must be signed in to change notification settings - Fork 0
/
excuse.ts
37 lines (34 loc) · 1.04 KB
/
excuse.ts
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
import {
bold,
green,
red,
yellow,
} from "https://deno.land/[email protected]/fmt/colors.ts";
async function fetchDeveloperExcuse() {
try {
const response = await fetch("http://developerexcuses.com/");
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.status}`);
}
const html = await response.text();
// Extract excuse text from HTML using a regex pattern
const excuseMatch = html.match(/<a.*?>(.*?)<\/a>/);
if (excuseMatch && excuseMatch[1]) {
const excuse = excuseMatch[1];
console.log(green(bold("Excuse: ")) + yellow(excuse));
} else {
console.log(red("Unable to extract the developer excuse."));
}
} catch (error: unknown) {
if (error instanceof Error) {
console.error(red("Error fetching excuse: ") + bold(error.message));
} else {
console.error(
red("Exception fetching excuse: ") +
bold(JSON.stringify(error, null, " ")),
);
}
}
}
// Run the function to fetch and print the latest developer excuse
fetchDeveloperExcuse();