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