move lastProcessedPostId.txt to data folder; some prettier format changes

This commit is contained in:
René Schimmelpfennig 2024-03-27 19:43:00 +01:00
parent 2fe2ca478a
commit 333d26ada6
No known key found for this signature in database
GPG key ID: EABA8F8CCF842F0B
2 changed files with 18 additions and 11 deletions

29
main.js
View file

@ -12,7 +12,11 @@ const mastodonUser = process.env.MASTODON_USER;
const agent = new BskyAgent({ service: process.env.BLUESKY_ENDPOINT }); const agent = new BskyAgent({ service: process.env.BLUESKY_ENDPOINT });
// File to store the last processed Mastodon post ID // File to store the last processed Mastodon post ID
const lastProcessedPostIdFile = path.join(__dirname, "lastProcessedPostId.txt"); const lastProcessedPostIdFile = path.join(
__dirname,
"data",
"lastProcessedPostId.txt"
);
// Variable to store the last processed Mastodon post ID // Variable to store the last processed Mastodon post ID
let lastProcessedPostId = loadLastProcessedPostId(); let lastProcessedPostId = loadLastProcessedPostId();
@ -56,28 +60,31 @@ function removeHtmlTags(input) {
// Function to periodically fetch new Mastodon posts // Function to periodically fetch new Mastodon posts
async function fetchNewPosts() { async function fetchNewPosts() {
const response = await axios.get(`${mastodonInstance}/users/${mastodonUser}/outbox?page=true`); const response = await axios.get(
`${mastodonInstance}/users/${mastodonUser}/outbox?page=true`
);
const reversed = response.data.orderedItems.filter(item => item.object.type === 'Note') const reversed = response.data.orderedItems
.filter(item => item.object.inReplyTo === null) .filter((item) => item.object.type === "Note")
.reverse(); .filter((item) => item.object.inReplyTo === null)
.reverse();
let newTimestampId = 0; let newTimestampId = 0;
reversed.forEach(item => { reversed.forEach((item) => {
const currentTimestampId = Date.parse(item.published); const currentTimestampId = Date.parse(item.published);
if(currentTimestampId > newTimestampId) { if (currentTimestampId > newTimestampId) {
newTimestampId = currentTimestampId; newTimestampId = currentTimestampId;
} }
if(currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) { if (currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) {
const text = removeHtmlTags(item.object.content); const text = removeHtmlTags(item.object.content);
postToBluesky(text); postToBluesky(text);
} }
}) });
if(newTimestampId > 0) { if (newTimestampId > 0) {
lastProcessedPostId = newTimestampId; lastProcessedPostId = newTimestampId;
saveLastProcessedPostId(); saveLastProcessedPostId();
} }