diff --git a/.gitignore b/.gitignore index 5a5e00e..9ee7c89 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.pyc __pycache__ -.DS_Store \ No newline at end of file +.DS_Store +samirelanduk +secrets.py \ No newline at end of file diff --git a/html/post.html b/html/post.html new file mode 100644 index 0000000..05d9bb5 --- /dev/null +++ b/html/post.html @@ -0,0 +1,4 @@ +
+

+ +
\ No newline at end of file diff --git a/html/saved.html b/html/saved.html new file mode 100644 index 0000000..cf7aca2 --- /dev/null +++ b/html/saved.html @@ -0,0 +1,10 @@ + + + Saved Posts + + + +

Saved Posts

+ + + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6c81bc0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +praw diff --git a/save.py b/save.py index cabdc84..2680a4a 100755 --- a/save.py +++ b/save.py @@ -2,6 +2,7 @@ import argparse import os +from utilities import * # Get arguments parser = argparse.ArgumentParser(description="Save reddit posts to file.") @@ -13,4 +14,25 @@ location = args.location[0] # Is location specified a directory? if not os.path.isdir(location): - print(location, "is not a directory") \ No newline at end of file + print(location, "is not a directory") + +# Make a client object +client = make_client() + +posts_html = [] + +for post in get_saved_posts(client): + posts_html.append(get_post_html(post)) + +with open(os.path.join("html", "saved.html")) as f: + html = f.read() + +html = html.replace("", "\n".join(posts_html)) + +with open(os.path.join(location, "saved.html"), "w") as f: + f.write(html) + + + +'''for post in get_saved_posts(client): + print(post.title)''' \ No newline at end of file diff --git a/utilities.py b/utilities.py new file mode 100644 index 0000000..56a0a1c --- /dev/null +++ b/utilities.py @@ -0,0 +1,30 @@ +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): + for saved in client.user.me().saved(limit=None): + if saved.__class__.__name__ == "Submission": + yield saved + + +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("", post.title) + html = html.replace("", str(dt)) + html = html.replace("", dt.strftime("%d %B, %Y")) + return html \ No newline at end of file