From 333d26ada6e84c0dd4a693f7308b81f0117b7743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schimmelpfennig?= Date: Wed, 27 Mar 2024 19:43:00 +0100 Subject: [PATCH 1/3] move lastProcessedPostId.txt to data folder; some prettier format changes --- .../lastProcessedPostId.txt | 0 main.js | 29 ++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) rename lastProcessedPostId.txt => data/lastProcessedPostId.txt (100%) diff --git a/lastProcessedPostId.txt b/data/lastProcessedPostId.txt similarity index 100% rename from lastProcessedPostId.txt rename to data/lastProcessedPostId.txt diff --git a/main.js b/main.js index 9d12ae3..47285a4 100644 --- a/main.js +++ b/main.js @@ -12,7 +12,11 @@ const mastodonUser = process.env.MASTODON_USER; const agent = new BskyAgent({ service: process.env.BLUESKY_ENDPOINT }); // 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 let lastProcessedPostId = loadLastProcessedPostId(); @@ -56,28 +60,31 @@ function removeHtmlTags(input) { // Function to periodically fetch new Mastodon posts 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') - .filter(item => item.object.inReplyTo === null) - .reverse(); + const reversed = response.data.orderedItems + .filter((item) => item.object.type === "Note") + .filter((item) => item.object.inReplyTo === null) + .reverse(); let newTimestampId = 0; - - reversed.forEach(item => { + + reversed.forEach((item) => { const currentTimestampId = Date.parse(item.published); - if(currentTimestampId > newTimestampId) { + if (currentTimestampId > newTimestampId) { newTimestampId = currentTimestampId; } - if(currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) { + if (currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) { const text = removeHtmlTags(item.object.content); postToBluesky(text); } - }) + }); - if(newTimestampId > 0) { + if (newTimestampId > 0) { lastProcessedPostId = newTimestampId; saveLastProcessedPostId(); } From 927387ffb5e6a754d993448aae7c92fd017cc1a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schimmelpfennig?= Date: Wed, 27 Mar 2024 19:58:36 +0100 Subject: [PATCH 2/3] add env variable for interval time --- main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.js b/main.js index 47285a4..4d1b26e 100644 --- a/main.js +++ b/main.js @@ -92,4 +92,4 @@ async function fetchNewPosts() { fetchNewPosts(); // Fetch new posts every 5 minutes (adjust as needed) -setInterval(fetchNewPosts, 2 * 60 * 1000); +setInterval(fetchNewPosts, (process.env.INTERVAL_MINUTES ?? 5) * 60 * 1000); From c951284efb3af61667ac48b671029b188bd3436c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schimmelpfennig?= Date: Wed, 27 Mar 2024 19:58:49 +0100 Subject: [PATCH 3/3] add docker support --- .dockerignore | 5 +++++ Dockerfile | 10 ++++++++++ docker-compose.yml | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a7af302 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +.env +Dockerfile +.dockerignore \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0d7900c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM node:21-bookworm-slim + +WORKDIR /usr/src/app + +COPY package*.json ./ +RUN npm install + +COPY . . + +CMD [ "node", "main.js" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3e1a2e6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3' +services: + app: + image: host/mastodon-to-bluesky:latest + container_name: mastodon-to-bluesky + environment: + MASTODON_INSTANCE: 'https://mastodon.instance' + MASTODON_USER: 'username' + BLUESKY_ENDPOINT: 'https://bsky.social' + BLUESKY_HANDLE: 'USERNAME.bsky.social' + BLUESKY_PASSWORD: 'PASSWORD' + INTERVAL_MINUTES: 5 + volumes: + - mastodon-to-bluesky:/usr/src/app/data + restart: unless-stopped + +volumes: + mastodon-to-bluesky: + external: true \ No newline at end of file