Mastodon's API is fairly simple to use, and as a result the function responsible for actually sending the status update is too def send_toot(en): Sending Toots via Mastodon's statuses API We could store the hash of every item we've seen, but it would quickly become unweildy. So, there's a trade-off here: the bot won't pick up on items which later get inserted earlier in the feed. that items won't be inserted down the feed). There's an implicit assumption here that the feed is an accurate representation of the order of publishing (i.e. We only ever store one hash per feed: that of the first item in the most recent run. # If that worked, write hash to disk to prevent re-sending # Keep a record of the hash for the first item in the feedĪnd then trigger sending of the toot, before updating the on-disk hash record # Send the toot If INCLUDE_AUTHOR = "True" and hasattr(entry, "author"):īefore we do too much though, we need to check whether we've seen this entry before, so we generate a SHA1 checksum of the URL and compare that to the hash loaded from disk linkhash = hashlib.sha1(('utf-8')).hexdigest()Īssuming we didn't break out of the loop, we also need to check whether firsthash needs updating (it should only contain the hash of the first listed RSS item). Once the feed has been parsed, we need to iterate over the entries and build a dict for each containing the information that we want to use in our toot (we could, technically, just pass the feedparser object through, but converting to a dict helps make subsequent functions more re-usable). The function's feed argument is just a dict (because the feed config is loaded from a JSON file - we'll come to that a bit later). # This will be overridden as we iterate through ''' Process the RSS feed and generate a toot for any entry we haven't yet seen We use a file on disk to track state between runs def process_feed(feed): We do, however, need to build a little bit of logic to keep track of which feed items we've seen so that subsequent runs of the script do not re-toot the same content. It'll transparently handle differences in feed structure and format (for example if an atom feed is used instead of RSS). Once you've hit SUBMIT and created the application, click back into it and Mastodon should provide you with some secrets - the one that you're interested in is labelled Your access token.įetching and parsing the RSS feed itself is a solved problem: we can use python's feedparser module. The only scope needed by this bot is write:statuses, so uncheck the others (and set a name). Log into your bot account, and then choose Preferences -> Development, and then click New Application to access the Application creation screen There are rate limits on things like toot deletion (30 deletions per 30 mins), so you'll also find that correcting mistakes made in prod can be problematic.īecause the bot is going to send toots, it needs to be able to authenticate with Mastodon. Note: If you can it's better to do development and testing against a non-federated development instance so that you don't annoy others. This should leave you with a bio that pretty clearly says "I'm a bot"
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |