Youtube DL for videos

This commit is contained in:
Sam Ireland 2020-12-31 01:32:06 +00:00
parent 8460ec13bc
commit 80474bc74e
4 changed files with 34 additions and 14 deletions

View File

@ -2,7 +2,7 @@ a {
display: block; display: block;
} }
img { img, video {
max-width: 100px; max-width: 200px;
max-height: 200px; max-height: 300px;
} }

View File

@ -1,2 +1,3 @@
praw praw
requests requests
youtube_dl

View File

@ -35,10 +35,8 @@ posts_html = []
for post in get_posts(client): for post in get_posts(client):
post_html = get_post_html(post) post_html = get_post_html(post)
media = get_post_media(post) media = save_media(post, location)
if media: 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) post_html = add_media_preview_to_html(post_html, media)
posts_html.append(post_html) posts_html.append(post_html)

View File

@ -1,12 +1,14 @@
import os import os
import praw import praw
import requests import requests
import youtube_dl
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"] IMAGE_EXTENSIONS = ["jpg", "jpeg", "png", "gif"]
VIDEO_EXTENSIONS = ["mp4"] VIDEO_EXTENSIONS = ["mp4"]
PLATFORMS = ["redgifs.com"]
def make_client(): def make_client():
return praw.Reddit( return praw.Reddit(
@ -19,7 +21,7 @@ def make_client():
def get_saved_posts(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": if saved.__class__.__name__ == "Submission":
yield saved yield saved
@ -45,24 +47,43 @@ def get_post_html(post):
return html return html
def get_post_media(post): def save_media(post, location):
media_extensions = IMAGE_EXTENSIONS + VIDEO_EXTENSIONS media_extensions = IMAGE_EXTENSIONS + VIDEO_EXTENSIONS
extension = post.url.split(".")[-1].lower() extension = post.url.split(".")[-1].lower()
readable_name = list(filter(bool, post.permalink.split("/")))[-1] readable_name = list(filter(bool, post.permalink.split("/")))[-1]
if extension in media_extensions: if extension in media_extensions:
return { filename = f"{readable_name}_{post.id}.{extension}"
"name": f"{readable_name}_{post.id}.{extension}", with open(os.path.join(location, "media", filename), "wb") as f:
"content": requests.get(post.url).content 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 # gyfcat, v.reddit, imgur, redgifs
def add_media_preview_to_html(post_html, media): def add_media_preview_to_html(post_html, media):
extension = media["name"].split(".")[-1] extension = media.split(".")[-1]
location = "/".join(["media", media["name"]]) location = "/".join(["media", media])
if extension in IMAGE_EXTENSIONS: if extension in IMAGE_EXTENSIONS:
return post_html.replace( return post_html.replace(
"<!--preview-->", "<!--preview-->",
f'<img src="{location}">' f'<img src="{location}">'
) )
if extension in VIDEO_EXTENSIONS:
return post_html.replace(
"<!--preview-->",
f'<video controls><source src="{location}"></video>'
)