diff --git a/app/app.py b/app/app.py index 8184857..020bcca 100755 --- a/app/app.py +++ b/app/app.py @@ -3,6 +3,7 @@ from urllib.parse import urlparse import config import json +import re import sqlite3 import subprocess @@ -21,7 +22,14 @@ def hide_post(permalink): permalink = "/" + permalink connection = sqlite3.connect(config.db_file) cursor = connection.cursor() - update = "UPDATE post SET hidden = ? WHERE permalink = ?" + update = """ + UPDATE + post + SET + hidden = ? + WHERE + permalink = ? + """ binds = [True, permalink] print(update) print(binds) @@ -30,33 +38,131 @@ def hide_post(permalink): connection.close() return "" -@app.route('/', methods=['GET', 'POST']) +@app.route('/') def index(): + return front_page() + +@app.route('/r/all') +def front_page(): + title = "/r/all" connection = sqlite3.connect(config.db_file) cursor = connection.cursor() - select = "SELECT subreddit, count(*) count FROM post WHERE hidden = ? GROUP BY subreddit ORDER BY count desc LIMIT 1" - binds = [False] - row = cursor.execute(select, binds).fetchone() + subreddits = get_subreddits(cursor) + select = """ + SELECT + post + FROM + post + WHERE + hidden = ? + ORDER BY + score desc + LIMIT ? + """ + binds = [False, config.posts_per_page_load] + posts = get_posts_from_select(cursor, select, binds) + add_subreddits_to_posts(posts) connection.close() - return get_subreddit(row[0]) + return render_template('index.html', title=title, posts=posts, subreddits=subreddits) + +@app.route('/r/other') +def other_page(): + title = "/r/other" + connection = sqlite3.connect(config.db_file) + cursor = connection.cursor() + subreddits = get_subreddits(cursor) + select = """ + SELECT + post + FROM + post + WHERE + hidden = ? AND + subreddit IN + ( + SELECT + subreddit + FROM + ( + SELECT + subreddit, + count(*) count + FROM + post + WHERE + hidden = ? + GROUP BY + subreddit + ) t + WHERE + count <= ? + ) + ORDER BY + score desc + LIMIT ? + """ + binds = [False, False, config.other_posts_cutoff, config.posts_per_page_load] + posts = get_posts_from_select(cursor, select, binds) + add_subreddits_to_posts(posts) + connection.close() + return render_template('index.html', title=title, posts=posts, subreddits=subreddits) @app.route('/r/') def get_subreddit(subreddit): title = f"/r/{subreddit}" connection = sqlite3.connect(config.db_file) cursor = connection.cursor() - select = "SELECT subreddit, count FROM (SELECT subreddit, count(*) count FROM post WHERE hidden = ? GROUP BY subreddit ORDER BY count desc) t WHERE count > 0" - binds = [False] + subreddits = get_subreddits(cursor) + select = """ + SELECT + post + FROM + post + WHERE + subreddit = ? AND + hidden = ? + ORDER BY + score desc + LIMIT ? + """ + binds = [subreddit, False, config.posts_per_page_load] + posts = get_posts_from_select(cursor, select, binds) + connection.close() + return render_template('index.html', title=title, posts=posts, subreddits=subreddits) + +def get_subreddits(cursor): + select = """ + SELECT + subreddit + FROM + ( + SELECT + subreddit, + count(*) count + FROM + post + WHERE + hidden = ? + GROUP BY + subreddit + ORDER BY + count desc + ) t + WHERE + count > ? + """ + binds = [False, config.other_posts_cutoff] results = cursor.execute(select, binds).fetchall() subreddits = [f"/r/{sub[0]}" for sub in results] - count = results[subreddits.index(title)][1] - select = "SELECT post FROM post WHERE subreddit = ? and hidden = ? ORDER BY score desc LIMIT ?" - binds = [subreddit, False, config.posts_per_page_load] + subreddits.insert(0, "/r/all") + subreddits.append("/r/other") + return subreddits + +def get_posts_from_select(cursor, select, binds): results = cursor.execute(select, binds).fetchall() posts = [json.loads(post[0]) for post in results] add_media_html_to_posts(posts) - connection.close() - return render_template('index.html', title=title, count=count, posts=posts, subreddits=subreddits) + return posts def add_media_html_to_posts(posts): for post_index, post in enumerate(posts): @@ -68,6 +174,11 @@ def add_media_html_to_posts(posts): html = get_media_html(filename, True if (post_index < 3 and media_index == 0) else False) media_html.append(html) post["media_html"] = media_html + +def add_subreddits_to_posts(posts): + for post in posts: + m = re.search(r"\/r\/([a-zA-Z0-9_]+)\/.*", post["permalink"]) + post["subreddit"] = f"/r/{m.group(1)}" def get_media_html(file, priority=False): @@ -83,5 +194,4 @@ def get_media_html(file, priority=False): if __name__ == '__main__': subprocess.run(["python3", "make_db.py"]) - #subprocess.run(["python3", "scrape_posts.py"]) app.run(host='0.0.0.0', port=8000) diff --git a/app/config.py b/app/config.py index 8117988..bf1a98f 100644 --- a/app/config.py +++ b/app/config.py @@ -18,10 +18,14 @@ subreddits = [ ("linux4noobs", 100), ("selfhosted", 100), ("HomeServer", 100), - ("homelab", 100) + ("homelab", 100), + ("KidsAreFuckingStupid", 100), + ("NonPoliticalTwitter", 100), + ("all", 1000) ] max_age_days = 30 max_age_seconds = max_age_days * 24 * 60 * 60 +other_posts_cutoff = 1 #subreddits with this many unread posts or fewer are merged to /r/other # Webpage configuration posts_per_page_load = 50 diff --git a/app/templates/index.html b/app/templates/index.html index 6db1a6c..588e0ff 100755 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -174,11 +174,14 @@ {% endfor %}
-

{{ title }} ({{ count }})

+

{{ title }}

{% for post in posts %}
-

{{ post.title }}

-

Score: {{ post.score }}

+

{{ post.title }}

+ {% if post.subreddit %} +
{{ post.subreddit }}
+ {% endif %} + {% if post.media_html|length > 0 %}
{% for media in post.media_html %} {{ media|safe }} @@ -188,6 +191,7 @@ {% endif %}
+ {% endif %} {% if post.body %}
{{ post.body }}