deno fmt main.ts

This commit is contained in:
arne 2023-01-05 17:57:05 +01:00
commit 76304ea34f

71
main.ts
View file

@ -1,25 +1,35 @@
const MASTODON_INSTANCE_URL = Deno.env.get("MASTODON_INSTANCE_URL"); // e.g. https://botsin.space
const MASTODON_ACCESS_TOKEN = Deno.env.get("MASTODON_ACCESS_TOKEN"); // you can get this at $INSTANCE_URL/settings/applications
if (MASTODON_INSTANCE_URL == null || MASTODON_INSTANCE_URL === '' || MASTODON_ACCESS_TOKEN == null || MASTODON_ACCESS_TOKEN === '') {
console.error('Please set MASTODON_INSTANE_URL and MASTODON_ACCESS_TOKEN!')
Deno.exit(1)
if (
MASTODON_INSTANCE_URL == null || MASTODON_INSTANCE_URL === "" ||
MASTODON_ACCESS_TOKEN == null || MASTODON_ACCESS_TOKEN === ""
) {
console.error("Please set MASTODON_INSTANE_URL and MASTODON_ACCESS_TOKEN!");
Deno.exit(1);
}
// some helpers to work with the mastodon api
const handleResponse = async (res: Response) => {
if (res.ok) return { response: res, data: await res.json() }
throw new Error(await res.text())
}
if (res.ok) return { response: res, data: await res.json() };
throw new Error(await res.text());
};
const api = (path: string, params: { body?: FormData | string, method?: string }) => fetch(`${MASTODON_INSTANCE_URL.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`, {
method: "POST",
headers: {
Authorization: `Bearer ${MASTODON_ACCESS_TOKEN}`,
},
...params,
}).then(handleResponse)
const api = (
path: string,
params: { body?: FormData | string; method?: string },
) =>
fetch(
`${MASTODON_INSTANCE_URL.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${MASTODON_ACCESS_TOKEN}`,
},
...params,
},
).then(handleResponse);
// first we need to find out which files we can post, and which ones we already
// have posted
@ -52,7 +62,7 @@ const fileToPost =
availableFiles[Math.floor(Math.random() * availableFiles.length)];
const bytes = await Deno.readFile(`./posts/${fileToPost}`);
console.log(`Will post file ${fileToPost}`)
console.log(`Will post file ${fileToPost}`);
// we need two requests
// 1. upload media
@ -63,30 +73,33 @@ const mediaData = new FormData();
mediaData.append("file", new Blob([bytes]));
console.log("Sending post request to", `${MASTODON_INSTANCE_URL}/api/v2/media`);
const { data: mediaRequest } = await api('/api/v2/media', { body: mediaData }) as unknown as { data: { id: string } };
const { data: mediaRequest } = await api("/api/v2/media", {
body: mediaData,
}) as unknown as { data: { id: string } };
console.log("mediaRequest", mediaRequest);
// now we have to wait until the media file is processed
const sleep = (milliseconds: number) => new Promise((resolve, reject) => {
setTimeout(resolve, milliseconds)
})
const sleep = (milliseconds: number) =>
new Promise((resolve, reject) => {
setTimeout(resolve, milliseconds);
});
const startTime = Date.now()
const startTime = Date.now();
while (true) {
if ((Date.now() - startTime) / 1000 >= 60) {
console.error('Timeout waiting for media to be processed.')
Deno.exit(1)
console.error("Timeout waiting for media to be processed.");
Deno.exit(1);
}
const { response } = await api(`/api/v1/media/${mediaRequest.id}`, {
method: 'GET'
})
method: "GET",
});
if (response.status === 200) {
console.log('Media finished processing!')
break
console.log("Media finished processing!");
break;
} else {
console.log('Media still processing…')
await sleep(5000)
console.log("Media still processing…");
await sleep(5000);
}
}
@ -98,7 +111,9 @@ console.log(
"Sending post request to",
`${MASTODON_INSTANCE_URL}/api/v1/statuses`,
);
const { data: statusRequest } = await api('/api/v1/statuses', { body: statusData });
const { data: statusRequest } = await api("/api/v1/statuses", {
body: statusData,
});
console.log("statusRequest", statusRequest);
console.log("Linking posted file so we can skip it in the future…");