Save top-level comments

This commit is contained in:
Sam Ireland 2021-01-02 23:18:26 +00:00
parent 01cc17e408
commit 301e95b450
3 changed files with 26 additions and 1 deletions

6
html/comment-div.html Normal file
View File

@ -0,0 +1,6 @@
<div class="comment">
<div class="user"><!--user--></div>
<div class="score"><!--score--></div>
<a href="<!--link-->"><time title="<!--timestamp-->"><!--date--></time></a>
<div class="body"><!--body--></div>
</div>

View File

@ -8,6 +8,7 @@
<!--post--> <!--post-->
<div class="comments"> <div class="comments">
<h2>Comments</h2> <h2>Comments</h2>
<!--comments-->
</div> </div>
</body> </body>
</html> </html>

View File

@ -28,7 +28,7 @@ def get_saved_posts(client):
"""Gets a list of posts that the user has saved.""" """Gets a list of posts that the user has saved."""
return [ return [
saved for saved in client.user.me().saved(limit=None) saved for saved in client.user.me().saved(limit=20)
if saved.__class__.__name__ == "Submission" if saved.__class__.__name__ == "Submission"
] ]
@ -167,4 +167,22 @@ def create_post_page_html(post, post_html):
)) ))
with open(os.path.join("html", "style.css")) 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("<style></style>", f"<style>\n{f.read()}\n</style>")
comments_html = []
post.comments.replace_more(limit=0)
for comment in post.comments:
comments_html.append(get_comment_html(comment))
html = html.replace("<!--comments-->", "\n".join(comments_html))
return html
def get_comment_html(comment):
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("<!--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("<!--date-->", dt.strftime("%H:%M - %d %B, %Y"))
return html return html