from mastodon import Mastodon from loguru import logger import random # read all toots from file with open('toots.txt', 'r') as f: toots = f.read().split('\n\n') access_token = 'your access token' instance = 'https://mastodon.social' mastodon_cli = Mastodon( access_token=access_token, api_base_url=instance) #randomly choose a toot toot = random.choice(toots) # post toot try: mastodon_cli.toot(toot) logger.info('Toot posted, %s' % toot) # remove toot from file with open('toots.txt', 'w') as f: f.write('\n\n'.join(toots)) # record toot with open('toots_sent.txt', 'a') as f: f.write('\n\n' + toot) except Exception as e: logger.error(e)