diff --git a/app/app.py b/app/app.py index cc704fa..bbbdaa3 100755 --- a/app/app.py +++ b/app/app.py @@ -1,6 +1,7 @@ from flask import Flask, send_file, render_template, request from urllib.parse import urlparse +import delete_posts import config import json import re @@ -27,12 +28,32 @@ def hide_post(permalink): post SET hidden = ? + WHERE + permalink = ? AND + saved = ? + """ + binds = [True, permalink, False] + cursor.execute(update,binds) + connection.commit() + connection.close() + return "" + +@app.route('/save/') +def save_post(permalink): + if permalink[0] != "/": + permalink = "/" + permalink + connection = sqlite3.connect(config.db_file) + cursor = connection.cursor() + action = request.args.get("action") + update = """ + UPDATE + post + SET + saved = ? WHERE permalink = ? """ - binds = [True, permalink] - print(update) - print(binds) + binds = [(action == "save"), permalink] cursor.execute(update,binds) connection.commit() connection.close() @@ -115,6 +136,7 @@ def admin(): return "" cursor.execute(upsert, binds) connection.commit() + delete_posts.run() sidebar_links = get_sidebar_links(cursor) select = """ SELECT @@ -149,12 +171,13 @@ def front_page(): FROM post WHERE - hidden = ? + hidden = ? AND + saved = ? ORDER BY score desc LIMIT ? """ - binds = [False, config.posts_per_page_load] + binds = [False, 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, sidebar_links=sidebar_links) @@ -184,7 +207,8 @@ def other_page(): FROM post WHERE - hidden = ? + hidden = ? AND + saved = ? GROUP BY subreddit ) t @@ -195,11 +219,34 @@ def other_page(): score desc LIMIT ? """ - binds = [False, False, config.other_posts_cutoff, config.posts_per_page_load] + binds = [False, False, False, config.other_posts_cutoff, config.posts_per_page_load] posts = get_posts_from_select(cursor, select, binds) connection.close() return render_template('index.html', title=title, posts=posts, sidebar_links=sidebar_links) +@app.route('/r/saved') +def get_saved(): + title = "/r/saved" + connection = sqlite3.connect(config.db_file) + cursor = connection.cursor() + sidebar_links = get_sidebar_links(cursor) + select = """ + SELECT + post + FROM + post + WHERE + saved = ? + ORDER BY + score desc + LIMIT ? + """ + binds = [True, config.posts_per_page_load] + posts = get_posts_from_select(cursor, select, binds) + connection.close() + return render_template('index.html', title=title, posts=posts, sidebar_links=sidebar_links, saved=True) + + @app.route('/r/') def get_subreddit(subreddit): title = f"/r/{subreddit}" @@ -213,12 +260,13 @@ def get_subreddit(subreddit): post WHERE subreddit = ? AND - hidden = ? + hidden = ? AND + saved = ? ORDER BY score desc LIMIT ? """ - binds = [subreddit, False, config.posts_per_page_load] + binds = [subreddit, False, 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, sidebar_links=sidebar_links) @@ -249,6 +297,7 @@ def get_sidebar_links(cursor): links = [f"/r/{sub[0]}" for sub in results] links.insert(0, "/r/all") links.append("/r/other") + links.append("/r/saved") links.append("/admin") return links diff --git a/app/delete_posts.py b/app/delete_posts.py index a342f39..e888541 100644 --- a/app/delete_posts.py +++ b/app/delete_posts.py @@ -10,14 +10,22 @@ def run(): now = int(time.time()) max_created_utc = now - config.max_age_seconds print("Deleting old posts") - delete = "DELETE FROM post WHERE created_utc < ?" - binds = [max_created_utc] + delete = "DELETE FROM post WHERE created_utc < ? AND saved = ?" + binds = [max_created_utc, False] cursor.execute(delete, binds) print("Deleting posts from blocked subreddits") select = "SELECT name FROM block WHERE name like '/r/%'" binds = [row[0][3:] for row in cursor.execute(select).fetchall()] bind_array = ",".join(["?"]*len(binds)) - delete = f"DELETE FROM post WHERE subreddit IN ({bind_array})" + binds.append(False) + delete = f"DELETE FROM post WHERE subreddit IN ({bind_array}) AND saved = ?" + cursor.execute(delete, binds) + print("Deleting posts from blocked users") + select = "SELECT name FROM block WHERE name like '/u/%'" + binds = [row[0][3:] for row in cursor.execute(select).fetchall()] + bind_array = ",".join(["?"]*len(binds)) + binds.append(False) + delete = f"DELETE FROM post WHERE author IN ({bind_array}) AND saved = ?" cursor.execute(delete, binds) print("Deleting old media db rows") delete = "DELETE FROM media WHERE permalink NOT IN (SELECT permalink FROM post)" diff --git a/app/make_db.py b/app/make_db.py index 4782ccd..21aecbd 100755 --- a/app/make_db.py +++ b/app/make_db.py @@ -6,7 +6,7 @@ import sqlite3 os.makedirs(config.db_dir, exist_ok=True) connection = sqlite3.connect(config.db_file) cursor = connection.cursor() -cursor.execute("CREATE TABLE IF NOT EXISTS post(permalink primary key, subreddit, created_utc, score, media_fetched, post, hidden)") +cursor.execute("CREATE TABLE IF NOT EXISTS post(permalink primary key, subreddit, author, created_utc, score, media_fetched, post, hidden, saved)") cursor.execute("CREATE TABLE IF NOT EXISTS media(permalink, url , local, PRIMARY KEY (permalink, url))") cursor.execute("CREATE TABLE IF NOT EXISTS subreddit(subreddit primary key, minimum_score, fetch_by, fetch_max)") cursor.execute("CREATE TABLE IF NOT EXISTS block(name primary key)") diff --git a/app/scrape_posts.py b/app/scrape_posts.py index 649010c..caa5af5 100755 --- a/app/scrape_posts.py +++ b/app/scrape_posts.py @@ -57,8 +57,8 @@ def scrape_subreddit_data(subreddit, minimum_score=100, pull_by="day", limit=5, def save_posts_to_db(data, cursor): if len(data)==0: return - upsert = "INSERT INTO post(permalink, subreddit, created_utc, score, media_fetched, post, hidden) VALUES " - upsert += ",".join(["(?,?,?,?,?,?,?)"] * len(data)) + upsert = "INSERT INTO post(permalink, subreddit, created_utc, score, media_fetched, post, hidden, saved, author) VALUES " + upsert += ",".join(["(?,?,?,?,?,?,?,?,?)"] * len(data)) upsert += " ON CONFLICT(permalink) DO UPDATE SET score=excluded.score, post=excluded.post" binds = [] for post in data: @@ -69,6 +69,8 @@ def save_posts_to_db(data, cursor): binds.append(False) binds.append(json.dumps(post)) binds.append(False) + binds.append(False) + binds.append(post["author"]) cursor.execute(upsert, binds) def download_media(cursor): diff --git a/app/templates/index.html b/app/templates/index.html index b42e857..995f534 100755 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -10,6 +10,7 @@ --darker: #171717; --light: #bfbfbf; --confirm: #970000; + --saved: #9f9400; } html { height: 100%; @@ -139,6 +140,9 @@ .button-wrapper button.confirm { background-color: var(--confirm) } + .button-wrapper button.saved { + background-color: var(--saved) + } .button-wrapper button.gallery { padding: 5px; background-color: var(--darker); @@ -211,8 +215,9 @@ {% endif %} - - + + + {% endfor %} @@ -284,6 +289,11 @@ // main button code function hide(button, permalink){ + div = button.closest('.post'); + hideButton = div.querySelector('[name="save"]'); + if (hideButton.classList.contains('saved')){ + return; + } if (button.classList.contains('confirm')) { div = button.closest('.post'); div.remove(); @@ -319,6 +329,27 @@ }, 500); } } + + function save(button, permalink) { + if (button.classList.contains("saved")) { + button.classList.remove("saved"); + try { + fetch('/save' + permalink + '?action=unsave'); + } catch (error) { + console.error('Could not unsave', error); + } + } else { + button.classList.add("saved"); + div = button.closest('.post'); + hideButton = div.querySelector('[name="hide"]'); + hideButton.classList.remove("confirm"); + try { + fetch('/save' + permalink + '?action=save'); + } catch (error) { + console.error('Could not save', error); + } + } + } // text expand code function checkHeight(){