Get saved posts
This commit is contained in:
parent
495751b00c
commit
93225db543
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
|||||||
*.pyc
|
*.pyc
|
||||||
__pycache__
|
__pycache__
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
samirelanduk
|
||||||
|
secrets.py
|
4
html/post.html
Normal file
4
html/post.html
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<div class="post">
|
||||||
|
<h2><!--title--></h2>
|
||||||
|
<time title="<!--timestamp-->"><!--date--></time>
|
||||||
|
</div>
|
10
html/saved.html
Normal file
10
html/saved.html
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Saved Posts</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>Saved Posts</h1>
|
||||||
|
<!--posts-->
|
||||||
|
</body>
|
||||||
|
</html>
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
praw
|
22
save.py
22
save.py
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
|
from utilities import *
|
||||||
|
|
||||||
# Get arguments
|
# Get arguments
|
||||||
parser = argparse.ArgumentParser(description="Save reddit posts to file.")
|
parser = argparse.ArgumentParser(description="Save reddit posts to file.")
|
||||||
@ -14,3 +15,24 @@ location = args.location[0]
|
|||||||
# Is location specified a directory?
|
# Is location specified a directory?
|
||||||
if not os.path.isdir(location):
|
if not os.path.isdir(location):
|
||||||
print(location, "is not a directory")
|
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("<!--posts-->", "\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)'''
|
30
utilities.py
Normal file
30
utilities.py
Normal file
@ -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("<!--title-->", post.title)
|
||||||
|
html = html.replace("<!--timestamp-->", str(dt))
|
||||||
|
html = html.replace("<!--date-->", dt.strftime("%d %B, %Y"))
|
||||||
|
return html
|
Loading…
Reference in New Issue
Block a user