Allow specifying a custom folder
This commit is contained in:
parent
76304ea34f
commit
2a66af3091
2 changed files with 14 additions and 7 deletions
|
|
@ -1,7 +1,9 @@
|
|||
# Mastodon Image Bot
|
||||
|
||||
This is a small program that posts an image from `./posts` every time you call it. You can call it like so:
|
||||
This is a small program that posts an image from a given folder every time you call it. You can call it like so:
|
||||
|
||||
```
|
||||
MASTODON_INSTANCE_URL=... MASTODON_ACCESS_TOKEN=... deno run --allow-env --allow-net --allow-write --allow-read main.ts
|
||||
MASTODON_INSTANCE_URL=... MASTODON_ACCESS_TOKEN=... deno run --allow-env --allow-net --allow-write --allow-read main.ts [folder]
|
||||
```
|
||||
|
||||
If you leave out `folder` when calling the script, it will try to look for a `posts` directory inside the folder where the script is located.
|
||||
|
|
|
|||
15
main.ts
15
main.ts
|
|
@ -1,3 +1,5 @@
|
|||
import * as path from 'https://deno.land/std@0.102.0/path/mod.ts';
|
||||
|
||||
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
|
||||
|
||||
|
|
@ -33,17 +35,19 @@ const api = (
|
|||
|
||||
// first we need to find out which files we can post, and which ones we already
|
||||
// have posted
|
||||
const scriptDir = path.dirname(path.fromFileUrl(Deno.mainModule))
|
||||
const imgDir = path.resolve(scriptDir, Deno.args[0] || 'posts')
|
||||
|
||||
console.log("Picking file to post…");
|
||||
const alreadyPostedFiles = new Set();
|
||||
for await (const dirEntry of Deno.readDir("./posts/.posted")) {
|
||||
for await (const dirEntry of Deno.readDir(`${imgDir}/.posted`)) {
|
||||
if (dirEntry.isSymlink) {
|
||||
alreadyPostedFiles.add(dirEntry.name);
|
||||
}
|
||||
}
|
||||
|
||||
const availableFiles = [];
|
||||
for await (const dirEntry of Deno.readDir("./posts/")) {
|
||||
for await (const dirEntry of Deno.readDir(imgDir)) {
|
||||
if (dirEntry.name === ".gitignore") continue;
|
||||
|
||||
if (dirEntry.isFile && !alreadyPostedFiles.has(dirEntry.name)) {
|
||||
|
|
@ -60,7 +64,7 @@ if (availableFiles.length === 0) {
|
|||
|
||||
const fileToPost =
|
||||
availableFiles[Math.floor(Math.random() * availableFiles.length)];
|
||||
const bytes = await Deno.readFile(`./posts/${fileToPost}`);
|
||||
const bytes = await Deno.readFile(`${imgDir}/${fileToPost}`);
|
||||
|
||||
console.log(`Will post file ${fileToPost}`);
|
||||
|
||||
|
|
@ -117,7 +121,8 @@ const { data: statusRequest } = await api("/api/v1/statuses", {
|
|||
console.log("statusRequest", statusRequest);
|
||||
|
||||
console.log("Linking posted file so we can skip it in the future…");
|
||||
// FIXME: This requires unrestricted `--allow-read` and `--allow-write` at the;
|
||||
// FIXME: This requires unrestricted `--allow-read` and `--allow-write`;
|
||||
// deno should provide a finer-grained permission prompt here
|
||||
await Deno.symlink(`../${fileToPost}`, `./posts/.posted/${fileToPost}`);
|
||||
// see https://github.com/denoland/deno/issues/9607
|
||||
await Deno.symlink(`../${fileToPost}`, `${imgDir}/.posted/${fileToPost}`);
|
||||
console.log("Done!");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue