From 22c22ea9cfa2b4ad9f6f61c73942ea52f257b246 Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Wed, 28 Aug 2024 17:24:04 +0100 Subject: [PATCH] Add dockerfile, allow changing base data path to ./data/ --- Dockerfile | 13 +++++++++++++ bot.py | 52 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1d7ce0e --- /dev/null +++ b/Dockerfile @@ -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!!! \ No newline at end of file diff --git a/bot.py b/bot.py index c562c74..0031a42 100644 --- a/bot.py +++ b/bot.py @@ -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: