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