Ajout de la recherche google

This commit is contained in:
augustin64 2022-08-17 09:44:31 +02:00
parent ca72bbb89b
commit 121fa288d2
6 changed files with 131 additions and 7 deletions

View File

@ -11,7 +11,7 @@ from flask import (Blueprint, abort, flash, redirect, render_template, request,
from .auth import login_required from .auth import login_required
from .db import get_db from .db import get_db
from .utils import User, Album, get_all_partitions from .utils import User, Album, get_all_partitions
from .search import search as srch from . import search
bp = Blueprint("albums", __name__, url_prefix="/albums") bp = Blueprint("albums", __name__, url_prefix="/albums")
@ -27,14 +27,16 @@ def index():
@bp.route("/search", methods=["POST"]) @bp.route("/search", methods=["POST"])
@login_required @login_required
def search(): def search_page():
if "query" not in request.form or request.form["query"] == "": if "query" not in request.form or request.form["query"] == "":
flash("Aucun terme de recherche spécifié.") flash("Aucun terme de recherche spécifié.")
return redirect("/albums") return redirect("/albums")
query = request.form["query"] query = request.form["query"]
partitions = srch(query, get_all_partitions()) search.flush_cache()
return render_template("albums/search.html", partitions=partitions, query=query) partitions_local = search.local_search(query, get_all_partitions())
google_results = search.online_search(query)
return render_template("albums/search.html", partitions=partitions_local, google_results=google_results, query=query)
@bp.route("/<uuid>") @bp.route("/<uuid>")
def album(uuid): def album(uuid):

View File

@ -29,6 +29,23 @@ def partition(uuid):
return send_file(os.path.join("partitions", f"{uuid}.pdf")) return send_file(os.path.join("partitions", f"{uuid}.pdf"))
@bp.route("/search/<uuid>")
@login_required
def partition_search(uuid):
db = get_db()
partition = db.execute(
"""
SELECT * FROM search_results
WHERE uuid = ?
""",
(uuid,)
).fetchone()
if partition is None:
abort(404)
return send_file(os.path.join("search-partitions", f"{uuid}.pdf"))
@bp.route("/") @bp.route("/")
@admin_required @admin_required
def index(): def index():

View File

@ -3,6 +3,7 @@ DROP TABLE IF EXISTS partition;
DROP TABLE IF EXISTS album; DROP TABLE IF EXISTS album;
DROP TABLE IF EXISTS contient_partition; DROP TABLE IF EXISTS contient_partition;
DROP TABLE IF EXISTS contient_user; DROP TABLE IF EXISTS contient_user;
DROP TABLE IF EXISTS search_results;
CREATE TABLE user ( CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -35,3 +36,8 @@ CREATE TABLE contient_user (
album_id INTEGER NOT NULL, album_id INTEGER NOT NULL,
PRIMARY KEY (user_id, album_id) PRIMARY KEY (user_id, album_id)
); );
CREATE TABLE search_results (
uuid TEXT(36) PRIMARY KEY,
creation_time TEXT NULL DEFAULT (datetime('now', 'localtime'))
);

View File

@ -2,8 +2,16 @@
""" """
Module implémentant la recherche de partitions par mots-clés Module implémentant la recherche de partitions par mots-clés
""" """
from uuid import uuid4
import urllib.request
import os
def search(query, partitions): import googlesearch
from .db import get_db
def local_search(query, partitions):
""" """
Renvoie les 5 résultats les plus pertinents parmi une liste donnée Renvoie les 5 résultats les plus pertinents parmi une liste donnée
""" """
@ -30,3 +38,75 @@ def search(query, partitions):
else: else:
break break
return sorted_partitions[:min(5,len(sorted_partitions))] return sorted_partitions[:min(5,len(sorted_partitions))]
def online_search(query):
"""
Renvoie les 5 résultats les plus pertinents depuis google
"""
db = get_db()
query = f"partition filetype:pdf {query}"
partitions = []
results = googlesearch.search(
query,
num=5,
stop=5,
pause=1
)
for element in results:
while True:
try:
uuid = str(uuid4())
db.execute(
"""
INSERT INTO search_results (uuid)
VALUES (?)
""",
(uuid,)
)
urllib.request.urlretrieve(element, f"partitioncloud/search-partitions/{uuid}.pdf")
os.system(
f'/usr/bin/convert -thumbnail\
"178^>" -background white -alpha \
remove -crop 178x178+0+0 \
partitioncloud/search-partitions/{uuid}.pdf[0] \
partitioncloud/static/search-thumbnails/{uuid}.jpg'
)
partitions.append(
{
"name": element.split("://")[1].split("/")[0],
"uuid": uuid
}
)
db.commit()
break
except db.IntegrityError:
pass
except urllib.error.HTTPError as e:
print(e)
return partitions
def flush_cache():
"""
Supprimer les résultats de recherche datant de plus de 15 minutes
"""
db = get_db()
expired_cache = db.execute(
"""
SELECT uuid FROM search_results
WHERE creation_time <= datetime('now', '-15 minutes', 'localtime')
"""
).fetchall()
for element in expired_cache:
uuid = element["uuid"]
os.remove(f"partitioncloud/search-partitions/{uuid}.pdf")
os.remove(f"partitioncloud/static/search-thumbnails/{uuid}.jpg")
db.execute(
"""
DELETE FROM search_results
WHERE creation_time <= datetime('now', '-15 minutes', 'localtime')
"""
)

View File

@ -174,6 +174,11 @@ input[type=submit] {
overflow-x: scroll; overflow-x: scroll;
} }
#search-partitions-grid {
display: flex;
overflow-x: scroll;
}
#header-actions { #header-actions {
margin-bottom: 5px; margin-bottom: 5px;
} }

View File

@ -6,6 +6,7 @@
{% block content %} {% block content %}
{% if partitions|length != 0 %} {% if partitions|length != 0 %}
<h4>Résultats dans la bibliothèque locale</h4>
<div id="partitions-grid"> <div id="partitions-grid">
{% for partition in partitions %} {% for partition in partitions %}
<a href="/partition/{{ partition['uuid'] }}"> <a href="/partition/{{ partition['uuid'] }}">
@ -19,7 +20,20 @@
</a> </a>
{% endfor %} {% endfor %}
</div> </div>
{% else %} {% endif %}
<div>Aucune partition ne correspond aux termes de votre requête</div> {% if google_results|length != 0 %}
<h4>Résultats de la recherche en ligne</h4>
<div id="partitions-grid">
{% for partition in google_results %}
<a href="/partition/search/{{ partition['uuid'] }}">
<div class="partition" id="partition-{{ partition['uuid'] }}">
<img class="partition-thumbnail" src="/static/search-thumbnails/{{ partition['uuid'] }}.jpg">
<div class="partition-description">
<div class="partition-name">{{ partition["name"] }}</div>
</div>
</div>
</a>
{% endfor %}
</div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}