Run deno fmt
This commit is contained in:
parent
865db42af6
commit
01c8444ca3
1 changed files with 63 additions and 59 deletions
122
main.ts
122
main.ts
|
|
@ -1,97 +1,101 @@
|
||||||
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
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
console.log('Picking file to post…')
|
console.log("Picking file to post…");
|
||||||
const alreadyPostedFiles = new Set()
|
const alreadyPostedFiles = new Set();
|
||||||
for await (const dirEntry of Deno.readDir("./posts/.posted")) {
|
for await (const dirEntry of Deno.readDir("./posts/.posted")) {
|
||||||
if (dirEntry.isSymlink) {
|
if (dirEntry.isSymlink) {
|
||||||
alreadyPostedFiles.add(dirEntry.name)
|
alreadyPostedFiles.add(dirEntry.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const availableFiles = []
|
const availableFiles = [];
|
||||||
for await (const dirEntry of Deno.readDir("./posts/")) {
|
for await (const dirEntry of Deno.readDir("./posts/")) {
|
||||||
if (dirEntry.name === '.gitignore') continue
|
if (dirEntry.name === ".gitignore") continue;
|
||||||
|
|
||||||
if (dirEntry.isFile && !alreadyPostedFiles.has(dirEntry.name)) {
|
if (dirEntry.isFile && !alreadyPostedFiles.has(dirEntry.name)) {
|
||||||
availableFiles.push(dirEntry.name)
|
availableFiles.push(dirEntry.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Found ${availableFiles.length} files which are not yet posted`)
|
console.log(`Found ${availableFiles.length} files which are not yet posted`);
|
||||||
|
|
||||||
if (availableFiles.length === 0) {
|
if (availableFiles.length === 0) {
|
||||||
console.log('Nothing left to post at the moment. Bye!')
|
console.log("Nothing left to post at the moment. Bye!");
|
||||||
Deno.exit()
|
Deno.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileToPost = availableFiles[Math.floor(Math.random() * availableFiles.length)]
|
const fileToPost =
|
||||||
const bytes = await Deno.readFile(`./posts/${fileToPost}`)
|
availableFiles[Math.floor(Math.random() * availableFiles.length)];
|
||||||
|
const bytes = await Deno.readFile(`./posts/${fileToPost}`);
|
||||||
|
|
||||||
// we need two requests
|
// we need two requests
|
||||||
// 1. upload media
|
// 1. upload media
|
||||||
// 2. create status with media id
|
// 2. create status with media id
|
||||||
|
|
||||||
// see https://docs.joinmastodon.org/methods/media/#v2
|
// see https://docs.joinmastodon.org/methods/media/#v2
|
||||||
const mediaData = new FormData()
|
const mediaData = new FormData();
|
||||||
mediaData.append('file', new Blob([bytes]))
|
mediaData.append("file", new Blob([bytes]));
|
||||||
|
|
||||||
type MediaResponse = {
|
type MediaResponse = {
|
||||||
'id': string
|
"id": string;
|
||||||
'type': string
|
"type": string;
|
||||||
'url': string
|
"url": string;
|
||||||
'preview_url': string
|
"preview_url": string;
|
||||||
'remote_url': null,
|
"remote_url": null;
|
||||||
'text_url': string
|
"text_url": string;
|
||||||
'meta': {
|
"meta": {
|
||||||
'focus': {
|
"focus": {
|
||||||
'x': number
|
"x": number;
|
||||||
'y': number
|
"y": number;
|
||||||
},
|
};
|
||||||
'original': {
|
"original": {
|
||||||
'width': number
|
"width": number;
|
||||||
'height': number
|
"height": number;
|
||||||
'size': string
|
"size": string;
|
||||||
'aspect': number
|
"aspect": number;
|
||||||
},
|
};
|
||||||
'small': {
|
"small": {
|
||||||
'width': number
|
"width": number;
|
||||||
'height': number
|
"height": number;
|
||||||
'size': string
|
"size": string;
|
||||||
'aspect': number
|
"aspect": number;
|
||||||
}
|
};
|
||||||
},
|
};
|
||||||
'description': string
|
"description": string;
|
||||||
'blurhash': string
|
"blurhash": string;
|
||||||
}
|
};
|
||||||
|
|
||||||
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 mediaRequest = await fetch(`${MASTODON_INSTANCE_URL}/api/v2/media`, {
|
const mediaRequest = await fetch(`${MASTODON_INSTANCE_URL}/api/v2/media`, {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${MASTODON_ACCESS_TOKEN}`
|
Authorization: `Bearer ${MASTODON_ACCESS_TOKEN}`,
|
||||||
},
|
},
|
||||||
body: mediaData
|
body: mediaData,
|
||||||
}).then(res => res.json()) as unknown as MediaResponse
|
}).then((res) => res.json()) as unknown as MediaResponse;
|
||||||
console.log('mediaRequest', mediaRequest)
|
console.log("mediaRequest", mediaRequest);
|
||||||
|
|
||||||
// see https://docs.joinmastodon.org/methods/statuses/#create
|
// see https://docs.joinmastodon.org/methods/statuses/#create
|
||||||
const statusData = new FormData()
|
const statusData = new FormData();
|
||||||
statusData.append('media_ids[]', mediaRequest.id)
|
statusData.append("media_ids[]", mediaRequest.id);
|
||||||
|
|
||||||
console.log('Sending post request to', `${MASTODON_INSTANCE_URL}/api/v1/statuses`)
|
console.log(
|
||||||
|
"Sending post request to",
|
||||||
|
`${MASTODON_INSTANCE_URL}/api/v1/statuses`,
|
||||||
|
);
|
||||||
const statusRequest = await fetch(`${MASTODON_INSTANCE_URL}/api/v1/statuses`, {
|
const statusRequest = await fetch(`${MASTODON_INSTANCE_URL}/api/v1/statuses`, {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${MASTODON_ACCESS_TOKEN}`
|
Authorization: `Bearer ${MASTODON_ACCESS_TOKEN}`,
|
||||||
},
|
},
|
||||||
body: statusData
|
body: statusData,
|
||||||
}).then(res => res.json())
|
}).then((res) => res.json());
|
||||||
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…");
|
||||||
await Deno.symlink(`./posts/${fileToPost}`, `./posts/.posted/${fileToPost}`)
|
await Deno.symlink(`./posts/${fileToPost}`, `./posts/.posted/${fileToPost}`);
|
||||||
console.log('Done!')
|
console.log("Done!");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue