diff --git a/html/style.css b/html/style.css index 483d3d1..ed40b8e 100644 --- a/html/style.css +++ b/html/style.css @@ -2,7 +2,7 @@ a { display: block; } -img { - max-width: 100px; - max-height: 200px; +img, video { + max-width: 200px; + max-height: 300px; } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index cb68a86..d687361 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ praw requests +youtube_dl diff --git a/save.py b/save.py index 2479312..04f305a 100755 --- a/save.py +++ b/save.py @@ -35,10 +35,8 @@ posts_html = [] for post in get_posts(client): post_html = get_post_html(post) - media = get_post_media(post) + media = save_media(post, location) 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) diff --git a/utilities.py b/utilities.py index 5a9bde3..7f1bd90 100644 --- a/utilities.py +++ b/utilities.py @@ -1,12 +1,14 @@ import os import praw import requests +import youtube_dl 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"] +PLATFORMS = ["redgifs.com"] def make_client(): return praw.Reddit( @@ -19,7 +21,7 @@ def make_client(): def get_saved_posts(client): - for saved in client.user.me().saved(limit=None): + for saved in client.user.me().saved(limit=10): if saved.__class__.__name__ == "Submission": yield saved @@ -45,24 +47,43 @@ def get_post_html(post): return html -def get_post_media(post): +def save_media(post, location): 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 - } + filename = f"{readable_name}_{post.id}.{extension}" + with open(os.path.join(location, "media", filename), "wb") as f: + f.write(requests.get(post.url).content) + return filename + else: + domain = ".".join(post.url.split("/")[2].split(".")[-2:]) + if domain in PLATFORMS: + options = { + "nocheckcertificate": True, "quiet": True, "no_warnings": True, + "outtmpl": os.path.join( + location, "media", f"{readable_name}_{post.id}" + ".%(ext)s" + ) + } + with youtube_dl.YoutubeDL(options) as ydl: + ydl.download([post.url]) + for f in os.listdir(os.path.join(location, "media")): + if f.startswith(f"{readable_name}_{post.id}"): + return f # gyfcat, v.reddit, imgur, redgifs def add_media_preview_to_html(post_html, media): - extension = media["name"].split(".")[-1] - location = "/".join(["media", media["name"]]) + extension = media.split(".")[-1] + location = "/".join(["media", media]) if extension in IMAGE_EXTENSIONS: return post_html.replace( "", f'' ) + if extension in VIDEO_EXTENSIONS: + return post_html.replace( + "", + f'' + ) \ No newline at end of file