From 711e8282ef562caee3ad6185107683a7225b0df7 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 8 Jul 2018 10:42:14 -0400 Subject: [PATCH] 'Go to random website' button, and navigation in the website list --- app.py | 22 +++++++++++++++++++++- database.py | 13 +++++++++++-- search/search.py | 2 -- templates/websites.html | 29 ++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index fae2c27..1bf1b4e 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ from flask import Flask, render_template, redirect, request, flash, abort, Response, send_from_directory, session import json +from urllib.parse import urlparse import os import time import itertools @@ -105,7 +106,26 @@ def website_links(website_id): @app.route("/website/") def websites(): page = int(request.args.get("p")) if "p" in request.args else 0 - return render_template("websites.html", websites=db.get_websites(100, page)) + url = request.args.get("url") if "url" in request.args else "" + if url: + parsed_url = urlparse(url) + if parsed_url.scheme: + search_term = (parsed_url.scheme + "://" + parsed_url.netloc) + else: + flash("Sorry, I was not able to parse this url format. " + "Make sure you include the appropriate scheme (http/https/ftp)", "warning") + search_term = "" + else: + search_term = url + + return render_template("websites.html", + websites=db.get_websites(10, page, search_term), + p=page, url=search_term, per_page=10) + + +@app.route("/website/random") +def random_website(): + return redirect("/website/" + str(db.get_random_website_id())) @app.route("/website/redispatch_queued") diff --git a/database.py b/database.py index d594aa3..b53e158 100644 --- a/database.py +++ b/database.py @@ -104,16 +104,25 @@ class Database: else: return None - def get_websites(self, per_page, page: int): + def get_websites(self, per_page, page: int, url): """Get all websites""" with sqlite3.connect(self.db_path) as conn: cursor = conn.cursor() cursor.execute("SELECT Website.id, Website.url, Website.last_modified FROM Website " - "ORDER BY last_modified DESC LIMIT ? OFFSET ?", (per_page, page * per_page)) + "WHERE Website.url LIKE ?" + "ORDER BY last_modified DESC LIMIT ? OFFSET ?", (url + "%", per_page, page * per_page)) return cursor.fetchall() + def get_random_website_id(self): + + with sqlite3.connect(self.db_path) as conn: + cursor = conn.cursor() + cursor.execute("SELECT id FROM Website WHERE id >= (abs(random()) % (SELECT max(id) FROM Website)) LIMIT 1;") + + return cursor.fetchone()[0] + def website_exists(self, url): """Check if an url or the parent directory of an url already exists""" with sqlite3.connect(self.db_path) as conn: diff --git a/search/search.py b/search/search.py index 00fa03f..ba3da20 100644 --- a/search/search.py +++ b/search/search.py @@ -405,7 +405,6 @@ class ElasticSearchEngine(SearchEngine): return stats - def stream_all_docs(self): return helpers.scan(query={ "query": { @@ -413,7 +412,6 @@ class ElasticSearchEngine(SearchEngine): } }, scroll="5m", client=self.es, index=self.index_name) - def are_empty(self, websites): result = self.es.search(body={ "query": { diff --git a/templates/websites.html b/templates/websites.html index 7adbd8b..4b52ffd 100644 --- a/templates/websites.html +++ b/templates/websites.html @@ -6,7 +6,28 @@ {% block body %}
-
Last updated websites
+
Go to website
+
+ + Go to random website +
+

Website search

+
+
+
+ +
+
+ +
+
+
+
+
+
+
+
+
Websites
@@ -23,6 +44,12 @@ {% endfor %}
+ {% if websites|length == per_page %} + Next + {% endif %} + {% if p > 0 %} + Previous + {% endif %}