65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
import csv
|
|
import os
|
|
import tomllib
|
|
|
|
import flask_apscheduler
|
|
from flask import Flask
|
|
from whoosh.fields import *
|
|
from whoosh.index import create_in
|
|
from whoosh.qparser import QueryParser
|
|
|
|
from application.csvparser import CsvParser
|
|
|
|
SCRIPT_DIR = os.path.dirname(__file__)
|
|
DATA_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "data"))
|
|
|
|
|
|
def create_app():
|
|
settings = settings_from_file()
|
|
|
|
APP = Flask(__name__, static_folder="static")
|
|
APP.config["SECRET_KEY"] = "ty4425hk54a21eee5719b9s9df7sdfklx"
|
|
APP.config["UPLOAD_FOLDER"] = "tmpupload"
|
|
APP.config["LIBRARY_FILENAME"] = settings["libaryfilename"]
|
|
APP.config["IMAGE_FOLDER"] = "static/images"
|
|
APP.config["TITLE"] = settings["title"]
|
|
scheduler = flask_apscheduler.APScheduler()
|
|
scheduler.api_enabled = False
|
|
scheduler.init_app(APP)
|
|
scheduler.start()
|
|
index_books(APP.config["LIBRARY_FILENAME"], APP.config["IMAGE_FOLDER"])
|
|
|
|
@scheduler.task("interval", id="update", minutes=10)
|
|
def update():
|
|
index_books(APP.config["LIBRARY_FILENAME"], APP.config["IMAGE_FOLDER"])
|
|
|
|
return APP
|
|
|
|
|
|
def get_app():
|
|
APP = Flask(__name__, static_folder="static")
|
|
return APP
|
|
|
|
|
|
def index_books(filename: str, image_folder: str):
|
|
csvparser = CsvParser(filename, image_folder)
|
|
filename = os.path.join(DATA_DIR, csvparser.csv_file)
|
|
schema = Schema(
|
|
title=TEXT(stored=True), path=ID(stored=True), content=TEXT
|
|
)
|
|
ix = create_in(DATA_DIR, schema)
|
|
writer = ix.writer()
|
|
with open(filename, "r", encoding="utf_8_sig") as libcsv:
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
for row in csv_as_dict:
|
|
rowcontent = csvparser.concatenate_csv_row(row)
|
|
writer.add_document(title=row["Id"], path="/a", content=rowcontent)
|
|
writer.commit()
|
|
|
|
|
|
def settings_from_file():
|
|
settings = {}
|
|
with open("settings.toml", "rb") as settings_file:
|
|
settings = tomllib.load(settings_file)
|
|
return settings
|