Post pages

This commit is contained in:
Sam Ireland 2021-01-02 22:46:36 +00:00
parent 21f34d19d8
commit 01cc17e408
4 changed files with 51 additions and 18 deletions

16
html/post-div.html Normal file
View File

@ -0,0 +1,16 @@
<div class="post" id="<!--id-->">
<h2><!--title--></h2>
<time title="<!--timestamp-->"><!--date--></time>
<a href="<!--link-->">Link</a>
<a href="<!--reddit-link-->">reddit Link</a>
<a href="<!--content-link-->">Content Link</a>
<div class="subreddit"><!--subreddit--></div>
<div class="user"><!--user--></div>
<div class="body">
<!--body-->
</div>
<div class="preview">
<!--preview-->
</div>
<!--postend--></div>

View File

@ -1,15 +1,13 @@
<div class="post" id="<!--id-->"> <html>
<h2><!--title--></h2> <head>
<time title="<!--timestamp-->"><!--date--></time> <title><!--title--></title>
<a href="<!--link-->">Link</a> <style></style>
<a href="<!--content-link-->">Content Link</a> </head>
<div class="subreddit"><!--subreddit--></div>
<div class="user"><!--user--></div>
<div class="body">
<!--body-->
</div>
<div class="preview">
<!--preview-->
</div>
<!--postend--></div> <body>
<!--post-->
<div class="comments">
<h2>Comments</h2>
</div>
</body>
</html>

View File

@ -29,9 +29,11 @@ else:
html_file = "upvoted.html" html_file = "upvoted.html"
get_posts = get_upvoted_posts get_posts = get_upvoted_posts
# Make directory for media # Make directory for media and posts
if not os.path.exists(os.path.join(location, "media")): if not os.path.exists(os.path.join(location, "media")):
os.mkdir(os.path.join(location, "media")) os.mkdir(os.path.join(location, "media"))
if not os.path.exists(os.path.join(location, "posts")):
os.mkdir(os.path.join(location, "posts"))
# Are there any posts already? # Are there any posts already?
post_ids, posts_html = [], [] post_ids, posts_html = [], []
@ -55,6 +57,9 @@ else:
if media: if media:
post_html = add_media_preview_to_html(post_html, media) post_html = add_media_preview_to_html(post_html, media)
posts_html.append(post_html) posts_html.append(post_html)
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)
# Save HTML # Save HTML
with open(os.path.join("html", html_file)) as f: with open(os.path.join("html", html_file)) as f:
@ -65,3 +70,4 @@ html = html.replace("<!--posts-->", "\n".join(posts_html))
with open(os.path.join(location, html_file), "w") as f: with open(os.path.join(location, html_file), "w") as f:
f.write(html) f.write(html)

View File

@ -46,13 +46,14 @@ def get_post_html(post):
"""Takes a post object and creates a HTML for it - but not including the """Takes a post object and creates a HTML for it - but not including the
preview HTML.""" preview HTML."""
with open(os.path.join("html", "post.html")) as f: with open(os.path.join("html", "post-div.html")) as f:
html = f.read() html = f.read()
dt = datetime.utcfromtimestamp(post.created_utc) dt = datetime.utcfromtimestamp(post.created_utc)
html = html.replace("<!--title-->", post.title) html = html.replace("<!--title-->", post.title)
html = html.replace("<!--subreddit-->", f"/r/{str(post.subreddit)}") html = html.replace("<!--subreddit-->", f"/r/{str(post.subreddit)}")
html = html.replace("<!--user-->", f"/u/{post.author.name}" if post.author else "[deleted]") html = html.replace("<!--user-->", f"/u/{post.author.name}" if post.author else "[deleted]")
html = html.replace("<!--link-->", f"https://reddit.com{post.permalink}") html = html.replace("<!--link-->", f"posts/{post.id}.html")
html = html.replace("<!--reddit-link-->", f"https://reddit.com{post.permalink}")
html = html.replace("<!--content-link-->", post.url) html = html.replace("<!--content-link-->", post.url)
html = html.replace("<!--id-->", post.id) html = html.replace("<!--id-->", post.id)
html = html.replace("<!--body-->", post.selftext_html or "") html = html.replace("<!--body-->", post.selftext_html or "")
@ -154,4 +155,16 @@ def add_media_preview_to_html(post_html, media):
"<!--preview-->", "<!--preview-->",
f'<video controls><source src="{location}"></video>' f'<video controls><source src="{location}"></video>'
) )
return post_html return post_html
def create_post_page_html(post, post_html):
with open(os.path.join("html", "post.html")) as f:
html = f.read()
html = html.replace("<!--title-->", post.title)
html = html.replace("<!--post-->", post_html.replace("h2>", "h1>").replace(
'<img src="media/', '<img src="../media/'
))
with open(os.path.join("html", "style.css")) as f:
html = html.replace("<style></style>", f"<style>\n{f.read()}\n</style>")
return html