From 61f61b6535645ed58d6c42a47c6c0e111b19e435 Mon Sep 17 00:00:00 2001 From: Sam Ireland Date: Sat, 26 Aug 2023 14:56:54 +0100 Subject: [PATCH] Allow backing up of specific username --- html/username.html | 26 ++++++++++++++++++++++++++ save.py | 17 +++++++++++++---- utilities.py | 28 +++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 html/username.html diff --git a/html/username.html b/html/username.html new file mode 100644 index 0000000..47636ba --- /dev/null +++ b/html/username.html @@ -0,0 +1,26 @@ + + +Saved Posts + + + + + + +
+

u/[username]'s' Posts

+ +
+
+

u/[username]'s' Comments

+ +
+ + + \ No newline at end of file diff --git a/save.py b/save.py index 39ecd22..410019f 100644 --- a/save.py +++ b/save.py @@ -7,8 +7,12 @@ from tqdm import tqdm from utilities import * # Get arguments +def validate_mode(mode): + if mode not in ["saved", "upvoted"] and not mode.startswith("user:"): + raise argparse.ArgumentTypeError(f"Invalid mode: {mode}") + return mode parser = argparse.ArgumentParser(description="Save reddit posts to file.") -parser.add_argument("mode", type=str, nargs=1, choices=["saved", "upvoted"], help="The file to convert.") +parser.add_argument("mode", type=validate_mode, nargs=1, help="The file to convert.") if os.getenv("DOCKER", "0") != "1": parser.add_argument("location", type=str, nargs=1, help="The path to save to.") # Optional page size argument @@ -30,10 +34,15 @@ if mode == "saved": html_file = "saved.html" get_posts = get_saved_posts get_comments = get_saved_comments -else: +elif mode == "upvoted": html_file = "upvoted.html" get_posts = get_upvoted_posts get_comments = lambda client: [] +elif mode.startswith("user:"): + username = mode.split(":")[-1] + html_file = f"{username}.html" + get_posts = lambda client: get_user_posts(client, username) + get_comments = lambda client: get_user_comments(client, username) # Make directory for media and posts if not os.path.exists(os.path.join(location, "media")): @@ -85,5 +94,5 @@ if page_size: posts_on_page = posts_html[i*page_size:(i+1)*page_size] comments_on_page = comments_html[i*page_size:(i+1)*page_size] has_next = i < page_count - 1 - save_html(posts_on_page, comments_on_page, location, html_file, i, has_next) -save_html(posts_html, comments_html, location, html_file, None, False) \ No newline at end of file + save_html(posts_on_page, comments_on_page, location, html_file, i, has_next, username=html_file.split(".")[0]) +save_html(posts_html, comments_html, location, html_file, None, False, username=html_file.split(".")[0]) diff --git a/utilities.py b/utilities.py index cbfe849..3a11066 100644 --- a/utilities.py +++ b/utilities.py @@ -23,6 +23,8 @@ PLATFORMS = ["redgifs.com", "gfycat.com", "imgur.com", "youtube.com"] def make_client(): """Creates a PRAW client with the details in the secrets.py file.""" + print(REDDIT_USERNAME) + return praw.Reddit( username=REDDIT_USERNAME, password=REDDIT_PASSWORD, @@ -92,6 +94,22 @@ def get_saved_comments(client): ] +def get_user_posts(client, username): + """Gets a list of posts that the user has made.""" + + return [ + post for post in client.redditor(username).submissions.new(limit=None) + ] + + +def get_user_comments(client, username): + """Gets a list of comments that the user has made.""" + + return [ + comment for comment in client.redditor(username).comments.new(limit=None) + ] + + def get_post_html(post): """Takes a post object and creates a HTML for it - but not including the preview HTML.""" @@ -278,9 +296,13 @@ def get_comment_html(comment, children=True, op=None): return html -def save_html(posts, comments, location, html_file, page, has_next): - with open(os.path.join("html", html_file), encoding="utf-8") as f: - html = f.read() +def save_html(posts, comments, location, html_file, page, has_next, username=None): + if username: + with open(os.path.join("html", "username.html"), encoding="utf-8") as f: + html = f.read().replace("[username]", username) + else: + with open(os.path.join("html", html_file), encoding="utf-8") as f: + html = f.read() with open(os.path.join("html", "style.css"), encoding="utf-8") as f: html = html.replace("", f"") with open(os.path.join("html", "main.js"), encoding="utf-8") as f: