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"""
import praw, wget
from dotenv import load_dotenv
from mastodon import Mastodon
from os import getenv, remove
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_dotenv()
@ -42,7 +45,9 @@ for submission in reddit.subreddit(str(getenv("REDDIT_SUBREDDIT"))).top(time_fil
if submission.permalink in posted_submission["content"]:
already_posted = True
break
# abort download if the post has been posted already
if already_posted:
print("Skipping post: https://www.reddit.com" + submission.permalink)
continue
# 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")
# check if reddit video
elif "v.redd.it" in submission.url:
url = submission.media["reddit_video"]["fallback_url"]
url = url.split("?")[0]
filename = wget.download(url)
print()
print("Downloading video post: https://www.reddit.com" + submission.permalink)
downloader = Downloader(url="https://www.reddit.com"+submission.permalink, filename="video.mp4", max_q=True)
downloader.download()
print("\nUploading video")
# 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)
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")
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")
remove(filename)
else:
filename = wget.download(submission.url)
print()
print("Downloading image post: https://www.reddit.com" + submission.permalink)
filename = wget.download(submission.url, out="image." + submission.url.split(".")[-1])
print("\nUploading image")
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")
remove(filename)
break