Grundfunktionen fertig
This commit is contained in:
+28
-2
@@ -19,9 +19,35 @@ class Ordner(BaseModel):
|
||||
class Foto(BaseModel):
|
||||
id = peewee.AutoField()
|
||||
ordner = peewee.IntegerField()
|
||||
name = peewee.CharField()
|
||||
notiz = peewee.CharField() # 255?
|
||||
name = peewee.CharField(null=True)
|
||||
notiz = peewee.CharField(null=True) # 255?
|
||||
|
||||
|
||||
def db_create_table():
|
||||
DB_PROXY.create_tables([Ordner, Foto])
|
||||
|
||||
|
||||
def update_root_verzeichnis(root_verzeichnis):
|
||||
Ordner.update(name=root_verzeichnis, parent=0).where(Ordner.id == 1).execute()
|
||||
|
||||
|
||||
def ordner_struktur_erstellen(ordner_liste, parent=1):
|
||||
for ordner_einzel in ordner_liste:
|
||||
ordner_existiert = False
|
||||
for ordner in Ordner.select().where(Ordner.parent == parent and Ordner.name == ordner_einzel):
|
||||
ordner_existiert = True
|
||||
parent = ordner.id
|
||||
if not ordner_existiert:
|
||||
neuer_ordner = Ordner.create(parent=parent, name=ordner_einzel)
|
||||
parent = neuer_ordner.id
|
||||
return parent
|
||||
|
||||
|
||||
def file_speichern(ordner_id, notiz, dateiendung):
|
||||
ordner_name = Ordner.select(Ordner.name).where(Ordner.id == ordner_id).scalar()
|
||||
file = Foto.create(ordner=ordner_id, notiz=notiz)
|
||||
file_name = "{}_{}.{}".format(ordner_name, file.id, dateiendung)
|
||||
file.name = file_name
|
||||
file.save()
|
||||
return file_name
|
||||
|
||||
|
||||
+59
-32
@@ -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
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# vorlage_config_fotobot.toml umbenennen zu config_fotobot.toml
|
||||
|
||||
token = "<Bottoken>"
|
||||
root_verzeichnis = "<gewünschtes root Verzeichnis für die Bildablage>"
|
||||
erlaubte_telegram_ids = [<liste mit freigegebenen Telegramids>]
|
||||
Reference in New Issue
Block a user