From f86e7f0a02aa4bb2e4bd4819388fc5711805948b Mon Sep 17 00:00:00 2001 From: lluni Date: Tue, 1 Aug 2023 18:59:46 +0200 Subject: [PATCH] Add some comments --- masto.py | 12 ++++++++---- reddit.py | 6 ++++-- script.py | 14 +++++++++----- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/masto.py b/masto.py index eef742c..552f6dc 100644 --- a/masto.py +++ b/masto.py @@ -4,17 +4,20 @@ from dotenv import load_dotenv from mastodon import Mastodon from os import getenv -# load .env +# ----- load .env ----- # + load_dotenv() -# register application +# ----- register application ----- # + Mastodon.create_app( getenv("MASTODON_APP_NAME"), api_base_url = str(getenv("MASTODON_URL")), to_file = getenv("MASTODON_CLIENT_SECRET") ) -# login +# ----- login Mastodon account ----- # + mastodon = Mastodon( client_id = getenv("MASTODON_CLIENT_SECRET"), api_base_url = getenv("MASTODON_URL") @@ -25,5 +28,6 @@ mastodon.log_in( to_file = getenv("MASTODON_USER_SECRET") ) -# test toot +# ----- post test toot ----- # + mastodon.toot("Hello World") diff --git a/reddit.py b/reddit.py index ab43ca2..43a8d61 100644 --- a/reddit.py +++ b/reddit.py @@ -4,10 +4,12 @@ import praw from dotenv import load_dotenv from os import getenv -# load .env +# ----- load .env ----- # + load_dotenv() -# initialize reddit +# ----- initialize reddit ----- # + reddit = praw.Reddit( client_id = getenv("REDDIT_CLIENT_ID"), client_secret = getenv("REDDIT_CLIENT_SECRET"), diff --git a/script.py b/script.py index 450b6a2..6e054d4 100644 --- a/script.py +++ b/script.py @@ -6,9 +6,12 @@ from mastodon import Mastodon from os import getenv, remove from os.path import getsize -# load .env +# ----- load .env ----- # + load_dotenv() +# ----- initialization ----- # + # initialize reddit reddit = praw.Reddit( client_id = getenv("REDDIT_CLIENT_ID"), @@ -23,6 +26,8 @@ mastodon = Mastodon( api_base_url = getenv("MASTODON_URL") ) +# ----- main script ----- # + # get recent posts to check for duplicates 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 if (submission.stickied): continue - + # check if the post has been posted before already_posted = False for posted_submission in last_posts: @@ -39,7 +44,7 @@ for submission in reddit.subreddit(str(getenv("REDDIT_SUBREDDIT"))).top(time_fil break if already_posted: continue - + # check if text only if submission.is_self: 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] filename = wget.download(url) print() - + # check if video file is small enough for the server (10MB) if getsize(filename) < 10*1000000: 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") remove(filename) break - \ No newline at end of file