Change video download logic to enable videos with sound

This commit is contained in:
lluni 2024-01-04 19:38:26 +01:00
parent 34c734bc0d
commit cf2d9b8af6
Signed by: lluni
GPG key ID: ACEEB468BC325D35

View file

@ -1,11 +1,14 @@
"""Main script to toot a new post""" """Main script to toot a new post"""
import praw, wget
from dotenv import load_dotenv
from mastodon import Mastodon
from os import getenv, remove from os import getenv, remove
from os.path import getsize from os.path import getsize
import praw
import wget
from dotenv import load_dotenv
from mastodon import Mastodon
from redvid import Downloader
# ----- load .env ----- # # ----- load .env ----- #
load_dotenv() load_dotenv()
@ -42,7 +45,9 @@ for submission in reddit.subreddit(str(getenv("REDDIT_SUBREDDIT"))).top(time_fil
if submission.permalink in posted_submission["content"]: if submission.permalink in posted_submission["content"]:
already_posted = True already_posted = True
break break
# abort download if the post has been posted already
if already_posted: if already_posted:
print("Skipping post: https://www.reddit.com" + submission.permalink)
continue continue
# check if text only # check if text only
@ -51,25 +56,30 @@ for submission in reddit.subreddit(str(getenv("REDDIT_SUBREDDIT"))).top(time_fil
mastodon.status_post(status_text, visibility="unlisted") mastodon.status_post(status_text, visibility="unlisted")
# check if reddit video # check if reddit video
elif "v.redd.it" in submission.url: elif "v.redd.it" in submission.url:
url = submission.media["reddit_video"]["fallback_url"] print("Downloading video post: https://www.reddit.com" + submission.permalink)
url = url.split("?")[0] downloader = Downloader(url="https://www.reddit.com"+submission.permalink, filename="video.mp4", max_q=True)
filename = wget.download(url) downloader.download()
print()
print("\nUploading video")
# check if video file is small enough for the server (10MB) # check if video file is small enough for the server (10MB)
if getsize(filename) < 10*1000000: if getsize(filename) < 10_000_000:
media = mastodon.media_post(filename) media = mastodon.media_post(filename)
status_text = submission.title + "\n\ngeposted von u/" + submission.author.name + "\nhttps://reddit.com" + submission.permalink status_text = submission.title + "\n\ngeposted von u/" + submission.author.name + "\nhttps://www.reddit.com" + submission.permalink
mastodon.status_post(status_text, media_ids=media["id"], visibility="unlisted") mastodon.status_post(status_text, media_ids=media["id"], visibility="unlisted")
else: else:
status_text = submission.title + "\n\nGeposteter Videolink: " + url + "\n\ngeposted von u/" + submission.author.name + "\n" + submission.permalink + "\n\n" + submission.selftext status_text = submission.title + "\n\nDas gepostete Video ist leider zu lang für diesen Server. Das Video bzw. der Post kann über den untenstehenden Link aufgerufen werden. " + "\n\ngeposted von u/" + submission.author.name + "\nhttps://www.reddit.com" + submission.permalink
mastodon.status_post(status_text, visibility="unlisted") mastodon.status_post(status_text, visibility="unlisted")
remove(filename) remove(filename)
else: else:
filename = wget.download(submission.url) print("Downloading image post: https://www.reddit.com" + submission.permalink)
print() filename = wget.download(submission.url, out="image." + submission.url.split(".")[-1])
print("\nUploading image")
media = mastodon.media_post(filename) media = mastodon.media_post(filename)
status_text = submission.title + "\n\ngeposted von u/" + submission.author.name + "\nhttps://reddit.com" + submission.permalink status_text = submission.title + "\n\ngeposted von u/" + submission.author.name + "\nhttps://www.reddit.com" + submission.permalink
mastodon.status_post(status_text, media_ids=media["id"], visibility="unlisted") mastodon.status_post(status_text, media_ids=media["id"], visibility="unlisted")
remove(filename) remove(filename)
break break