diff --git a/README.md b/README.md index ec11afb..20f66a1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/main.ts b/main.ts index 51e03ab..a2f0e42 100644 --- a/main.ts +++ b/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!");