From 6d0ecd9c1d9c2aea7d2cb3530d93e41a2596a6f9 Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Sun, 27 Nov 2022 12:02:41 -0500 Subject: [PATCH] Built out interaction choices and stubbed out tagged toot method --- tootapalooza/cli.py | 76 +++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/tootapalooza/cli.py b/tootapalooza/cli.py index 8aa2c9f..bb041d0 100644 --- a/tootapalooza/cli.py +++ b/tootapalooza/cli.py @@ -62,36 +62,64 @@ class Tooter(Mastodon): with item.open('r') as f: cls.files[f.name] = f.readlines() + def tagged_public_toot(self): + message = self.new_message() + if( args.dry_run ): + print(f"tagged toot message: \"{message}\"") + else: + if( args.debug ): + print(f"{self.name} tagged toots \"{message}\"") + + self.toot(message) + return 0 + + def plain_public_toot(self): + message = self.new_message() + if( args.dry_run ): + print(f"toot message: \"{message}\"") + else: + if( args.debug ): + print(f"{self.name} toots \"{message}\"") + + self.toot(message) + return 0 + + @classmethod + def new_message(cls): + sourcefile = random.choice(list(cls.files.values())) + startline = random.randint(0,len(sourcefile)) + sourceline = '' + i=0 + # Starting at a random line, keep adding more lines to my + # toot until I get over 400 characters (max is 500 on most + # mastodon servers) + while(len(sourceline) < 400 and startline+i < len(sourcefile)): + sourceline=sourceline+sourcefile[startline+i] + i+=1 + + # Try to find a 400-odd character string that ends in a full stop + tootline = re.search( '((\s|\S){,400})\.', sourceline ) + if( tootline ): + message=tootline.group(0).strip() + else: + message=sourceline.strip() + + return(message) + + def random_interaction(self): + """Choose one possible interaction according to the weights, and do it.""" + interactions = [ self.plain_public_toot, self.tagged_public_toot ] + weights = [1, 1] + chosen = random.choices(population=interactions, weights=weights)[0] + chosen() + def daemon_main(tooter: Tooter): """Run from a command line.""" while True: # do a thing time.sleep(600) - -def once(tooter: Tooter): - """Run from a command line.""" - # message = check_public_timeline(tooter) - message = f'{tooter.name} says hi!' - sourcefile = random.choice(list(Tooter.files.values())) - startline = random.randint(0,len(sourcefile)) - sourceline = '' - i=0 - while(len(sourceline) < 400 and startline+i < len(sourcefile)): - sourceline=sourceline+sourcefile[startline+i] - i+=1 - tootline = re.search( '((\s|\S){,400})\.', sourceline ) - if( tootline ): - message=tootline.group(0).strip() - else: - message=sourceline.strip() - - if( args.dry_run ): - print(f"toot message: \"{message}\"") - else: - tooter.toot(message) - return 0 def main(): global args @@ -124,7 +152,7 @@ def main(): if args.once: for name in Tooter.credentials: t = Tooter(name) - once(t) + t.random_interaction() return 0 daemon_main(t)