Merge pull request #5 from coding-lemur/feat/docker-support

feat: docker support
This commit is contained in:
Maurice Renck 2024-03-30 13:20:11 +01:00 committed by GitHub
commit 4f6c3ba325
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 53 additions and 12 deletions

5
.dockerignore Normal file
View file

@ -0,0 +1,5 @@
node_modules
npm-debug.log
.env
Dockerfile
.dockerignore

10
Dockerfile Normal file
View file

@ -0,0 +1,10 @@
FROM node:21-bookworm-slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "node", "main.js" ]

19
docker-compose.yml Normal file
View file

@ -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

31
main.js
View file

@ -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();
}
@ -85,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);