generated from philnash/bsky-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPostText.ts
88 lines (76 loc) · 3.01 KB
/
getPostText.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
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
import { parseStringPromise } from 'xml2js';
import * as fs from 'fs';
const filePath: string = 'current_status.txt';
function readFile(filePath: string): string | null {
try {
const data = fs.readFileSync(filePath, 'utf-8');
return data;
} catch (error) {
console.error(`Error reading file: ${error}`);
return null;
}
}
const fileContent = readFile(filePath);
function cleanText(input: string): string {
// Replace \r with \n
input = input.replace(/\r/g, '\n');
input = input.replace(/\n /g, '\n');
// Replace double spaces with a single space
input = input.replace(/ +/g, ' ');
return input;
}
export default async function getPostText(): Promise<string> {
// Fetch the XML file
// Test URL: https://www.opm.gov/xml/operatingstatus.xml?date=05/10/2013&markup=on
// Test URL (long message): https://www.opm.gov/xml/operatingstatus.xml?date=02/06/2025&markup=on
const response = await fetch("https://www.opm.gov/xml/operatingstatus.xml", {
headers: {
Accept: "application/xml", // Accept XML response
},
});
// Get the XML text
const xmlText = await response.text();
try {
// Parse the XML string using xml2js
const parsedData = await parseStringPromise(xmlText);
// Extract the "CurrentStatus" object
const currentStatus = parsedData?.CurrentStatus;
// Check if "CurrentStatus" exists
if (currentStatus) {
// Extract the first value from the AppliesTo and ShortStatusMessage arrays
const appliesTo = currentStatus.AppliesTo?.[0]?.trim() || ''; // First element in AppliesTo array
const longStatus = currentStatus.LongStatusMessage?.[0]?.trim() || ''; // First element in longStatusMessage array
const longStatusMessage = cleanText(longStatus)
const shortSummary = currentStatus.OperatingStatus?.[0]?.trim() || '';
const statusURL = currentStatus.Url?.[0]?.trim() || '';
// Concatenate the extracted values into a single string with newline separator
const result = `${appliesTo}\n\n${longStatusMessage}\n\n${statusURL}`;
const short_result = `${appliesTo}\n\n${shortSummary}\n\n${statusURL}`;
if(result != fileContent){
fs.writeFile(filePath, result, (err) => {
if (err) {
console.error("An error occurred while writing to the file:", err);
} else {
console.log("Text successfully written to", filePath);
}
})
console.log("Text is different, successfully updated");
if(result.length > 300){
console.log(short_result);
return short_result;
}else{
return result;}
} else {
console.error("Current status is identical to cached status");
return '';
};
// Return the concatenated string
} else {
console.error("CurrentStatus not found in XML.");
return ''; // Return an empty string if CurrentStatus is not found
}
} catch (error) {
console.error("Error parsing XML:", error);
throw error; // Optional: re-throw or handle error as appropriate
}
}