reddit-save/utilities.py

40 lines
1.3 KiB
Python
Raw Normal View History

2020-12-30 23:59:55 +01:00
import os
import praw
from datetime import datetime
from secrets import REDDIT_USERNAME, REDDIT_PASSWORD
from secrets import REDDIT_CLIENT_ID, REDDIT_SECRET
def make_client():
return praw.Reddit(
username=REDDIT_USERNAME,
password=REDDIT_PASSWORD,
client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_SECRET,
user_agent="reddit-save",
)
def get_saved_posts(client):
2020-12-31 00:51:41 +01:00
for saved in client.user.me().saved(limit=10):
2020-12-30 23:59:55 +01:00
if saved.__class__.__name__ == "Submission":
yield saved
2020-12-31 00:05:45 +01:00
def get_upvoted_posts(client):
for upvoted in client.user.me().upvoted(limit=None):
if upvoted.__class__.__name__ == "Submission":
yield upvoted
2020-12-30 23:59:55 +01:00
def get_post_html(post):
with open(os.path.join("html", "post.html")) as f:
html = f.read()
dt = datetime.utcfromtimestamp(post.created_utc)
html = html.replace("<!--title-->", post.title)
2020-12-31 00:47:15 +01:00
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("<!--link-->", f"https://reddit.com{post.permalink}")
html = html.replace("<!--body-->", post.selftext_html or "")
2020-12-30 23:59:55 +01:00
html = html.replace("<!--timestamp-->", str(dt))
html = html.replace("<!--date-->", dt.strftime("%d %B, %Y"))
return html