Skip to content

Commit

Permalink
Add R2 Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
juanegiraldoa committed Apr 17, 2024
1 parent 540a0c3 commit 46ddcad
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
11 changes: 10 additions & 1 deletion src/Mastodon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
26 changes: 16 additions & 10 deletions src/WednesdayMyDudes/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> {
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<Response> {
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));
},
};
4 changes: 4 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ MASTODON_WEDNESDAY_DUDE_MEDIA = "112288098480431582"

[triggers]
crons = [ "0 8 * * wed" ]

[[r2_buckets]]
binding = 'RESOURCES'
bucket_name = 'resources'

0 comments on commit 46ddcad

Please sign in to comment.