index books onn applications startup and once every 10 minutes

This commit is contained in:
crunk 2023-07-14 12:54:43 +02:00
parent d306b61b2d
commit ff7189af66
3 changed files with 80 additions and 27 deletions

45
library/app.py Normal file
View File

@ -0,0 +1,45 @@
import csv
import os
import flask_apscheduler
from flask import Flask
from whoosh.fields import *
from whoosh.index import create_in
from whoosh.qparser import QueryParser
from csvparser.csvparser import concatenate_csv_row, getfullpublication
SCRIPT_DIR = os.path.dirname(__file__)
DATA_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "data"))
def create_app():
APP = Flask(__name__, static_folder="static")
APP.config["SECRET_KEY"] = "ty4425hk54a21eee5719b9s9df7sdfklx"
APP.config["UPLOAD_FOLDER"] = "tmpupload"
scheduler = flask_apscheduler.APScheduler()
scheduler.api_enabled = False
scheduler.init_app(APP)
scheduler.start()
index_books()
@scheduler.task("interval", id="update", minutes=10)
def update():
index_books()
return APP
def index_books():
filename = os.path.join(DATA_DIR, "varlib.csv")
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 = concatenate_csv_row(row)
writer.add_document(title=row["Id"], path="/a", content=rowcontent)
writer.commit()

View File

@ -1,9 +1,9 @@
"""This parses the varlib.csv but only in a way """This parses the varlib.csv but only in a way
that is actually useful for the site""" that is actually useful for the site"""
from tempfile import NamedTemporaryFile
import shutil
import csv import csv
import os import os
import shutil
from tempfile import NamedTemporaryFile
script_dir = os.path.dirname(__file__) script_dir = os.path.dirname(__file__)
data_dir = os.path.abspath(os.path.join(script_dir, "../data")) data_dir = os.path.abspath(os.path.join(script_dir, "../data"))
@ -222,3 +222,15 @@ def editborrowedby(pubid, borrower):
csv_as_writer.writerow(row) csv_as_writer.writerow(row)
shutil.move(tempfile.name, filename) shutil.move(tempfile.name, filename)
def concatenate_csv_row(row):
rowcontent = []
rowcontent.append(row["Publication"])
rowcontent.append(row["Author"])
rowcontent.append(row["Fields"])
rowcontent.append(row["Type"])
rowcontent.append(row["Publishers"])
rowcontent.append(row["Highlights"])
rowcontent.append(row["Comments"])
return " ".join(rowcontent)

View File

@ -1,44 +1,35 @@
"""This is the main flask library page""" """This is the main flask library page"""
import os
import flask
from requests import get
from icalendar import Calendar
import datetime import datetime
import os
import bcrypt import bcrypt
from flask import ( import flask
render_template, from flask import redirect, render_template, request
redirect,
request,
)
from flask_wtf.csrf import CSRFProtect from flask_wtf.csrf import CSRFProtect
from werkzeug.utils import secure_filename from icalendar import Calendar
from PIL import Image from PIL import Image
from requests import get
from werkzeug.utils import secure_filename
from app import create_app
from borrowform import BorrowForm
from csvparser.csvparser import (editborrowedby, getfullpublication,
getlicenses, getpublications, gettypes,
getyears, writepublication)
from rnrfeed.rnrfeeder import getevents, getlatestevent from rnrfeed.rnrfeeder import getevents, getlatestevent
from uploadform import PublicationForm from uploadform import PublicationForm
from borrowform import BorrowForm
from csvparser.csvparser import (
getlicenses,
getpublications,
gettypes,
getyears,
getfullpublication,
writepublication,
editborrowedby,
)
APP = create_app()
csrf = CSRFProtect() csrf = CSRFProtect()
APP = flask.Flask(__name__, static_folder="static")
APP.config["SECRET_KEY"] = "ty4425hk54a21eee5719b9s9df7sdfklx"
APP.config["UPLOAD_FOLDER"] = "tmpupload"
csrf.init_app(APP) csrf.init_app(APP)
@APP.route("/") @APP.route("/")
def index(): def index():
"""Main route, shows all the books and you can filter them, a bit""" """Main route, shows all the books and you can filter them
based on year, license, type"""
pubtypes = gettypes() pubtypes = gettypes()
pubyears = getyears() pubyears = getyears()
publicenses = getlicenses() publicenses = getlicenses()
@ -96,6 +87,11 @@ def show_book(publicationID):
) )
@APP.route("/search", methods=["GET"])
def searchbooks():
return
@APP.route("/pastevents") @APP.route("/pastevents")
def pastevents(): def pastevents():
"""show past R&R events and book recommendations""" """show past R&R events and book recommendations"""