Allow backing up of specific username

This commit is contained in:
Sam Ireland 2023-08-26 14:56:54 +01:00
parent 54ff47d9ed
commit 61f61b6535
3 changed files with 64 additions and 7 deletions

26
html/username.html Normal file
View File

@ -0,0 +1,26 @@
<html>
<head>
<title>Saved Posts</title>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,600;0,700;1,300;1,400;1,600;1,700&display=swap" rel="stylesheet">
<style></style>
<script></script>
</head>
<body>
<div class="links">
<a href="[username].p.html">Previous</a>
<a href="[username].n.html">Next</a>
</div>
<section class="posts-section">
<h1>u/[username]'s' Posts</h1>
<!--posts-->
</section>
<section class="comments-section">
<h1>u/[username]'s' Comments</h1>
<!--comments-->
</section>
<div class="links">
<a href="[username].n.html">Previous</a>
<a href="[username].n.html">Next</a>
</div>
</body>
</html>

17
save.py
View File

@ -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)
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])

View File

@ -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("<style></style>", f"<style>\n{f.read()}\n</style>")
with open(os.path.join("html", "main.js"), encoding="utf-8") as f: