From 9e8925e8898a67bbdfdabbc7e9a762d65c3fc4b0 Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Sun, 27 Nov 2022 16:57:02 -0500 Subject: [PATCH] converted all print() statements to logger --- tootapalooza/cli.py | 98 ++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 55 deletions(-) diff --git a/tootapalooza/cli.py b/tootapalooza/cli.py index 392f7f1..97951be 100644 --- a/tootapalooza/cli.py +++ b/tootapalooza/cli.py @@ -17,8 +17,10 @@ import sys from pathlib import Path import random import re +import logging args=None +logger=None class Tooter(Mastodon): credentials: dict = {} @@ -104,8 +106,7 @@ class Tooter(Mastodon): def toot_tagged_public(self) -> int: """Get a random message, pick a small number of words in it to hashtag. Then toot it publicly.""" - if( args.debug ): - print(f"{self.acct} toot_tagged_public") + logger.debug(f"{self.acct} toot_tagged_public") rawmessage = self.new_message().split() numtaggable = sum(1 for word in rawmessage if len(word) > 3) # Pick a small number of words to tag, but not greater than the @@ -121,24 +122,21 @@ class Tooter(Mastodon): message = ' '.join(rawmessage) if( args.dry_run ): - print(f"tagged toot message: \"{message}\"") + logger.info(f"tagged toot message: \"{message}\"") else: - if( args.debug ): - print(f"{self.acct} tagged toots \"{message}\"") + logger.debug(f"{self.acct} tagged toots \"{message}\"") self.toot(message) return 0 def toot_plain_public(self) -> int: """Toot a random message.""" - if( args.debug ): - print(f"{self.acct} toot_plain_public") + logger.debug(f"{self.acct} toot_plain_public") message = self.new_message() if( args.dry_run ): - print(f"toot message: \"{message}\"") + logger.info(f"toot message: \"{message}\"") else: - if( args.debug ): - print(f"{self.acct} toots \"{message}\"") + logger.debug(f"{self.acct} toots \"{message}\"") self.toot(message) return 0 @@ -147,28 +145,24 @@ class Tooter(Mastodon): """Toot a random message unlisted.""" message = self.new_message() if( args.dry_run ): - print(f"toot unlisted message: \"{message}\"") + logger.info(f"toot unlisted message: \"{message}\"") else: - if( args.debug ): - print(f"{self.acct} toots unlisted \"{message}\"") + logger.debug(f"{self.acct} toots unlisted \"{message}\"") self.status_post( message, visibility='private' ) return( 0 ) def reply_random_local(self) -> int: - if( args.debug ): - print(f"{self.acct} reply_random_local") + logger.debug(f"{self.acct} reply_random_local") return(self._reply_random('local')) def reply_random_home(self) -> int: - if( args.debug ): - print(f"{self.acct} reply_random_home") + logger.debug(f"{self.acct} reply_random_home") return(self._reply_random('home')) def reply_random_public(self) -> int: - if( args.debug ): - print(f"{self.acct} reply_random_public") + logger.debug(f"{self.acct} reply_random_public") return(self._reply_random('public')) @@ -187,15 +181,13 @@ class Tooter(Mastodon): message = self.new_message() if( args.dry_run ): - print(f"{self.acct} replied to {timeline} {reply_post.id}") + logger.info(f"{self.acct} replied to {timeline} {reply_post.id}") else: - if( args.debug ): - print(f"{self.acct} replied to {reply_post.account.acct} {reply_post.uri}") + logger.debug(f"{self.acct} replied to {reply_post.account.acct} {reply_post.uri}") self.status_post( message, in_reply_to_id=reply_post.id, visibility='public' ) def follow_random_local(self) -> int: - if( args.debug ): - print(f"{self.acct} follow_random_local") + logger.debug(f"{self.acct} follow_random_local") followlist = self.account_following(id=self.id) followed_people = {account.acct for account in followlist} @@ -206,46 +198,40 @@ class Tooter(Mastodon): follow_target = random.choice(list(potentials)) target_dict = self.account_lookup(follow_target) if( args.dry_run ): - print(f"{self.acct} will follow {follow_target} (id: {target_dict.id})") + logger.info(f"{self.acct} will follow {follow_target} (id: {target_dict.id})") else: self.account_follow(target_dict.id) return( 0 ) def unfollow_random(self) -> int: - if( args.debug ): - print(f"{self.acct} unfollow_random") + logger.debug(f"{self.acct} unfollow_random") followlist = self.account_following(id=self.id) unfollowee = random.choice(followlist) if( len(followlist) == 0 ): - if( args.debug ): - print(f"{self.acct} can't unfollow! Not following anyone.") + logger.debug(f"{self.acct} can't unfollow! Not following anyone.") return( 0 ) if( args.dry_run ): - print(f"{self.acct} unfollows {unfollowee.acct} (id: {unfollowee.id})") + logger.info(f"{self.acct} unfollows {unfollowee.acct} (id: {unfollowee.id})") else: self.account_unfollow(unfollowee.id) return( 0 ) def boost_random_local(self) -> int: - if( args.debug ): - print(f"{self.acct} boost_random_local") + logger.debug(f"{self.acct} boost_random_local") return( 0 ) def favourite_random_local(self) -> int: - if( args.debug ): - print(f"{self.acct} favourite_random_local") + logger.debug(f"{self.acct} favourite_random_local") return( self._favourite( timeline='local') ) def favourite_random_public(self) -> int: - if( args.debug ): - print(f"{self.acct} favourite_random_public") + logger.debug(f"{self.acct} favourite_random_public") return( self._favourite( timeline='public') ) def favourite_random_home(self) -> int: - if( args.debug ): - print(f"{self.acct} favourite_random_home") + logger.debug(f"{self.acct} favourite_random_home") return( self._favourite( timeline='home') ) def _favourite(self, timeline: str) -> int: @@ -256,27 +242,23 @@ class Tooter(Mastodon): fav_post = random.choice(timeline_list) if( args.dry_run): - print(f"{self.acct} favourites {fav_post.id}") + logger.info(f"{self.acct} favourites {fav_post.id}") else: - if( args.debug ): - print(f"{self.acct} favourites {fav_post.id}") + logger.debug(f"{self.acct} favourites {fav_post.id}") self.status_favourite(id=fav_post.id) return( 0 ) def boost_random_local(self) -> int: - if( args.debug ): - print(f"{self.acct} boost_random_local") + logger.debug(f"{self.acct} boost_random_local") return( self._boost( timeline='local') ) def boost_random_public(self) -> int: - if( args.debug ): - print(f"{self.acct} boost_random_public") + logger.debug(f"{self.acct} boost_random_public") return( self._boost( timeline='public') ) def boost_random_home(self) -> int: - if( args.debug ): - print(f"{self.acct} boost_random_home") + logger.debug(f"{self.acct} boost_random_home") return( self._boost( timeline='home') ) def _boost(self, timeline: str) -> int: @@ -287,17 +269,15 @@ class Tooter(Mastodon): fav_post = random.choice(timeline_list) if( args.dry_run): - print(f"{self.acct} boosts {fav_post.id}") + logger.info(f"{self.acct} boosts {fav_post.id}") else: - if( args.debug ): - print(f"{self.acct} boosts {fav_post.id}") + logger.debug(f"{self.acct} boosts {fav_post.id}") self.status_reblog(id=fav_post.id) return( 0 ) def report_random_local(self) -> int: - if( args.debug ): - print(f"{self.acct} report_random_local") + logger.debug(f"{self.acct} report_random_local") return( 0 ) def random_interaction(self): @@ -330,9 +310,9 @@ def daemon_main(tooter: Tooter): # do a thing time.sleep(600) - def main(): global args + global logger parser = argparse.ArgumentParser( description='Randomly interact with a Mastodon timeline.') parser.add_argument( '-d', '--debug', action='store_true', @@ -347,12 +327,20 @@ def main(): help='TOML file with user credentials (see server-util/README.md).') args = parser.parse_args() + logger = logging.getLogger(__name__) + logging.basicConfig(format='%(levelname)s\t%(message)s') + + if( args.debug ): + logger.setLevel(logging.DEBUG) + else: + logger.setLevel(logging.ERROR) + p = Path(args.directory) if not p.exists(): - print(f'{sys.argv[0]}: {args.directory}: No such file or directory', file=sys.stderr) + logger.error(f'{sys.argv[0]}: {args.directory}: No such file or directory') return 2 if not p.is_dir(): - print(f'{sys.argv[0]}: {args.directory}: Is not a directory', file=sys.stderr) + logger.error(f'{sys.argv[0]}: {args.directory}: Is not a directory') return 2 Tooter.load_src_files(p)