more pep8 compliant, added a server class
parent
063947992a
commit
2c4bbec58e
|
@ -18,94 +18,108 @@ from pprint import pprint
|
||||||
# import toota-palooza
|
# import toota-palooza
|
||||||
|
|
||||||
|
|
||||||
def mastodonInit():
|
class Server(Mastodon):
|
||||||
"""Connect to the Mastodon instance based on .env files"""
|
dotenv_loaded = False
|
||||||
|
|
||||||
server = Mastodon(
|
def __init__(self):
|
||||||
client_id = '.toota-palooza.env',
|
self.maybe_load_dotenv()
|
||||||
api_base_url = os.getenv('MD_HOST')
|
|
||||||
)
|
|
||||||
|
|
||||||
load_dotenv()
|
self.client_id = '.toota-palooza.env'
|
||||||
server.log_in(
|
self.hostname = os.getenv('MD_HOST')
|
||||||
os.getenv('MD_USER'),
|
self.username = os.getenv('MD_USER')
|
||||||
os.getenv('MD_PASS'),
|
self.passwd = os.getenv('MD_PASS')
|
||||||
to_file = '.toota-palooza-usercred.env'
|
self.to_file = '.toota-palooza-usercred.env'
|
||||||
)
|
|
||||||
return(server)
|
super().__init__(client_id=self.client_id, api_base_url=self.hostname)
|
||||||
|
|
||||||
|
self.log_in(
|
||||||
|
self.username,
|
||||||
|
self.passwd,
|
||||||
|
to_file=self.to_file
|
||||||
|
)
|
||||||
|
|
||||||
|
def post(self, message):
|
||||||
|
self.status_post(message, visibility='public')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def maybe_load_dotenv(cls):
|
||||||
|
if not cls.dotenv_loaded:
|
||||||
|
load_dotenv()
|
||||||
|
cls.dotenv_loaded = True
|
||||||
|
|
||||||
|
|
||||||
def checkPublicTimeline( server):
|
def check_public_timeline(server):
|
||||||
"""Do one run. Connect to the database, connect to the server, get the public timeline.
|
"""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."""
|
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
|
# Here's the idea: pick a chunk_size. 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.
|
# 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.
|
# as soon as we get less than we asked for, quit.
|
||||||
#
|
#
|
||||||
# XXX Not sure about rate-limiting
|
# XXX Not sure about rate-limiting
|
||||||
#
|
#
|
||||||
chunkSize = 20
|
chunk_size = 20
|
||||||
maxPosts = 1000
|
max_posts = 1000
|
||||||
timelineList = []
|
timeline_list = []
|
||||||
useridList = {}
|
userid_list = {}
|
||||||
total = 0
|
total = 0
|
||||||
calls = 0
|
calls = 0
|
||||||
while( total < maxPosts ):
|
domain_def = server.hostname.split('/')[2]
|
||||||
timelineList = server.timeline(timeline='public', since_id=latestId, limit=chunkSize)
|
while total < max_posts:
|
||||||
calls = calls + 1
|
timeline_list = server.timeline(timeline='public', limit=chunk_size)
|
||||||
for post in timelineList:
|
calls += 1
|
||||||
|
for post in timeline_list:
|
||||||
userid = post.account.acct
|
userid = post.account.acct
|
||||||
username=userid.split('@')[0]
|
name_and_domain = userid.split('@', 1)
|
||||||
|
username = name_and_domain[0]
|
||||||
|
|
||||||
try:
|
if len(name_and_domain) == 2:
|
||||||
domain=userid.split('@')[1]
|
domain = name_and_domain[1]
|
||||||
except IndexError:
|
else:
|
||||||
# if there is no domain, then it's a local account
|
# if there is no domain, then it's a local account
|
||||||
domain=os.getenv('MD_HOST').split('/')[2]
|
domain = domain_def
|
||||||
useridList[userid] = (username,domain,post.account.display_name,post.account.bot,post.url)
|
userid_list[userid] = (username, domain,
|
||||||
latestId=post.id
|
post.account.display_name, post.account.bot, post.url)
|
||||||
|
|
||||||
if( len(timelineList) < 1):
|
if len(timeline_list) == 0:
|
||||||
# We got fewer than we asked for. Drop out of the loop.
|
# We got fewer than we asked for. Drop out of the loop.
|
||||||
total = total + len(timelineList)
|
|
||||||
break
|
break
|
||||||
else:
|
# record how many we did, and go again.
|
||||||
# record how many we did, and go again.
|
total += len(timeline_list)
|
||||||
total = total + len(timelineList)
|
timeline_list.clear()
|
||||||
timelineList = []
|
|
||||||
|
|
||||||
# Ok, we got them all, time to insert
|
# Ok, we got them all, time to insert
|
||||||
server.status_post( "{} calls to get {} posts, {} users processed".format(calls, total, len(useridList)),
|
return (f'{calls} calls to get {total} posts,'
|
||||||
visibility='public' )
|
+f' {len(userid_list)} users processed')
|
||||||
|
|
||||||
def daemon_main():
|
def daemon_main():
|
||||||
"""Run from a command line."""
|
"""Run from a command line."""
|
||||||
server = mastodonInit()
|
|
||||||
|
|
||||||
while(True):
|
while True:
|
||||||
# do a thing
|
# do a thing
|
||||||
time.sleep(600)
|
time.sleep(600)
|
||||||
|
|
||||||
def once():
|
def once(server):
|
||||||
"""Run from a command line."""
|
"""Run from a command line."""
|
||||||
server = mastodonInit()
|
message = check_public_timeline(server)
|
||||||
|
server.post(message)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description='Check for suspicious impersonators.'
|
description='Check for suspicious impersonators.')
|
||||||
)
|
|
||||||
parser.add_argument( '-d', '--debug', action='store_true',
|
parser.add_argument( '-d', '--debug', action='store_true',
|
||||||
help='Enable debugging messages.')
|
help='Enable debugging messages.')
|
||||||
parser.add_argument( '-o', '--once', action='store_true',
|
parser.add_argument( '-o', '--once', action='store_true',
|
||||||
help='Run once and exit. Default is to run as a daemon.')
|
help='Run once and exit. Default is to run as a daemon.')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if( args.once ):
|
server = Server()
|
||||||
exit(once())
|
|
||||||
else:
|
if args.once:
|
||||||
daemon_main()
|
return once(server)
|
||||||
|
|
||||||
|
daemon_main(server)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
exit(once())
|
sys.exit(main())
|
Loading…
Reference in New Issue