Add some comments

This commit is contained in:
lluni 2023-08-01 18:59:46 +02:00
parent 091927de06
commit f86e7f0a02
Signed by: lluni
GPG key ID: ACEEB468BC325D35
3 changed files with 21 additions and 11 deletions

View file

@ -4,17 +4,20 @@ from dotenv import load_dotenv
from mastodon import Mastodon from mastodon import Mastodon
from os import getenv from os import getenv
# load .env # ----- load .env ----- #
load_dotenv() load_dotenv()
# register application # ----- register application ----- #
Mastodon.create_app( Mastodon.create_app(
getenv("MASTODON_APP_NAME"), getenv("MASTODON_APP_NAME"),
api_base_url = str(getenv("MASTODON_URL")), api_base_url = str(getenv("MASTODON_URL")),
to_file = getenv("MASTODON_CLIENT_SECRET") to_file = getenv("MASTODON_CLIENT_SECRET")
) )
# login # ----- login Mastodon account ----- #
mastodon = Mastodon( mastodon = Mastodon(
client_id = getenv("MASTODON_CLIENT_SECRET"), client_id = getenv("MASTODON_CLIENT_SECRET"),
api_base_url = getenv("MASTODON_URL") api_base_url = getenv("MASTODON_URL")
@ -25,5 +28,6 @@ mastodon.log_in(
to_file = getenv("MASTODON_USER_SECRET") to_file = getenv("MASTODON_USER_SECRET")
) )
# test toot # ----- post test toot ----- #
mastodon.toot("Hello World") mastodon.toot("Hello World")

View file

@ -4,10 +4,12 @@ import praw
from dotenv import load_dotenv from dotenv import load_dotenv
from os import getenv from os import getenv
# load .env # ----- load .env ----- #
load_dotenv() load_dotenv()
# initialize reddit # ----- initialize reddit ----- #
reddit = praw.Reddit( reddit = praw.Reddit(
client_id = getenv("REDDIT_CLIENT_ID"), client_id = getenv("REDDIT_CLIENT_ID"),
client_secret = getenv("REDDIT_CLIENT_SECRET"), client_secret = getenv("REDDIT_CLIENT_SECRET"),

View file

@ -6,9 +6,12 @@ from mastodon import Mastodon
from os import getenv, remove from os import getenv, remove
from os.path import getsize from os.path import getsize
# load .env # ----- load .env ----- #
load_dotenv() load_dotenv()
# ----- initialization ----- #
# initialize reddit # initialize reddit
reddit = praw.Reddit( reddit = praw.Reddit(
client_id = getenv("REDDIT_CLIENT_ID"), client_id = getenv("REDDIT_CLIENT_ID"),
@ -23,6 +26,8 @@ mastodon = Mastodon(
api_base_url = getenv("MASTODON_URL") api_base_url = getenv("MASTODON_URL")
) )
# ----- main script ----- #
# get recent posts to check for duplicates # get recent posts to check for duplicates
last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), limit=15) last_posts = mastodon.account_statuses(getenv("MASTODON_USER_ID"), limit=15)
@ -30,7 +35,7 @@ for submission in reddit.subreddit(str(getenv("REDDIT_SUBREDDIT"))).top(time_fil
# skip post if it is a stickied post # skip post if it is a stickied post
if (submission.stickied): if (submission.stickied):
continue continue
# check if the post has been posted before # check if the post has been posted before
already_posted = False already_posted = False
for posted_submission in last_posts: for posted_submission in last_posts:
@ -39,7 +44,7 @@ for submission in reddit.subreddit(str(getenv("REDDIT_SUBREDDIT"))).top(time_fil
break break
if already_posted: if already_posted:
continue continue
# check if text only # check if text only
if submission.is_self: if submission.is_self:
status_text = submission.title + "\n\ngeposted von u/" + submission.author.name + "\n" + submission.permalink + "\n\n" + submission.selftext status_text = submission.title + "\n\ngeposted von u/" + submission.author.name + "\n" + submission.permalink + "\n\n" + submission.selftext
@ -50,7 +55,7 @@ for submission in reddit.subreddit(str(getenv("REDDIT_SUBREDDIT"))).top(time_fil
url = url.split("?")[0] url = url.split("?")[0]
filename = wget.download(url) filename = wget.download(url)
print() print()
# 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*1000000:
media = mastodon.media_post(filename) media = mastodon.media_post(filename)
@ -68,4 +73,3 @@ for submission in reddit.subreddit(str(getenv("REDDIT_SUBREDDIT"))).top(time_fil
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