customizable instance url, default fetch int = 5, better startup handling
This commit is contained in:
parent
266d3e76d0
commit
4f52999317
|
@ -3,10 +3,10 @@ A Mastodon bot to post a feed of articles from The Verge - [@verge@wuff.space](h
|
|||
|
||||
# Setup
|
||||
## Docker
|
||||
**NOTE:** The Docker Compose file is located at `docker-compose.yml.example` in the root directory. If you want to run the bot using Docker Compose, refer to that file.
|
||||
**NOTE:** The Docker Compose file is located at `docker-compose.example.yml` in the root directory. If you want to run the bot using Docker Compose, refer to that file.
|
||||
Run the Docker container (remove the -d flag to run in the foreground)
|
||||
```bash
|
||||
docker run -d --env ACCESS_TOKEN=YOUR_ACCESS_TOKEN --name verge-mastodon-bot ghcr.io/milanmdev/verge-mastodon-bot
|
||||
docker run -d --env ACCESS_TOKEN=YOUR_ACCESS_TOKEN INSTANCE_URL=https://your-instance.url --name verge-mastodon-bot ghcr.io/milanmdev/verge-mastodon-bot
|
||||
```
|
||||
## Manual
|
||||
1. Clone the repository
|
||||
|
@ -20,6 +20,7 @@ yarn install
|
|||
3. Create a `.env` file in the root directory of the project and add the following environment variables:
|
||||
```bash
|
||||
ACCESS_TOKEN=YOUR_ACCESS_TOKEN
|
||||
INSTANCE_URL=https://your-instance.url
|
||||
```
|
||||
4. Run the bot
|
||||
```bash
|
||||
|
|
20
app/index.js
20
app/index.js
|
@ -1,5 +1,6 @@
|
|||
const FeedSub = require("feedsub");
|
||||
const { login } = require("masto");
|
||||
const process = require("process");
|
||||
require("dotenv").config();
|
||||
|
||||
if (!process.env.ACCESS_TOKEN) throw new Error("No access token provided.");
|
||||
|
@ -8,17 +9,22 @@ let fetch_url;
|
|||
if (!process.env.FETCH_URL)
|
||||
fetch_url = "https://www.theverge.com/rss/index.xml";
|
||||
else fetch_url = process.env.FETCH_URL;
|
||||
let instance_url;
|
||||
if (!process.env.INSTANCE_URL) instance_url = "https://wuff.space";
|
||||
else instance_url = process.env.INSTANCE_URL;
|
||||
let fetch_interval;
|
||||
if (!process.env.FETCH_INTERVAL) fetch_interval = 10;
|
||||
if (!process.env.FETCH_INTERVAL) fetch_interval = 5;
|
||||
else fetch_interval = process.env.FETCH_INTERVAL;
|
||||
|
||||
let launchItems = [];
|
||||
let reader = new FeedSub(fetch_url, {
|
||||
interval: fetch_interval,
|
||||
emitOnStart: true,
|
||||
});
|
||||
|
||||
reader.read();
|
||||
|
||||
reader.on("item", async (item) => {
|
||||
if (launchItems.includes(item.id)) return;
|
||||
if (process.uptime() < 60) return;
|
||||
console.log(
|
||||
`[${new Date().toUTCString()}] - [Mastodon] Posting new article (${
|
||||
item.title
|
||||
|
@ -26,7 +32,7 @@ reader.on("item", async (item) => {
|
|||
);
|
||||
|
||||
const masto = await login({
|
||||
url: "https://wuff.space",
|
||||
url: instance_url,
|
||||
accessToken: process.env.ACCESS_TOKEN,
|
||||
});
|
||||
|
||||
|
@ -39,12 +45,8 @@ reader.on("item", async (item) => {
|
|||
try {
|
||||
reader.start();
|
||||
console.log(
|
||||
`[${new Date().toUTCString()}] - [Verge RSS] Started RSS reader. Fetching from ${fetch_url}`
|
||||
`[${new Date().toUTCString()}] - [Verge RSS] Started RSS reader. Fetching from ${fetch_url} every ${fetch_interval} minutes.`
|
||||
);
|
||||
} catch (e) {
|
||||
console.log(`[${new Date().toUTCString()}] - [Verge RSS] ${e}`);
|
||||
}
|
||||
|
||||
reader.read(async function (err, item) {
|
||||
launchItems.push(item.id);
|
||||
});
|
||||
|
|
|
@ -5,4 +5,7 @@ services:
|
|||
image: ghcr.io/milanmdev/verge-mastodon-bot
|
||||
environment:
|
||||
- ACCESS_TOKEN=token
|
||||
- INSTANCE_URL=https://your-instance.url
|
||||
- FETCH_URL=https://www.theverge.com/rss/index.xml
|
||||
- FETCH_INTERVAL=5
|
||||
- NODE_ENV=production
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "verge-mastodon-bot",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.0",
|
||||
"main": "index.js",
|
||||
"repository": "https://github.com/milanmdev/verge-mastodon-bot.git",
|
||||
"author": "Milan Mehra <milan@milanm.org>",
|
||||
|
|
Loading…
Reference in a new issue