add logging

This commit is contained in:
Travis Shears 2024-06-24 15:44:17 +02:00
parent cf01dea155
commit e8234e41e8
4 changed files with 230 additions and 12 deletions

View file

@ -1,6 +1,16 @@
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 baseURL = `https://gram.social/api/pixelfed/v1/accounts/`;
const accountID = `703621281309160235`;
import { microBlogBackend as pb } from "./pocketbase";
const pb = new MicroBlogBackend(logger);
type PixelFedPost = {
media_attachments: {
@ -68,14 +78,14 @@ const savePost = async (post: PixelFedPost) => {
};
const saveTags = async (post: PixelFedPost, postId: string) => {
console.log({ tags: post.tags }, "saving tags");
logger.info({ tags: post.tags }, "saving tags");
for (const tag of post.tags) {
await pb.setTag(tag.name, postId);
}
};
const saveImages = async (post: PixelFedPost, postId: string) => {
console.log({ images: post.media_attachments }, "saving images");
logger.info({ images: post.media_attachments }, "saving images");
for (const image of post.media_attachments) {
await pb.saveAndSetImage(
{ remoteURL: image.url, alt: image.description },
@ -84,12 +94,12 @@ const saveImages = async (post: PixelFedPost, postId: string) => {
}
};
exports.run = async () => {
exports.run = async (event: any, context: any) => {
withRequest(event, context);
const lastSavedPostId = await pb.getLatestPostId("pixelfed");
const posts = await getPostUntilId({ lastSavedId: lastSavedPostId });
const post = posts[0];
if (post) {
console.log({ post }, "saving post");
for (const post of posts) {
logger.info({ post }, "saving post");
const savedNewPost = await savePost(post);
if (savedNewPost) {
await saveTags(post, savedNewPost.id);

View file

@ -1,3 +1,4 @@
import type { Logger } from "pino";
import PocketBase from "pocketbase";
export type MicroBlogPostImage = {
@ -33,17 +34,17 @@ export type MicroBlogPost = {
};
};
class MicroBlogBackend {
export class MicroBlogBackend {
private pb: PocketBase;
private clientSetTime?: Date;
constructor() {
constructor(private logger: Logger) {
this.pb = new PocketBase("https://personal-pocket-base.fly.dev");
}
private async login() {
const pw = process.env.POCKET_BASE_PW!;
const userName = process.env.POCKET_BASE_USER!;
console.log({ userName }, "Logging in to pocketbase");
this.logger.info({ userName }, "Logging in to pocketbase");
await this.pb.collection("users").authWithPassword(userName, pw);
this.clientSetTime = new Date();
}
@ -137,6 +138,7 @@ class MicroBlogBackend {
.collection<MicroBlogPost>("micro_blog_posts")
.create(post);
}
this.logger.info({ existingPost }, "Found existing post");
return existingPost;
}
@ -198,5 +200,3 @@ class MicroBlogBackend {
return resultList;
}
}
export const microBlogBackend = new MicroBlogBackend();