Display image previews
This commit is contained in:
parent
e078fbb8b9
commit
8460ec13bc
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@ __pycache__
|
||||
.DS_Store
|
||||
samirelanduk
|
||||
secrets.py
|
||||
secrets2.py
|
@ -8,4 +8,7 @@
|
||||
<div class="body">
|
||||
<!--body-->
|
||||
</div>
|
||||
<div class="preview">
|
||||
<!--preview-->
|
||||
</div>
|
||||
</div>
|
@ -1,3 +1,8 @@
|
||||
a {
|
||||
display: block;
|
||||
}
|
||||
|
||||
img {
|
||||
max-width: 100px;
|
||||
max-height: 200px;
|
||||
}
|
@ -1 +1,2 @@
|
||||
praw
|
||||
requests
|
||||
|
17
save.py
17
save.py
@ -19,6 +19,7 @@ if not os.path.isdir(location):
|
||||
# Make a client object
|
||||
client = make_client()
|
||||
|
||||
# Saved posts or upvoted posts?
|
||||
if mode == "saved":
|
||||
html_file = "saved.html"
|
||||
get_posts = get_saved_posts
|
||||
@ -26,10 +27,20 @@ else:
|
||||
html_file = "upvoted.html"
|
||||
get_posts = get_upvoted_posts
|
||||
|
||||
# Make directory for media
|
||||
if not os.path.exists(os.path.join(location, "media")):
|
||||
os.mkdir(os.path.join(location, "media"))
|
||||
|
||||
posts_html = []
|
||||
|
||||
for post in get_posts(client):
|
||||
posts_html.append(get_post_html(post))
|
||||
post_html = get_post_html(post)
|
||||
media = get_post_media(post)
|
||||
if media:
|
||||
with open(os.path.join(location, "media", media["name"]), "wb") as f:
|
||||
f.write(media["content"])
|
||||
post_html = add_media_preview_to_html(post_html, media)
|
||||
posts_html.append(post_html)
|
||||
|
||||
with open(os.path.join("html", html_file)) as f:
|
||||
html = f.read()
|
||||
@ -42,7 +53,3 @@ html = html.replace("<!--posts-->", "\n".join(posts_html))
|
||||
with open(os.path.join(location, html_file), "w") as f:
|
||||
f.write(html)
|
||||
|
||||
|
||||
|
||||
'''for post in get_saved_posts(client):
|
||||
print(post.title)'''
|
29
utilities.py
29
utilities.py
@ -1,9 +1,13 @@
|
||||
import os
|
||||
import praw
|
||||
import requests
|
||||
from datetime import datetime
|
||||
from secrets import REDDIT_USERNAME, REDDIT_PASSWORD
|
||||
from secrets import REDDIT_CLIENT_ID, REDDIT_SECRET
|
||||
|
||||
IMAGE_EXTENSIONS = ["jpg", "jpeg", "png", "gif"]
|
||||
VIDEO_EXTENSIONS = ["mp4"]
|
||||
|
||||
def make_client():
|
||||
return praw.Reddit(
|
||||
username=REDDIT_USERNAME,
|
||||
@ -15,7 +19,7 @@ def make_client():
|
||||
|
||||
|
||||
def get_saved_posts(client):
|
||||
for saved in client.user.me().saved(limit=10):
|
||||
for saved in client.user.me().saved(limit=None):
|
||||
if saved.__class__.__name__ == "Submission":
|
||||
yield saved
|
||||
|
||||
@ -39,3 +43,26 @@ def get_post_html(post):
|
||||
html = html.replace("<!--timestamp-->", str(dt))
|
||||
html = html.replace("<!--date-->", dt.strftime("%d %B, %Y"))
|
||||
return html
|
||||
|
||||
|
||||
def get_post_media(post):
|
||||
media_extensions = IMAGE_EXTENSIONS + VIDEO_EXTENSIONS
|
||||
extension = post.url.split(".")[-1].lower()
|
||||
readable_name = list(filter(bool, post.permalink.split("/")))[-1]
|
||||
if extension in media_extensions:
|
||||
return {
|
||||
"name": f"{readable_name}_{post.id}.{extension}",
|
||||
"content": requests.get(post.url).content
|
||||
}
|
||||
|
||||
# gyfcat, v.reddit, imgur, redgifs
|
||||
|
||||
|
||||
def add_media_preview_to_html(post_html, media):
|
||||
extension = media["name"].split(".")[-1]
|
||||
location = "/".join(["media", media["name"]])
|
||||
if extension in IMAGE_EXTENSIONS:
|
||||
return post_html.replace(
|
||||
"<!--preview-->",
|
||||
f'<img src="{location}">'
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user