From 8460ec13bc20a357c38da755e56467a58047eb30 Mon Sep 17 00:00:00 2001 From: Sam Ireland Date: Thu, 31 Dec 2020 00:33:30 +0000 Subject: [PATCH] Display image previews --- .gitignore | 3 ++- html/post.html | 3 +++ html/style.css | 5 +++++ requirements.txt | 1 + save.py | 17 ++++++++++++----- utilities.py | 31 +++++++++++++++++++++++++++++-- 6 files changed, 52 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 9ee7c89..401bc88 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ __pycache__ .DS_Store samirelanduk -secrets.py \ No newline at end of file +secrets.py +secrets2.py \ No newline at end of file diff --git a/html/post.html b/html/post.html index 864bf23..c2fa751 100644 --- a/html/post.html +++ b/html/post.html @@ -8,4 +8,7 @@
+
+ +
\ No newline at end of file diff --git a/html/style.css b/html/style.css index fe90ed7..483d3d1 100644 --- a/html/style.css +++ b/html/style.css @@ -1,3 +1,8 @@ a { display: block; +} + +img { + max-width: 100px; + max-height: 200px; } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 6c81bc0..cb68a86 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ praw +requests diff --git a/save.py b/save.py index c93404d..2479312 100755 --- a/save.py +++ b/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("", "\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)''' \ No newline at end of file diff --git a/utilities.py b/utilities.py index 6678e5a..5a9bde3 100644 --- a/utilities.py +++ b/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 @@ -38,4 +42,27 @@ def get_post_html(post): html = html.replace("", post.selftext_html or "") html = html.replace("", str(dt)) html = html.replace("", dt.strftime("%d %B, %Y")) - return html \ No newline at end of file + 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( + "", + f'' + )