Saved comments

This commit is contained in:
Sam Ireland 2021-01-03 00:20:15 +00:00
parent 61015c6087
commit e3c060053e
4 changed files with 54 additions and 6 deletions

View File

@ -1,4 +1,4 @@
<div class="comment">
<div class="comment" id="<!--id-->">
<div class="user"><!--user--></div>
<div class="score"><!--score--></div>
<a href="<!--link-->"><time title="<!--timestamp-->"><!--date--></time></a>
@ -6,4 +6,4 @@
<div class="child-comments">
<!--children-->
</div>
</div>
<!--commentend--></div>

View File

@ -7,5 +7,7 @@
<body>
<h1>Saved Posts</h1>
<!--posts-->
<h1>Saved Comments</h1>
<!--comments-->
</body>
</html>

32
save.py
View File

@ -25,9 +25,11 @@ client = make_client()
if mode == "saved":
html_file = "saved.html"
get_posts = get_saved_posts
get_comments = get_saved_comments
else:
html_file = "upvoted.html"
get_posts = get_upvoted_posts
get_comments = lambda client: []
# Make directory for media and posts
if not os.path.exists(os.path.join(location, "media")):
@ -36,17 +38,18 @@ if not os.path.exists(os.path.join(location, "posts")):
os.mkdir(os.path.join(location, "posts"))
# Are there any posts already?
post_ids, posts_html = [], []
post_ids, existing_posts_html = [], []
if os.path.exists(os.path.join(location, html_file)):
with open(os.path.join(location, html_file)) as f:
current_html = f.read()
post_ids = re.findall(r'id="(.+?)"', current_html)
posts_html = re.findall(
existing_posts_html = re.findall(
r'(<div class="post"[\S\n\t\v ]+?<!--postend--><\/div>)',
current_html
)
# Get posts HTML
posts_html = []
posts = [p for p in get_posts(client) if p.id not in post_ids]
if not posts:
print("No new saved posts")
@ -60,6 +63,30 @@ else:
page_html = create_post_page_html(post, post_html)
with open(os.path.join(location, "posts", f"{post.id}.html"), "w") as f:
f.write(page_html)
posts_html += existing_posts_html
# Are there any comments already?
comment_ids, existing_comments_html = [], []
if os.path.exists(os.path.join(location, html_file)):
with open(os.path.join(location, html_file)) as f:
current_html = f.read()
comment_ids = re.findall(r'id="(.+?)"', current_html)
existing_comments_html = re.findall(
r'(<div class="comment"[\S\n\t\v ]+?<!--commentend--><\/div>)',
current_html
)
# Get comments HTML
comments_html = []
comments = [c for c in get_comments(client) if c.id not in comment_ids]
if not comments:
print("No new saved comments")
else:
for comment in tqdm(comments):
comment_html = get_comment_html(comment)
media = save_media(post, location)
comments_html.append(comment_html)
comments_html += existing_comments_html
# Save HTML
with open(os.path.join("html", html_file)) as f:
@ -67,6 +94,7 @@ with open(os.path.join("html", html_file)) as f:
with open(os.path.join("html", "style.css")) as f:
html = html.replace("<style></style>", f"<style>\n{f.read()}\n</style>")
html = html.replace("<!--posts-->", "\n".join(posts_html))
html = html.replace("<!--comments-->", "\n".join(comments_html))
with open(os.path.join(location, html_file), "w") as f:
f.write(html)

View File

@ -34,7 +34,7 @@ def get_saved_posts(client):
def get_upvoted_posts(client):
"""Gets a list of posts that the user has saved."""
"""Gets a list of posts that the user has upvoted."""
return [
upvoted for upvoted in client.user.me().upvoted(limit=None)
@ -42,6 +42,15 @@ def get_upvoted_posts(client):
]
def get_saved_comments(client):
"""Gets a list of comments that the user has saved."""
return [
saved for saved in client.user.me().saved(limit=None)
if saved.__class__.__name__ != "Submission"
]
def get_post_html(post):
"""Takes a post object and creates a HTML for it - but not including the
preview HTML."""
@ -159,6 +168,8 @@ def add_media_preview_to_html(post_html, media):
def create_post_page_html(post, post_html):
"""Creates the HTML for a post's own page."""
with open(os.path.join("html", "post.html")) as f:
html = f.read()
html = html.replace("<!--title-->", post.title)
@ -176,14 +187,21 @@ def create_post_page_html(post, post_html):
def get_comment_html(comment, children=True):
"""Takes a post object and creates a HTML for it - it will get its children
too unless you specify otherwise."""
with open(os.path.join("html", "comment-div.html")) as f:
html = f.read()
dt = datetime.utcfromtimestamp(comment.created_utc)
html = html.replace("<!--user-->", f"/u/{comment.author.name}" if comment.author else "[deleted]")
html = html.replace(
"<!--user-->",
f"/u/{comment.author.name}" if comment.author else "[deleted]"
)
html = html.replace("<!--body-->", comment.body_html or "")
html = html.replace("<!--score-->", str(comment.score))
html = html.replace("<!--link-->", f"https://reddit.com{comment.permalink}")
html = html.replace("<!--timestamp-->", str(dt))
html = html.replace("<!--id-->", comment.id)
html = html.replace("<!--date-->", dt.strftime("%H:%M - %d %B, %Y"))
if children:
children_html = []