2022-12-09 00:04:01 +01:00
""" Main script to toot a new post """
2024-01-08 15:21:29 +01:00
import time
2022-12-09 00:04:01 +01:00
from os import getenv , remove
from os . path import getsize
2024-01-04 19:38:26 +01:00
import praw
import wget
from dotenv import load_dotenv
from mastodon import Mastodon
from redvid import Downloader
2023-08-01 18:59:46 +02:00
# ----- load .env ----- #
2022-12-09 00:04:01 +01:00
load_dotenv ( )
2023-08-01 18:59:46 +02:00
# ----- initialization ----- #
2022-12-09 00:04:01 +01:00
# initialize reddit
reddit = praw . Reddit (
client_id = getenv ( " REDDIT_CLIENT_ID " ) ,
client_secret = getenv ( " REDDIT_CLIENT_SECRET " ) ,
redirect_uri = " http://localhost:8080 " , # does not matter what is here
user_agent = getenv ( " REDDIT_USER_AGENT " )
)
# initialize mastodon
mastodon = Mastodon (
access_token = getenv ( " MASTODON_USER_SECRET " ) ,
api_base_url = getenv ( " MASTODON_URL " )
)
2023-08-01 18:59:46 +02:00
# ----- main script ----- #
2022-12-09 00:04:01 +01:00
# get recent posts to check for duplicates
last_posts = mastodon . account_statuses ( getenv ( " MASTODON_USER_ID " ) , limit = 15 )
2022-12-10 00:46:15 +01:00
for submission in reddit . subreddit ( str ( getenv ( " REDDIT_SUBREDDIT " ) ) ) . top ( time_filter = " day " , limit = 10 ) :
2022-12-09 00:04:01 +01:00
# skip post if it is a stickied post
if ( submission . stickied ) :
continue
2023-08-01 18:59:46 +02:00
2022-12-09 00:04:01 +01:00
# check if the post has been posted before
already_posted = False
for posted_submission in last_posts :
if submission . permalink in posted_submission [ " content " ] :
already_posted = True
break
2024-01-04 19:38:26 +01:00
# abort download if the post has been posted already
2022-12-09 00:04:01 +01:00
if already_posted :
2024-01-04 19:38:26 +01:00
print ( " Skipping post: https://www.reddit.com " + submission . permalink )
2022-12-09 00:04:01 +01:00
continue
2023-08-01 18:59:46 +02:00
2022-12-09 00:04:01 +01:00
# check if text only
if submission . is_self :
status_text = submission . title + " \n \n geposted von u/ " + submission . author . name + " \n " + submission . permalink + " \n \n " + submission . selftext
mastodon . status_post ( status_text , visibility = " unlisted " )
# check if reddit video
elif " v.redd.it " in submission . url :
2024-01-08 15:12:30 +01:00
filename = " video.mp4 "
2024-01-08 15:10:05 +01:00
# download video with highest possible resolution while not exceeding 10MB
2024-01-04 19:38:26 +01:00
print ( " Downloading video post: https://www.reddit.com " + submission . permalink )
2024-01-08 15:12:30 +01:00
downloader = Downloader ( url = " https://www.reddit.com " + submission . permalink , filename = filename , max_s = 10_000_000 , auto_max = True )
2024-01-08 15:10:05 +01:00
return_val = downloader . download ( )
2024-01-04 19:38:26 +01:00
print ( " \n Uploading video " )
2023-08-01 18:59:46 +02:00
2024-01-08 15:10:05 +01:00
# check if video was downloaded successfully (digit return value if unsuccessful, otherwise string value containing the path to the video)
if str ( return_val ) . isdigit ( ) :
status_text = submission . title + " \n \n Das gepostete Video ist leider zu groß für diesen Server. Das Video bzw. der Post kann über den untenstehenden Link aufgerufen werden. " + " \n \n geposted von u/ " + submission . author . name + " \n https://www.reddit.com " + submission . permalink
mastodon . status_post ( status_text , visibility = " unlisted " )
else :
2022-12-09 00:04:01 +01:00
media = mastodon . media_post ( filename )
2024-01-08 15:21:29 +01:00
time . sleep ( 60.0 )
2024-01-04 19:38:26 +01:00
status_text = submission . title + " \n \n geposted von u/ " + submission . author . name + " \n https://www.reddit.com " + submission . permalink
2022-12-09 00:04:01 +01:00
mastodon . status_post ( status_text , media_ids = media [ " id " ] , visibility = " unlisted " )
2024-01-04 19:38:26 +01:00
2022-12-09 00:04:01 +01:00
remove ( filename )
else :
2024-01-04 19:38:26 +01:00
print ( " Downloading image post: https://www.reddit.com " + submission . permalink )
filename = wget . download ( submission . url , out = " image. " + submission . url . split ( " . " ) [ - 1 ] )
print ( " \n Uploading image " )
2022-12-09 00:04:01 +01:00
media = mastodon . media_post ( filename )
2024-01-04 19:38:26 +01:00
status_text = submission . title + " \n \n geposted von u/ " + submission . author . name + " \n https://www.reddit.com " + submission . permalink
2022-12-09 00:04:01 +01:00
mastodon . status_post ( status_text , media_ids = media [ " id " ] , visibility = " unlisted " )
2024-01-04 19:38:26 +01:00
2022-12-09 00:04:01 +01:00
remove ( filename )
break