|
|
|
@ -1,8 +1,12 @@
|
|
|
|
|
from telegram_api import telegram_bot_api as api
|
|
|
|
|
import os
|
|
|
|
|
import toml
|
|
|
|
|
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")
|
|
|
|
@ -13,40 +17,42 @@ def load_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, root_verzeichnis):
|
|
|
|
|
def __init__(self, token):
|
|
|
|
|
super().__init__(token)
|
|
|
|
|
self.root = root_verzeichnis
|
|
|
|
|
self.aktuelles_verzeichnis = self.root
|
|
|
|
|
|
|
|
|
|
self.menue = None
|
|
|
|
|
self.umenue = None
|
|
|
|
|
|
|
|
|
|
self.bot_kommando = {
|
|
|
|
|
"alarme": self.alarm
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def message_handler(self, message):
|
|
|
|
|
if self.menue is not None:
|
|
|
|
|
self.menue()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def verzeichnis_wechseln(self, verzeichnisse):
|
|
|
|
|
self.aktuelles_verzeichnis = os.path.join(self.root, *verzeichnisse)
|
|
|
|
|
self.menue(message)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def bot_kommando_exec(self, kommando):
|
|
|
|
|
self.menue = self.bot_kommando.get(kommando, self.kommando_unbekannt)
|
|
|
|
|
self.menue()
|
|
|
|
|
|
|
|
|
|
def alarm(self):
|
|
|
|
|
print("Hello World")
|
|
|
|
|
|
|
|
|
|
def kommando_unbekannt(self):
|
|
|
|
|
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
|
|
|
|
@ -56,20 +62,31 @@ def get_biggest_size(photos):
|
|
|
|
|
return max_index
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def datei_speichern(bot, file_name, inhalt):
|
|
|
|
|
file = os.path.join(bot.aktuelles_verzeichnis, file_name)
|
|
|
|
|
def datei_speichern(verzeichnis, file_name, inhalt):
|
|
|
|
|
file = os.path.join(verzeichnis, file_name)
|
|
|
|
|
with open(file, "wb") as file_:
|
|
|
|
|
file_.write(inhalt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_file_name(file_path):
|
|
|
|
|
filename = file_path.split("/")[1]
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
print(message)
|
|
|
|
|
absender = message["message"]["from"].get("id", 0)
|
|
|
|
|
print(absender)
|
|
|
|
|
if absender not in CONFIG["erlaubte_telegram_ids"]:
|
|
|
|
|
bot.send_message(absender, "User nicht freigegeben")
|
|
|
|
|
return
|
|
|
|
@ -79,22 +96,32 @@ def nachricht_auswerten(bot, message):
|
|
|
|
|
text = message["message"]["text"]
|
|
|
|
|
kommando = text.replace("/", "")
|
|
|
|
|
bot.bot_kommando_exec(kommando)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if "photo" in message["message"]:
|
|
|
|
|
max_index = get_biggest_size(message["message"]["photo"])
|
|
|
|
|
file, file_path = bot.get_file(message["message"]["photo"][max_index]["file_id"])
|
|
|
|
|
#file_name = get_file_name()
|
|
|
|
|
#datei_speichern(bot, file_name, file)
|
|
|
|
|
|
|
|
|
|
if "document" in message["message"]:
|
|
|
|
|
file, file_path = bot.get_file(message["message"]["document"]["file_id"])
|
|
|
|
|
|
|
|
|
|
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"], CONFIG["root_verzeichnis"])
|
|
|
|
|
bot = FotoBot(CONFIG["token"])
|
|
|
|
|
db.db_create_table()
|
|
|
|
|
db.update_root_verzeichnis(CONFIG["root_verzeichnis"])
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
nachricht_vorhanden = False
|
|
|
|
|