|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
import toml
|
|
|
|
from peewee import SqliteDatabase
|
|
|
|
|
|
|
|
import db_modell as db
|
|
|
|
from telegram_api import telegram_bot_api as api
|
|
|
|
|
|
|
|
|
|
|
|
def load_config():
|
|
|
|
configfile = os.path.join(SKRIPTPFAD, "config_fotobot.toml")
|
|
|
|
with open(configfile) as conffile:
|
|
|
|
config = toml.loads(conffile.read())
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
SKRIPTPFAD = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
CONFIG = load_config()
|
|
|
|
# Nur für Testzwecke!
|
|
|
|
CONFIG["root_verzeichnis"] = SKRIPTPFAD
|
|
|
|
|
|
|
|
db.DB_PROXY.initialize(SqliteDatabase(os.path.join(SKRIPTPFAD, "datenbank.db3")))
|
|
|
|
|
|
|
|
|
|
|
|
class FotoBot(api.Bot):
|
|
|
|
def __init__(self, token):
|
|
|
|
super().__init__(token)
|
|
|
|
|
|
|
|
self.menue = None
|
|
|
|
self.umenue = None
|
|
|
|
|
|
|
|
self.bot_kommando = {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
def message_handler(self, message):
|
|
|
|
if self.menue is not None:
|
|
|
|
self.menue(message)
|
|
|
|
return
|
|
|
|
|
|
|
|
def bot_kommando_exec(self, kommando):
|
|
|
|
self.menue = self.bot_kommando.get(kommando, self.kommando_unbekannt)
|
|
|
|
self.menue()
|
|
|
|
|
|
|
|
def kommando_unbekannt(self, message):
|
|
|
|
print("Unbekannt")
|
|
|
|
|
|
|
|
|
|
|
|
def verzeichnis_erstellen(ordner):
|
|
|
|
verzeichnis = os.path.join(CONFIG["root_verzeichnis"], *ordner)
|
|
|
|
os.makedirs(verzeichnis, mode=0o660, exist_ok=True)
|
|
|
|
return verzeichnis
|
|
|
|
|
|
|
|
|
|
|
|
def get_biggest_size(photos):
|
|
|
|
max_file_size = 0
|
|
|
|
max_index = None
|
|
|
|
for index, photo in enumerate(photos):
|
|
|
|
if photo["file_size"] > max_file_size:
|
|
|
|
max_index = index
|
|
|
|
return max_index
|
|
|
|
|
|
|
|
|
|
|
|
def datei_speichern(verzeichnis, file_name, inhalt):
|
|
|
|
file = os.path.join(verzeichnis, file_name)
|
|
|
|
with open(file, "wb") as file_:
|
|
|
|
file_.write(inhalt)
|
|
|
|
|
|
|
|
|
|
|
|
def caption_analysieren(caption):
|
|
|
|
caption_split = caption.split(";")
|
|
|
|
ordner = caption_split[0].split(",")
|
|
|
|
ordner = [ordner_einzel.replace(" ", "") for ordner_einzel in ordner]
|
|
|
|
try:
|
|
|
|
notizen = caption_split[1]
|
|
|
|
if notizen.startswith(" "):
|
|
|
|
notizen = notizen.replace(" ", "", 1)
|
|
|
|
except IndexError:
|
|
|
|
notizen = []
|
|
|
|
return ordner, notizen
|
|
|
|
|
|
|
|
|
|
|
|
def get_dateiendung(file_path):
|
|
|
|
return file_path.split(".")[-1]
|
|
|
|
|
|
|
|
|
|
|
|
def nachricht_auswerten(bot, message):
|
|
|
|
absender = message["message"]["from"].get("id", 0)
|
|
|
|
if absender not in CONFIG["erlaubte_telegram_ids"]:
|
|
|
|
bot.send_message(absender, "User nicht freigegeben")
|
|
|
|
return
|
|
|
|
if bot.menue is None:
|
|
|
|
if "entities" in message["message"]:
|
|
|
|
if "bot_command" in message["message"]["entities"][0]["type"]:
|
|
|
|
text = message["message"]["text"]
|
|
|
|
kommando = text.replace("/", "")
|
|
|
|
bot.bot_kommando_exec(kommando)
|
|
|
|
|
|
|
|
if "photo" in message["message"]:
|
|
|
|
if "caption" in message["message"]:
|
|
|
|
ordner, notiz = caption_analysieren(message["message"]["caption"])
|
|
|
|
ordner_db_id = db.ordner_struktur_erstellen(ordner)
|
|
|
|
verzeichnis = verzeichnis_erstellen(ordner)
|
|
|
|
|
|
|
|
max_index = get_biggest_size(message["message"]["photo"])
|
|
|
|
file, file_path = bot.get_file(message["message"]["photo"][max_index]["file_id"])
|
|
|
|
dateiendung = get_dateiendung(file_path)
|
|
|
|
file_name = db.file_speichern(ordner_db_id, notiz, dateiendung)
|
|
|
|
datei_speichern(verzeichnis, file_name, file)
|
|
|
|
|
|
|
|
else:
|
|
|
|
bot.send_message(chat_id=absender,
|
|
|
|
text="Bild kann nicht verabeitet werden, Ordner wurde nicht angegeben",
|
|
|
|
reply_to_message_id=message["message"]["message_id"])
|
|
|
|
|
|
|
|
else:
|
|
|
|
bot.menue()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
bot = FotoBot(CONFIG["token"])
|
|
|
|
db.db_create_table()
|
|
|
|
db.update_root_verzeichnis(CONFIG["root_verzeichnis"])
|
|
|
|
|
|
|
|
while True:
|
|
|
|
nachricht_vorhanden = False
|
|
|
|
for message in bot.get_updates():
|
|
|
|
nachricht_vorhanden = True
|
|
|
|
nachricht_auswerten(bot, message)
|
|
|
|
|
|
|
|
if not nachricht_vorhanden and bot.menue is None:
|
|
|
|
break
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|