Display image previews
This commit is contained in:
parent
e078fbb8b9
commit
8460ec13bc
3
.gitignore
vendored
3
.gitignore
vendored
@ -2,4 +2,5 @@
|
|||||||
__pycache__
|
__pycache__
|
||||||
.DS_Store
|
.DS_Store
|
||||||
samirelanduk
|
samirelanduk
|
||||||
secrets.py
|
secrets.py
|
||||||
|
secrets2.py
|
@ -8,4 +8,7 @@
|
|||||||
<div class="body">
|
<div class="body">
|
||||||
<!--body-->
|
<!--body-->
|
||||||
</div>
|
</div>
|
||||||
|
<div class="preview">
|
||||||
|
<!--preview-->
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
@ -1,3 +1,8 @@
|
|||||||
a {
|
a {
|
||||||
display: block;
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
img {
|
||||||
|
max-width: 100px;
|
||||||
|
max-height: 200px;
|
||||||
}
|
}
|
@ -1 +1,2 @@
|
|||||||
praw
|
praw
|
||||||
|
requests
|
||||||
|
17
save.py
17
save.py
@ -19,6 +19,7 @@ if not os.path.isdir(location):
|
|||||||
# Make a client object
|
# Make a client object
|
||||||
client = make_client()
|
client = make_client()
|
||||||
|
|
||||||
|
# Saved posts or upvoted posts?
|
||||||
if mode == "saved":
|
if mode == "saved":
|
||||||
html_file = "saved.html"
|
html_file = "saved.html"
|
||||||
get_posts = get_saved_posts
|
get_posts = get_saved_posts
|
||||||
@ -26,10 +27,20 @@ else:
|
|||||||
html_file = "upvoted.html"
|
html_file = "upvoted.html"
|
||||||
get_posts = get_upvoted_posts
|
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 = []
|
posts_html = []
|
||||||
|
|
||||||
for post in get_posts(client):
|
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:
|
with open(os.path.join("html", html_file)) as f:
|
||||||
html = f.read()
|
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:
|
with open(os.path.join(location, html_file), "w") as f:
|
||||||
f.write(html)
|
f.write(html)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
'''for post in get_saved_posts(client):
|
|
||||||
print(post.title)'''
|
|
31
utilities.py
31
utilities.py
@ -1,9 +1,13 @@
|
|||||||
import os
|
import os
|
||||||
import praw
|
import praw
|
||||||
|
import requests
|
||||||
from datetime import datetime
|
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"]
|
||||||
|
VIDEO_EXTENSIONS = ["mp4"]
|
||||||
|
|
||||||
def make_client():
|
def make_client():
|
||||||
return praw.Reddit(
|
return praw.Reddit(
|
||||||
username=REDDIT_USERNAME,
|
username=REDDIT_USERNAME,
|
||||||
@ -15,7 +19,7 @@ def make_client():
|
|||||||
|
|
||||||
|
|
||||||
def get_saved_posts(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":
|
if saved.__class__.__name__ == "Submission":
|
||||||
yield saved
|
yield saved
|
||||||
|
|
||||||
@ -38,4 +42,27 @@ def get_post_html(post):
|
|||||||
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"))
|
||||||
return html
|
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