From 46ddcad0772765c419df7107748275b960be218a Mon Sep 17 00:00:00 2001 From: Juan Giraldo Date: Wed, 17 Apr 2024 14:26:43 -0500 Subject: [PATCH] Add R2 Integration --- src/Mastodon/index.ts | 11 ++++++++++- src/WednesdayMyDudes/index.ts | 26 ++++++++++++++++---------- src/index.ts | 5 +++-- wrangler.toml | 4 ++++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Mastodon/index.ts b/src/Mastodon/index.ts index b3ce398..908eb94 100644 --- a/src/Mastodon/index.ts +++ b/src/Mastodon/index.ts @@ -16,4 +16,13 @@ const postStatus = async (token: string, media_ids: string) => { return await response.json(); }; -export { getTimeLine, postStatus }; +const uploadMedia = async (token: string, media: any) => { + const FINAL_URL = `${BASE_URL}/media`; + let formData = new FormData(); + formData.append('file', media, 'wednesday.jpg'); + formData.append('description', 'It is wednesdat, my dudes. Big fat frog.'); + const response = await fetch(FINAL_URL, { headers: getHeaders(token), body: formData, method: 'POST' }); + return await response.json(); +}; + +export { getTimeLine, postStatus, uploadMedia }; diff --git a/src/WednesdayMyDudes/index.ts b/src/WednesdayMyDudes/index.ts index aad1d80..5c3b98f 100644 --- a/src/WednesdayMyDudes/index.ts +++ b/src/WednesdayMyDudes/index.ts @@ -1,17 +1,23 @@ import moment from 'moment'; -import { getTimeLine, postStatus } from '../Mastodon'; +import { getTimeLine, postStatus, uploadMedia } from '../Mastodon'; const DATE_FORMAT = 'YYYY-MM-DD'; -const postOnWednesday = async (token: string, media_ids: string) => { - if (moment().day() === 3) { - const [lastPost]: any = await getTimeLine(token); - const lastPostDate = lastPost ? moment(lastPost.created_at).format(DATE_FORMAT) : null; - if (!lastPostDate && lastPostDate !== moment().format(DATE_FORMAT)) { - await postStatus(token, media_ids); - return "It's Wednesday My Dudes"; - } else return 'Already Posted My Dude'; - } else return "It's Not Wednesday My Dudes"; +const postOnWednesday = async (token: string, media_ids: string, resources: R2Bucket) => { + const image = await resources.get('wednesday.jpg'); + if (image === null) return; + const response = await uploadMedia(token, await image.blob()); + console.log(response); + + // if (moment().day() === 3) { + // const [lastPost]: any = await getTimeLine(token); + // const lastPostDate = lastPost ? moment(lastPost.created_at).format(DATE_FORMAT) : null; + // if (!lastPostDate && lastPostDate !== moment().format(DATE_FORMAT)) { + // await postStatus(token, media_ids); + // return "It's Wednesday My Dudes"; + // } else return 'Already Posted My Dude'; + // } else return "It's Not Wednesday My Dudes"; + return 'HELLO'; }; export { postOnWednesday }; diff --git a/src/index.ts b/src/index.ts index 55d83f7..c76c84e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,17 +3,18 @@ import { postOnWednesday } from './WednesdayMyDudes'; export interface Env { MASTODON_TOKEN: string; MASTODON_WEDNESDAY_DUDE_MEDIA: string; + RESOURCES: R2Bucket; } export default { async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { const token: string = env.MASTODON_TOKEN; const media_ids: string = env.MASTODON_WEDNESDAY_DUDE_MEDIA; - return new Response(await postOnWednesday(token, media_ids)); + return new Response(await postOnWednesday(token, media_ids, env.RESOURCES)); }, async scheduled(request: Request, env: Env, ctx: ExecutionContext): Promise { const token: string = env.MASTODON_TOKEN; const media_ids: string = env.MASTODON_WEDNESDAY_DUDE_MEDIA; - return new Response(await postOnWednesday(token, media_ids)); + return new Response(await postOnWednesday(token, media_ids, env.RESOURCES)); }, }; diff --git a/wrangler.toml b/wrangler.toml index c8c6a85..dc17278 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -10,3 +10,7 @@ MASTODON_WEDNESDAY_DUDE_MEDIA = "112288098480431582" [triggers] crons = [ "0 8 * * wed" ] + +[[r2_buckets]] +binding = 'RESOURCES' +bucket_name = 'resources'