Grundfunktionen fertig
This commit is contained in:
+28
-2
@@ -19,9 +19,35 @@ class Ordner(BaseModel):
|
|||||||
class Foto(BaseModel):
|
class Foto(BaseModel):
|
||||||
id = peewee.AutoField()
|
id = peewee.AutoField()
|
||||||
ordner = peewee.IntegerField()
|
ordner = peewee.IntegerField()
|
||||||
name = peewee.CharField()
|
name = peewee.CharField(null=True)
|
||||||
notiz = peewee.CharField() # 255?
|
notiz = peewee.CharField(null=True) # 255?
|
||||||
|
|
||||||
|
|
||||||
def db_create_table():
|
def db_create_table():
|
||||||
DB_PROXY.create_tables([Ordner, Foto])
|
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
|
||||||
|
|
||||||
|
|||||||
+54
-27
@@ -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):
|
def kommando_unbekannt(self, message):
|
||||||
print("Hello World")
|
|
||||||
|
|
||||||
def kommando_unbekannt(self):
|
|
||||||
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
|
||||||
@@ -81,20 +98,30 @@ def nachricht_auswerten(bot, message):
|
|||||||
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
|
||||||
|
|||||||
@@ -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