From 0fb8929a80a62433ed7def093939115f6f424dd9 Mon Sep 17 00:00:00 2001 From: crunk Date: Sat, 18 May 2024 16:11:05 +0200 Subject: [PATCH] file_crawler update --- verse/file_crawler.py | 70 +++++++++++++++++++++++---- verse/models/distribusi_file_model.py | 2 +- verse/statuspengguna/loginuser.py | 1 - 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/verse/file_crawler.py b/verse/file_crawler.py index 5d1d0f9..4ff054b 100644 --- a/verse/file_crawler.py +++ b/verse/file_crawler.py @@ -2,22 +2,74 @@ import os import magic from distribusi.mappings import CODE_TYPES, FILE_TYPES, SUB_TYPES - +from models.distribusi_model import Distribusis from models.distribusi_file_model import DistribusiFiles +from app import create_app, get_app, db +from sqlalchemy.exc import ( + DatabaseError, + DataError, + IntegrityError, + InterfaceError, + InvalidRequestError, +) MIME_TYPE = magic.Magic(mime=True) -def distribusi_file_with_type(full_path): +def _distribusi_file_with_type(distribusi, full_path): mime = MIME_TYPE.from_file(full_path) type_, subtype = mime.split("/") if type_ in FILE_TYPES: - print(f"distribusi file:{full_path} type:{type_}") + _add_distribusi_file_to_db(distribusi, full_path, type_) + +def _get_distribusi_from_path(path): + distribusi = Distribusis.query.filter_by(distribusiname=path).first() + return distribusi + +def _add_distribusi_file_to_db(distribusi, full_path, type): + app = get_app() + print(f"adding file to database: {full_path} type: {type}") + try: + new_distribusi_file = DistribusiFiles( + path=full_path, + type=type, + distribusi=distribusi.id, + ) + db.session.add(new_distribusi_file) + db.session.commit() + return + except InvalidRequestError: + db.session.rollback() + app.logger.error("Something went wrong!") + except IntegrityError: + db.session.rollback() + app.logger.error("file %s already exists!", full_path) + except DataError: + db.session.rollback() + app.logger.error("%s Invalid Entry", full_path) + except InterfaceError: + db.session.rollback() + app.logger.error( + "Error connecting to the database" + ) + except DatabaseError: + db.session.rollback() + app.logger.error( + "Error connecting to the database" + ) + + +def add_distribusi_files(path): + app = create_app() + with app.app_context(): + distribusi = _get_distribusi_from_path(path) + path = os.path.join("stash", path) + for root, dirs, files in os.walk(path, topdown=True): + files = list(filter(lambda f: not f.startswith("."), files)) + files = list(filter(lambda f: not f.endswith(".html"), files)) + for file in files: + full_path = os.path.join(root, file) + _distribusi_file_with_type(distribusi, full_path) -for root, dirs, files in os.walk("stash", topdown=True): - files = list(filter(lambda f: not f.startswith("."), files)) - files = list(filter(lambda f: not f.endswith(".html"), files)) - for file in files: - full_path = os.path.join(root, file) - distribusi_file_with_type(full_path) +add_distribusi_files("2018-12-WttF-Mastodon-and-the-Fediverse") diff --git a/verse/models/distribusi_file_model.py b/verse/models/distribusi_file_model.py index 417b861..5e3f5ac 100644 --- a/verse/models/distribusi_file_model.py +++ b/verse/models/distribusi_file_model.py @@ -12,7 +12,7 @@ class DistribusiFiles(db.Model): path = db.Column(db.String(4096), nullable=True, unique=False) alttext = db.Column(db.String(255), nullable=True, unique=False) tags = db.Column(db.String(500), nullable=True, unique=False) - description = db.Column(db.String(9), nullable=True, unique=False) + description = db.Column(db.String(4096), nullable=True, unique=False) def __repr__(self): return "" % self.distribusiname diff --git a/verse/statuspengguna/loginuser.py b/verse/statuspengguna/loginuser.py index db08cac..5d72063 100644 --- a/verse/statuspengguna/loginuser.py +++ b/verse/statuspengguna/loginuser.py @@ -37,7 +37,6 @@ def LoginUser(): loginform.password.errors.append("Invalid email or password!") return render_template("login.html", loginform=loginform) if check_password_hash(user.password, loginform.password.data): - print(type(user)) login_user(user) flash("Logged in successfully.", "success") next = request.args.get("next")