Skip to content

Commit

Permalink
Add diferents images on special dates
Browse files Browse the repository at this point in the history
  • Loading branch information
juanegiraldoa committed Apr 26, 2024
1 parent b375243 commit 1534a62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Mastodon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const postStatus = async (token: string, media_ids: string) => {
return await response.json();
};

const uploadMedia = async (token: string, media: any) => {
const uploadMedia = async (token: string, media: Blob, description: string) => {
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.');
formData.append('description', description);
const response = await fetch(FINAL_URL, { headers: getHeaders(token), body: formData, method: 'POST' });
return await response.json();
};
Expand Down
26 changes: 22 additions & 4 deletions src/WednesdayMyDudes/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import { getTimeLine, postStatus, uploadMedia } from '../Mastodon';

const wednesdayPosts = {
defaultWednesday: { image: 'wednesday.jpg', description: 'It is Wednesday, my dudes. Big fat frog.' },
wednesdayOctober: { image: 'october-wednesday.jpg', description: 'It is Wednesday, my dudes. Big skeleton frog.' },
lastWednesdayOctober: {
image: 'spoktober-wednesday.jpg',
description: 'It is Hallowednesday, my doots. Big skeleton frog with trumpet.',
},
};

const postOnWednesday = async (token: string, resources: R2Bucket) => {
const now = new Date();
if (now.getDay() === 3) {
if (now.getUTCDay() === 3) {
const [lastPost]: any = await getTimeLine(token);
if (!lastPost || new Date(lastPost.created_at).getDate() !== now.getDate()) {
const image = await resources.get('wednesday.jpg');
if (image === null) return;
const { id }: any = await uploadMedia(token, await image.blob());
const info = getImageInfo(now);
const image = await resources.get(info.image);
if (image === null) return 'Wednesday Not Found';
const { id }: any = await uploadMedia(token, await image.blob(), info.description);
await postStatus(token, id);
return "It's Wednesday My Dudes";
} else return 'Already Posted My Dude';
} else return "It's Not Wednesday My Dudes";
};

const getImageInfo = (date: Date) => {
if (date.getUTCMonth() === 9) {
if (date.getUTCDate() + 7 > 31) return wednesdayPosts.lastWednesdayOctober;
return wednesdayPosts.wednesdayOctober;
}
return wednesdayPosts.defaultWednesday;
};

export { postOnWednesday };

0 comments on commit 1534a62

Please sign in to comment.