From f969fec15e6d9feffa194da559012602d360594d Mon Sep 17 00:00:00 2001 From: Marcel Gansfusz Date: Sun, 9 Feb 2025 20:37:43 +0100 Subject: [PATCH] added py file to initialize database form sftp --- .gitignore | 1 + app/init.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++ app/main.py | 2 ++ database_init.sql | 2 +- run.txt | 1 + 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 app/init.py diff --git a/.gitignore b/.gitignore index 9d08b52..24ba8e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ app/venv/ app/files/ +app/pwfile.json diff --git a/app/init.py b/app/init.py new file mode 100644 index 0000000..73beb6f --- /dev/null +++ b/app/init.py @@ -0,0 +1,48 @@ +import paramiko +from stat import S_ISDIR, S_ISREG +import re +from base64 import decodebytes +import json +import mariadb +db = mariadb.connect( + host="localhost", user="wildserver", password="DBPassword", database="Unizeug" +) +c=db.cursor() +c.execute("DROP TABLE LVAs") +c.execute("CREATE TABLE LVAs(id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,lvid MEDIUMINT unsigned, lvname VARCHAR(256), lvpath VARCHAR(256),PRIMARY KEY(id))") +db.commit() +def get_dirstruct(): + with open("app/pwfile.json", "r") as f: + cred = json.load(f) + ssh = paramiko.SSHClient() + print(cred["sftpurl"]) + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + # key=paramiko.RSAKey(data=decodebytes(bytes(cred["key"],"utf-8"))) + # ssh.get_host_keys().add(cred["sftpurl"], 'ssh-rsa', key) + ssh.connect(cred["sftpurl"], username=cred["sftpuser"], password=cred["sftpPW"]) + sftp = ssh.open_sftp() + folders = sftp.listdir_attr("/mnt/save/daten/Unizeug/") + for entry in folders: + if entry is None: + continue + if S_ISDIR(entry.st_mode): + fname = str(entry.filename) + regex=re.compile(r"Multimedia_only") + if regex.search(fname): + continue + # print(fname) + lvid = re.search(r"\d{3}.\d{3}", fname) + # print(lvid) + if lvid is None: + continue + lvid=int(lvid.group()[:3]+lvid.group()[4:]) + name=fname[:-8] + print(name) + print(lvid) + cur=db.cursor() + cur.execute("INSERT INTO LVAs (lvid, lvname, lvpath) VALUES(?,?,?)",(lvid,name,fname)) + db.commit() + + +if __name__ == "__main__": + get_dirstruct() diff --git a/app/main.py b/app/main.py index 7f8ba0e..3b0d271 100644 --- a/app/main.py +++ b/app/main.py @@ -8,9 +8,11 @@ from fastapi.staticfiles import StaticFiles import pymupdf import json + # import os import mariadb + app = FastAPI() app.mount("/favicon", StaticFiles(directory="./favicon"), name="favicon") diff --git a/database_init.sql b/database_init.sql index 97e45de..0390b57 100644 --- a/database_init.sql +++ b/database_init.sql @@ -1,6 +1,6 @@ CREATE DATABASE Unizeug; USE Unizeug; -CREATE TABLE LVAs(id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,number MEDIUMINT unsigned, name VARCHAR(256),PRIMARY KEY(id)); +CREATE TABLE LVAs(id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,lvid MEDIUMINT unsigned, lvname VARCHAR(256), lvpath VARCHAR(256),PRIMARY KEY(id)); CREATE TABLE Porfs(id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,name VARCHAR(256),PRIMARY KEY(id)); CREATE TABLE LPLink(id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,LVAID bigint(20),ProfID bigint(20),PRIMARY KEY(id)); CREATE TABLE FIP(id UUID DEFAULT(UUID()), filename VARCHAR(256), PRIMARY KEY(id)); diff --git a/run.txt b/run.txt index 2632075..705b620 100644 --- a/run.txt +++ b/run.txt @@ -1,3 +1,4 @@ sudo systemctl start mariadb.service python -m uvicorn app.main:app --reload Requirements: For pymupdf tesseract needs to be installed for the language deu un the system. +TISS API maby not nessecarry: https://tiss.tuwien.ac.at/api/course/101685-2024W