Add dockerfile, allow changing base data path to ./data/

This commit is contained in:
Nexus 2024-08-28 17:24:04 +01:00
parent a5b305bef9
commit 22c22ea9cf
2 changed files with 50 additions and 15 deletions

13
Dockerfile Normal file
View file

@ -0,0 +1,13 @@
FROM python:3.12-slim
ENV PYTHONUNBUFFERED=1
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y && apt-get install -y python3-pip
WORKDIR /app
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt
COPY bot.py /app/
CMD ["python", "bot.py"]
# DONT FORGET TO MOUNT BOT.LOG, CONFIG.INI, AND .LASTFETCH TO /APP OR /APP/DATA!!!

52
bot.py
View file

@ -4,12 +4,18 @@ import sys
import logging
import random
from configparser import ConfigParser
from pathlib import Path
from misskey import Misskey
from misskey.exceptions import MisskeyAPIException
BASE = Path.cwd()
if (BASE / "data").exists():
BASE = BASE / "data"
print("Data/ directory found. Using it as base.")
logging.basicConfig(
filename="bot.log",
filename=BASE / "bot.log",
filemode="a",
level=getattr(logging, os.getenv("LOGLEVEL", "INFO")),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
@ -17,7 +23,7 @@ logging.basicConfig(
logger = logging.getLogger("fedibot")
config = ConfigParser()
config.read("config.ini")
config.read(str(BASE / "config.ini"))
server_domain = list(config.keys())[1]
api_key = config[server_domain]["api_key"]
@ -26,7 +32,7 @@ limit = int(config[server_domain].get("limit", 100))
bot = Misskey(server_domain, api_key)
with open("./copy.txt") as _fd:
with open(BASE / "copy.txt") as _fd:
copy = _fd.read()
@ -34,7 +40,7 @@ def main():
print("Bot running.")
while True:
try:
with open("./.lastfetch", "r") as fd:
with open(BASE / ".lastfetch", "r") as fd:
lastfetch = fd.read()
logger.debug(f"Last fetch: {lastfetch}")
except FileNotFoundError:
@ -51,7 +57,7 @@ def main():
)
except MisskeyAPIException as e:
logger.error("Failed to fetch notifications: %r", e, exc_info=True)
retry = random.uniform(0, 2 ** i)
retry = random.uniform(0, 2**i)
logger.warning("Retrying in %.2f seconds...", retry)
time.sleep(retry)
else:
@ -75,17 +81,33 @@ def main():
visible_user_ids=[notification["user"]["id"]],
)
logger.info("Successfully replied to post %r", notification["note"]["id"])
except MisskeyAPIException as e:
logger.error("Failed to reply to post %r: %r", notification["note"]["id"], e, exc_info=True)
try:
logger.info(
"Reacting with the inbox emoji to post %r",
notification["note"]["id"],
)
bot.notes_reactions_create(note_id=notification["note"]["id"], reaction="📥")
logger.info(
"Successfully reacted with the inbox emoji to post %r",
notification["note"]["id"],
)
except MisskeyAPIException:
logger.error(
"Failed to react with the inbox emoji to post %r",
notification["note"]["id"],
exc_info=True,
)
try:
logger.info("Reacting with the inbox emoji to post %r", notification["note"]["id"])
bot.notes_reactions_create(note_id=notification["note"]["id"], reaction="📥")
logger.info("Successfully reacted with the inbox emoji to post %r", notification["note"]["id"])
except MisskeyAPIException:
logger.error("Failed to react with the inbox emoji to post %r", notification["note"]["id"], exc_info=True)
with open("./.lastfetch", "w") as fd:
fd.write(notification["id"])
# Save that we replied to this post
with open(BASE / ".lastfetch", "w") as fd:
fd.write(notification["id"])
except MisskeyAPIException as e:
logger.error(
"Failed to reply to post %r: %r",
notification["note"]["id"],
e,
exc_info=True,
)
try:
time.sleep(sleep_time)
except KeyboardInterrupt: