''' Command line module for calling toota-palooza to do its work ''' __all__ = ['toota-palooza'] __author__ = 'Paco Hope ' __date__ = '25 November 2022' __version__ = '1.0' __copyright__ = 'Copyright © 2022 Paco Hope. See LICENSE for details.' from mastodon import Mastodon from dotenv import load_dotenv import os import time import argparse import sys from pprint import pprint # import toota-palooza def mastodonInit(): """Connect to the Mastodon instance based on .env files""" server = Mastodon( client_id = '.toota-palooza.env', api_base_url = os.getenv('MD_HOST') ) load_dotenv() server.log_in( os.getenv('MD_USER'), os.getenv('MD_PASS'), to_file = '.toota-palooza-usercred.env' ) return(server) def checkPublicTimeline( server): """Do one run. Connect to the database, connect to the server, get the public timeline. Look at users and check to see if any are potential impersonators. Then exit.""" # Here's the idea: pick a chunkSize. Ask the server for that many toots off the public # timeline. As long as the server gives us as many as we asked for, keep trying. # as soon as we get less than we asked for, quit. # # XXX Not sure about rate-limiting # chunkSize = 20 maxPosts = 1000 timelineList = [] useridList = {} total = 0 calls = 0 while( total < maxPosts ): timelineList = server.timeline(timeline='public', since_id=latestId, limit=chunkSize) calls = calls + 1 for post in timelineList: userid = post.account.acct username=userid.split('@')[0] try: domain=userid.split('@')[1] except IndexError: # if there is no domain, then it's a local account domain=os.getenv('MD_HOST').split('/')[2] useridList[userid] = (username,domain,post.account.display_name,post.account.bot,post.url) latestId=post.id if( len(timelineList) < 1): # We got fewer than we asked for. Drop out of the loop. total = total + len(timelineList) break else: # record how many we did, and go again. total = total + len(timelineList) timelineList = [] # Ok, we got them all, time to insert server.status_post( "{} calls to get {} posts, {} users processed".format(calls, total, len(useridList)), visibility='public' ) def daemon_main(): """Run from a command line.""" server = mastodonInit() while(True): # do a thing time.sleep(600) def once(): """Run from a command line.""" server = mastodonInit() return 0 def main(): parser = argparse.ArgumentParser( description='Check for suspicious impersonators.' ) parser.add_argument( '-d', '--debug', action='store_true', help='Enable debugging messages.') parser.add_argument( '-o', '--once', action='store_true', help='Run once and exit. Default is to run as a daemon.') args = parser.parse_args() if( args.once ): exit(once()) else: daemon_main() if __name__ == '__main__': exit(once())