diff --git a/default_config.py b/default_config.py index e5aa4ae..70c9c5e 100644 --- a/default_config.py +++ b/default_config.py @@ -6,4 +6,5 @@ SECRET_KEY="dev" # Port to run on -PORT="5000" \ No newline at end of file +PORT="5000" +MAX_ONLINE_QUERIES=3 \ No newline at end of file diff --git a/partitioncloud/modules/albums.py b/partitioncloud/modules/albums.py index e130c19..1aa9252 100644 --- a/partitioncloud/modules/albums.py +++ b/partitioncloud/modules/albums.py @@ -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", diff --git a/partitioncloud/modules/search.py b/partitioncloud/modules/search.py index de05d33..692a8a5 100644 --- a/partitioncloud/modules/search.py +++ b/partitioncloud/modules/search.py @@ -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 diff --git a/partitioncloud/static/style.css b/partitioncloud/static/style.css index af575aa..cd8e92b 100644 --- a/partitioncloud/static/style.css +++ b/partitioncloud/static/style.css @@ -205,7 +205,7 @@ input[type=submit] { width: stretch; } -#online-search-label { +#nb-queries-label { font-size: .7rem; font-weight: lighter; } diff --git a/partitioncloud/templates/albums/index.html b/partitioncloud/templates/albums/index.html index 12b605c..13338c3 100644 --- a/partitioncloud/templates/albums/index.html +++ b/partitioncloud/templates/albums/index.html @@ -14,8 +14,12 @@

- - + +
{% if albums|length != 0 %} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c040060 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +flask +sqlite3 +urllib +googlesearch \ No newline at end of file