mirror of
				https://github.com/simon987/od-database.git
				synced 2025-11-04 06:56:52 +00:00 
			
		
		
		
	'Go to random website' button, and navigation in the website list
This commit is contained in:
		
							parent
							
								
									9ff21e7943
								
							
						
					
					
						commit
						711e8282ef
					
				
							
								
								
									
										22
									
								
								app.py
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								app.py
									
									
									
									
									
								
							@ -1,5 +1,6 @@
 | 
				
			|||||||
from flask import Flask, render_template, redirect, request, flash, abort, Response, send_from_directory, session
 | 
					from flask import Flask, render_template, redirect, request, flash, abort, Response, send_from_directory, session
 | 
				
			||||||
import json
 | 
					import json
 | 
				
			||||||
 | 
					from urllib.parse import urlparse
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
import itertools
 | 
					import itertools
 | 
				
			||||||
@ -105,7 +106,26 @@ def website_links(website_id):
 | 
				
			|||||||
@app.route("/website/")
 | 
					@app.route("/website/")
 | 
				
			||||||
def websites():
 | 
					def websites():
 | 
				
			||||||
    page = int(request.args.get("p")) if "p" in request.args else 0
 | 
					    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")
 | 
					@app.route("/website/redispatch_queued")
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										13
									
								
								database.py
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								database.py
									
									
									
									
									
								
							@ -104,16 +104,25 @@ class Database:
 | 
				
			|||||||
            else:
 | 
					            else:
 | 
				
			||||||
                return None
 | 
					                return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_websites(self, per_page, page: int):
 | 
					    def get_websites(self, per_page, page: int, url):
 | 
				
			||||||
        """Get all websites"""
 | 
					        """Get all websites"""
 | 
				
			||||||
        with sqlite3.connect(self.db_path) as conn:
 | 
					        with sqlite3.connect(self.db_path) as conn:
 | 
				
			||||||
            cursor = conn.cursor()
 | 
					            cursor = conn.cursor()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            cursor.execute("SELECT Website.id, Website.url, Website.last_modified FROM Website "
 | 
					            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()
 | 
					            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):
 | 
					    def website_exists(self, url):
 | 
				
			||||||
        """Check if an url or the parent directory of an url already exists"""
 | 
					        """Check if an url or the parent directory of an url already exists"""
 | 
				
			||||||
        with sqlite3.connect(self.db_path) as conn:
 | 
					        with sqlite3.connect(self.db_path) as conn:
 | 
				
			||||||
 | 
				
			|||||||
@ -405,7 +405,6 @@ class ElasticSearchEngine(SearchEngine):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return stats
 | 
					        return stats
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def stream_all_docs(self):
 | 
					    def stream_all_docs(self):
 | 
				
			||||||
        return helpers.scan(query={
 | 
					        return helpers.scan(query={
 | 
				
			||||||
            "query": {
 | 
					            "query": {
 | 
				
			||||||
@ -413,7 +412,6 @@ class ElasticSearchEngine(SearchEngine):
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }, scroll="5m", client=self.es, index=self.index_name)
 | 
					        }, scroll="5m", client=self.es, index=self.index_name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def are_empty(self, websites):
 | 
					    def are_empty(self, websites):
 | 
				
			||||||
        result = self.es.search(body={
 | 
					        result = self.es.search(body={
 | 
				
			||||||
            "query": {
 | 
					            "query": {
 | 
				
			||||||
 | 
				
			|||||||
@ -6,7 +6,28 @@
 | 
				
			|||||||
{% block body %}
 | 
					{% block body %}
 | 
				
			||||||
    <div class="container">
 | 
					    <div class="container">
 | 
				
			||||||
        <div class="card">
 | 
					        <div class="card">
 | 
				
			||||||
            <div class="card-header">Last updated websites</div>
 | 
					            <div class="card-header">Go to website</div>
 | 
				
			||||||
 | 
					            <div class="card-body">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                <a class="btn btn-secondary" href="/website/random">Go to random website</a>
 | 
				
			||||||
 | 
					                <hr>
 | 
				
			||||||
 | 
					                <h3>Website search</h3>
 | 
				
			||||||
 | 
					                <form>
 | 
				
			||||||
 | 
					                    <div class="form-row">
 | 
				
			||||||
 | 
					                        <div class="col-md-11">
 | 
				
			||||||
 | 
					                            <input class="form-control" placeholder="URL" name="url" value="{{ url }}">
 | 
				
			||||||
 | 
					                        </div>
 | 
				
			||||||
 | 
					                        <div class="col-md-1">
 | 
				
			||||||
 | 
					                            <input class="btn btn-primary" type="submit" value="Search">
 | 
				
			||||||
 | 
					                        </div>
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                </form>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					    <div class="container">
 | 
				
			||||||
 | 
					        <div class="card">
 | 
				
			||||||
 | 
					            <div class="card-header">Websites</div>
 | 
				
			||||||
            <div class="card-body">
 | 
					            <div class="card-body">
 | 
				
			||||||
                <table class="table table-striped">
 | 
					                <table class="table table-striped">
 | 
				
			||||||
                    <thead>
 | 
					                    <thead>
 | 
				
			||||||
@ -23,6 +44,12 @@
 | 
				
			|||||||
                        </tr>
 | 
					                        </tr>
 | 
				
			||||||
                    {% endfor %}
 | 
					                    {% endfor %}
 | 
				
			||||||
                </table>
 | 
					                </table>
 | 
				
			||||||
 | 
					                {% if websites|length == per_page %}
 | 
				
			||||||
 | 
					                    <a href="/website?url={{ url }}&p={{ p + 1 }}" class="btn btn-primary" style="float: right">Next</a>
 | 
				
			||||||
 | 
					                {% endif %}
 | 
				
			||||||
 | 
					                {% if p > 0 %}
 | 
				
			||||||
 | 
					                    <a href="/website?url={{ url }}&p={{ p - 1 }}" class="btn btn-primary">Previous</a>
 | 
				
			||||||
 | 
					                {% endif %}
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user