Add MAX_ONLINE_QUERIES parameter

This commit is contained in:
augustin64 2022-10-25 18:45:08 +02:00
parent 7b0566fbff
commit b6a965dd87
6 changed files with 35 additions and 14 deletions

View File

@ -6,4 +6,5 @@
SECRET_KEY="dev"
# Port to run on
PORT="5000"
PORT="5000"
MAX_ONLINE_QUERIES=3

View File

@ -7,7 +7,7 @@ import shutil
from uuid import uuid4
from flask import (Blueprint, abort, flash, redirect, render_template, request,
send_file, session)
send_file, session, current_app)
from .auth import login_required
from .db import get_db
@ -23,7 +23,12 @@ def index():
user = User(session.get("user_id"))
albums = user.get_albums()
return render_template("albums/index.html", albums=albums)
if user.access_level == 1:
max_queries = 10
else:
max_queries = current_app.config["MAX_ONLINE_QUERIES"]
return render_template("albums/index.html", albums=albums, MAX_QUERIES=max_queries)
@bp.route("/search", methods=["POST"])
@ -34,13 +39,20 @@ def search_page():
return redirect("/albums")
query = request.form["query"]
nb_queries = abs(int(request.form["nb-queries"]))
search.flush_cache()
partitions_local = search.local_search(query, get_all_partitions())
if "online-search" in request.form:
google_results = search.online_search(query)
user = User(session.get("user_id"))
if nb_queries > 0:
if user.access_level != 1:
nb_queries = min(current_app.config["MAX_ONLINE_QUERIES"], nb_queries)
else:
nb_queries = min(10, nb_queries) # Query limit is 10 for an admin
google_results = search.online_search(query, nb_queries)
else:
google_results = []
user = User(session.get("user_id"))
return render_template(
"albums/search.html",

View File

@ -40,7 +40,7 @@ def local_search(query, partitions):
return sorted_partitions[:min(5,len(sorted_partitions))]
def online_search(query):
def online_search(query, num_queries):
"""
Renvoie les 3 résultats les plus pertinents depuis google
"""
@ -49,8 +49,8 @@ def online_search(query):
partitions = []
results = googlesearch.search(
query,
num=3,
stop=3,
num=num_queries,
stop=num_queries,
pause=0.2
)
for element in results:
@ -83,8 +83,8 @@ def online_search(query):
break
except db.IntegrityError:
pass
except urllib.error.HTTPError as e:
print(e)
except (urllib.error.HTTPError, urllib.error.URLError) as e:
print(e, element)
db.execute(
"""
DELETE FROM search_results

View File

@ -205,7 +205,7 @@ input[type=submit] {
width: stretch;
}
#online-search-label {
#nb-queries-label {
font-size: .7rem;
font-weight: lighter;
}

View File

@ -14,8 +14,12 @@
<form action="/albums/search" method="post" id="search-form">
<input id="search-bar" type="search" name="query" placeholder="Rechercher" required>
<br/>
<input type="checkbox" id="online-search" name="online-search" value="online-search" checked="on">
<label for="online-search" id="online-search-label">Recherche en ligne</label>
<select id="nb-queries" name="nb-queries">
{% for i in range(0, MAX_QUERIES+1) %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>
<label for="nb-queries" id="nb-queries-label">Recherches en ligne</label>
</form>
<section id="albums">
{% if albums|length != 0 %}

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
flask
sqlite3
urllib
googlesearch