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!!!

46
bot.py
View file

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