From adbc1f51337d40859042df49042617088a3dc9c1 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Thu, 22 May 2025 19:04:40 +0200 Subject: [PATCH] convert mastodon scraper to docker --- build.mjs | 6 ++++++ create_docker_image.sh | 11 +++++++++++ package.json | 7 +++++-- src/mastodon.ts | 14 +++++--------- 4 files changed, 27 insertions(+), 11 deletions(-) create mode 100755 create_docker_image.sh diff --git a/build.mjs b/build.mjs index 0b1f353..078e31d 100644 --- a/build.mjs +++ b/build.mjs @@ -17,3 +17,9 @@ await esbuild.build({ entryPoints: ["src/bluesky.ts"], outfile: "dist/bluesky.js", }); + +await esbuild.build({ + ...commonProps, + entryPoints: ["src/mastodon.ts"], + outfile: "dist/mastodon.js", +}); diff --git a/create_docker_image.sh b/create_docker_image.sh new file mode 100755 index 0000000..9775066 --- /dev/null +++ b/create_docker_image.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +set -e + +VERSION=$(jq '.version' < package.json | sed 's/"//g') + +IMAGE="853019563312.dkr.ecr.eu-central-1.amazonaws.com/micro-blog-fetchers:${VERSION}" +echo Building and deploying micro blog fetchers version: $VERSION; +# # Build the Docker image +docker buildx build --platform linux/amd64,linux/arm64 -t $IMAGE . +docker push $IMAGE diff --git a/package.json b/package.json index c59738c..193c459 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,11 @@ { "name": "micro_blog_fetchers", - "version": "1.1.0", + "version": "1.2.0", "scripts": { - "dev:nostr": "node build.mjs && node ./dist/nostr.js | pino-pretty" + "dev:nostr": "node build.mjs && node ./dist/nostr.js | pino-pretty", + "dev:bluesky": "node build.mjs && node ./dist/bluesky.js | pino-pretty", + "dev:mastodon": "node build.mjs && node ./dist/mastodon.js | pino-pretty", + "build": "node build.mjs" }, "devDependencies": { "@types/node": "^20.14.8", diff --git a/src/mastodon.ts b/src/mastodon.ts index 63d13c6..ef14cc6 100644 --- a/src/mastodon.ts +++ b/src/mastodon.ts @@ -1,11 +1,8 @@ import pino from "pino"; -import { lambdaRequestTracker, pinoLambdaDestination } from "pino-lambda"; import { MicroBlogBackend } from "./pocketbase"; // custom destination formatter -const destination = pinoLambdaDestination(); -const logger = pino({}, destination); -const withRequest = lambdaRequestTracker(); +const logger = pino(); const pb = new MicroBlogBackend(logger); @@ -39,7 +36,7 @@ const getPostUntilId = async ({ const params = new URLSearchParams(); params.append("limit", "10"); const urlWithParams = new URL( - `${baseURL}/api/v1/accounts/${accountId}/statuses` + `${baseURL}/api/v1/accounts/${accountId}/statuses`, ); urlWithParams.search = params.toString(); @@ -86,13 +83,12 @@ const saveImages = async (post: MastodonPost, postId: string) => { for (const image of post.media_attachments) { await pb.saveAndSetImage( { remoteURL: image.url, alt: image.description }, - postId + postId, ); } }; -exports.run = async (event: any, context: any) => { - withRequest(event, context); +(async () => { const lastSavedPostId = await pb.getLatestPostRemoteIDBySource("mastodon"); console.log({ lastSavedPostId }); const posts = await getPostUntilId({ lastSavedId: lastSavedPostId }); @@ -104,4 +100,4 @@ exports.run = async (event: any, context: any) => { await saveTags(post, savedNewPost.id); await saveImages(post, savedNewPost.id); } -}; +})();