Created
December 19, 2024 08:52
-
-
Save intellectronica/1092ca5e986eb55f916f3cc3659bec54 to your computer and use it in GitHub Desktop.
Boost all posts from a Fediverse account you follow (useful for boosting a bridged account's posts, for example)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /// script | |
# requires-python = ">=3.13" | |
# dependencies = [ | |
# "Mastodon.py", | |
# ] | |
# /// | |
import os | |
from mastodon import Mastodon | |
from datetime import datetime, timedelta | |
MASTODON_CLIENT_ID = '...' | |
MASTODON_CLIENT_SECRET = '...' | |
MASTODON_ACCESS_TOKEN = '...' | |
MASTODON_API_BASE_URL = 'https://mastodon.social' | |
TARGET_USER_NAME = '[email protected]' | |
LOOKBACK_DAYS = 3 | |
def get_mastodon_api(): | |
""" | |
Initializes and returns a Mastodon API instance. | |
""" | |
mastodon = Mastodon( | |
client_id=MASTODON_CLIENT_ID, | |
client_secret=MASTODON_CLIENT_SECRET, | |
access_token=MASTODON_ACCESS_TOKEN, | |
api_base_url=MASTODON_API_BASE_URL | |
) | |
return mastodon | |
def get_user_id(mastodon, username): | |
""" | |
Retrieves the user ID for a given username. | |
""" | |
accounts = mastodon.account_search(username) | |
if not accounts: | |
raise ValueError(f"User '{username}' not found.") | |
return accounts[0]['id'] | |
def get_posts_to_boost(mastodon, user_id, lookback_days): | |
""" | |
Fetches recent posts from the specified user and filters out posts that have already been boosted. | |
""" | |
posts_to_boost = [] | |
now = datetime.now(tz=datetime.now().astimezone().tzinfo) | |
threshold = now - timedelta(days=lookback_days) | |
statuses = mastodon.account_statuses(user_id, limit=40) # Fetch up to 40 posts (API limit) | |
while statuses: | |
for post in statuses: | |
if post['created_at'] >= threshold: | |
if not post['reblogged']: # Check if the post has been boosted | |
posts_to_boost.append(post) | |
else: | |
return posts_to_boost # Stop if we've reached posts older than the threshold | |
statuses = mastodon.fetch_next(statuses) # Fetch next page if available | |
return posts_to_boost | |
def boost_posts(mastodon, posts): | |
""" | |
Boosts (retoots) the given list of posts. | |
""" | |
for post in posts: | |
try: | |
mastodon.status_reblog(post['id']) | |
print(f"Boosted post: {post['url']}") | |
except Exception as e: | |
print(f"Error boosting post {post['id']}: {e}") | |
mastodon = get_mastodon_api() | |
target_user_id = get_user_id(mastodon, TARGET_USER_NAME) | |
posts = get_posts_to_boost(mastodon, target_user_id, LOOKBACK_DAYS) | |
if posts: | |
print(f"Found {len(posts)} posts to boost.") | |
boost_posts(mastodon, posts) | |
else: | |
print("No posts to boost found.") | |
# To run: | |
# > uv run boost-mastodon-posts.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment