Don't do previously saved items

This commit is contained in:
Sam Ireland 2021-01-02 22:33:47 +00:00
parent d28398e6f0
commit 21f34d19d8
3 changed files with 29 additions and 15 deletions

View File

@ -1,4 +1,4 @@
<div class="post"> <div class="post" id="<!--id-->">
<h2><!--title--></h2> <h2><!--title--></h2>
<time title="<!--timestamp-->"><!--date--></time> <time title="<!--timestamp-->"><!--date--></time>
<a href="<!--link-->">Link</a> <a href="<!--link-->">Link</a>
@ -11,4 +11,5 @@
<div class="preview"> <div class="preview">
<!--preview--> <!--preview-->
</div> </div>
</div>
<!--postend--></div>

34
save.py
View File

@ -2,6 +2,7 @@
import argparse import argparse
import os import os
import re
from tqdm import tqdm from tqdm import tqdm
from utilities import * from utilities import *
@ -32,24 +33,35 @@ else:
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"))
posts_html = [] # Are there any posts already?
post_ids, 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(
r'(<div class="post"[\S\n\t\v ]+?<!--postend--><\/div>)',
current_html
)
posts = get_posts(client) # Get posts HTML
for post in tqdm(posts): posts = [p for p in get_posts(client) if p.id not in post_ids]
post_html = get_post_html(post) if not posts:
media = save_media(post, location) print("No new saved posts")
if media: else:
post_html = add_media_preview_to_html(post_html, media) for post in tqdm(posts):
posts_html.append(post_html) post_html = get_post_html(post)
media = save_media(post, location)
if media:
post_html = add_media_preview_to_html(post_html, media)
posts_html.append(post_html)
# Save HTML
with open(os.path.join("html", html_file)) as f: with open(os.path.join("html", html_file)) as f:
html = f.read() html = f.read()
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>")
html = html.replace("<!--posts-->", "\n".join(posts_html)) 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

@ -8,7 +8,7 @@ from datetime import datetime
from secrets import REDDIT_USERNAME, REDDIT_PASSWORD from secrets import REDDIT_USERNAME, REDDIT_PASSWORD
from secrets import REDDIT_CLIENT_ID, REDDIT_SECRET from secrets import REDDIT_CLIENT_ID, REDDIT_SECRET
IMAGE_EXTENSIONS = ["jpg", "jpeg", "png", "gif", "gifv"] IMAGE_EXTENSIONS = ["gif", "gifv", "jpg", "jpeg", "png"]
VIDEO_EXTENSIONS = ["mp4"] VIDEO_EXTENSIONS = ["mp4"]
PLATFORMS = ["redgifs.com", "gfycat.com", "imgur.com", "youtube.com"] PLATFORMS = ["redgifs.com", "gfycat.com", "imgur.com", "youtube.com"]
@ -38,7 +38,7 @@ def get_upvoted_posts(client):
return [ return [
upvoted for upvoted in client.user.me().upvoted(limit=None) upvoted for upvoted in client.user.me().upvoted(limit=None)
if saved.__class__.__name__ == "Submission" if upvoted.__class__.__name__ == "Submission"
] ]
@ -54,6 +54,7 @@ def get_post_html(post):
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"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("<!--body-->", post.selftext_html or "") html = html.replace("<!--body-->", post.selftext_html or "")
html = html.replace("<!--timestamp-->", str(dt)) html = html.replace("<!--timestamp-->", str(dt))
html = html.replace("<!--date-->", dt.strftime("%d %B, %Y")) html = html.replace("<!--date-->", dt.strftime("%d %B, %Y"))