converted all print() statements to logger

main
Paco Hope 2022-11-27 16:57:02 -05:00
parent acbe23b4be
commit 9e8925e889
1 changed files with 43 additions and 55 deletions

View File

@ -17,8 +17,10 @@ import sys
from pathlib import Path from pathlib import Path
import random import random
import re import re
import logging
args=None args=None
logger=None
class Tooter(Mastodon): class Tooter(Mastodon):
credentials: dict = {} credentials: dict = {}
@ -104,8 +106,7 @@ class Tooter(Mastodon):
def toot_tagged_public(self) -> int: def toot_tagged_public(self) -> int:
"""Get a random message, pick a small number of words in it to """Get a random message, pick a small number of words in it to
hashtag. Then toot it publicly.""" hashtag. Then toot it publicly."""
if( args.debug ): logger.debug(f"{self.acct} toot_tagged_public")
print(f"{self.acct} toot_tagged_public")
rawmessage = self.new_message().split() rawmessage = self.new_message().split()
numtaggable = sum(1 for word in rawmessage if len(word) > 3) numtaggable = sum(1 for word in rawmessage if len(word) > 3)
# Pick a small number of words to tag, but not greater than the # Pick a small number of words to tag, but not greater than the
@ -121,24 +122,21 @@ class Tooter(Mastodon):
message = ' '.join(rawmessage) message = ' '.join(rawmessage)
if( args.dry_run ): if( args.dry_run ):
print(f"tagged toot message: \"{message}\"") logger.info(f"tagged toot message: \"{message}\"")
else: else:
if( args.debug ): logger.debug(f"{self.acct} tagged toots \"{message}\"")
print(f"{self.acct} tagged toots \"{message}\"")
self.toot(message) self.toot(message)
return 0 return 0
def toot_plain_public(self) -> int: def toot_plain_public(self) -> int:
"""Toot a random message.""" """Toot a random message."""
if( args.debug ): logger.debug(f"{self.acct} toot_plain_public")
print(f"{self.acct} toot_plain_public")
message = self.new_message() message = self.new_message()
if( args.dry_run ): if( args.dry_run ):
print(f"toot message: \"{message}\"") logger.info(f"toot message: \"{message}\"")
else: else:
if( args.debug ): logger.debug(f"{self.acct} toots \"{message}\"")
print(f"{self.acct} toots \"{message}\"")
self.toot(message) self.toot(message)
return 0 return 0
@ -147,28 +145,24 @@ class Tooter(Mastodon):
"""Toot a random message unlisted.""" """Toot a random message unlisted."""
message = self.new_message() message = self.new_message()
if( args.dry_run ): if( args.dry_run ):
print(f"toot unlisted message: \"{message}\"") logger.info(f"toot unlisted message: \"{message}\"")
else: else:
if( args.debug ): logger.debug(f"{self.acct} toots unlisted \"{message}\"")
print(f"{self.acct} toots unlisted \"{message}\"")
self.status_post( message, visibility='private' ) self.status_post( message, visibility='private' )
return( 0 ) return( 0 )
def reply_random_local(self) -> int: def reply_random_local(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} reply_random_local")
print(f"{self.acct} reply_random_local")
return(self._reply_random('local')) return(self._reply_random('local'))
def reply_random_home(self) -> int: def reply_random_home(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} reply_random_home")
print(f"{self.acct} reply_random_home")
return(self._reply_random('home')) return(self._reply_random('home'))
def reply_random_public(self) -> int: def reply_random_public(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} reply_random_public")
print(f"{self.acct} reply_random_public")
return(self._reply_random('public')) return(self._reply_random('public'))
@ -187,15 +181,13 @@ class Tooter(Mastodon):
message = self.new_message() message = self.new_message()
if( args.dry_run ): 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: else:
if( args.debug ): logger.debug(f"{self.acct} replied to {reply_post.account.acct} {reply_post.uri}")
print(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' ) self.status_post( message, in_reply_to_id=reply_post.id, visibility='public' )
def follow_random_local(self) -> int: def follow_random_local(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} follow_random_local")
print(f"{self.acct} follow_random_local")
followlist = self.account_following(id=self.id) followlist = self.account_following(id=self.id)
followed_people = {account.acct for account in followlist} followed_people = {account.acct for account in followlist}
@ -206,46 +198,40 @@ class Tooter(Mastodon):
follow_target = random.choice(list(potentials)) follow_target = random.choice(list(potentials))
target_dict = self.account_lookup(follow_target) target_dict = self.account_lookup(follow_target)
if( args.dry_run ): 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: else:
self.account_follow(target_dict.id) self.account_follow(target_dict.id)
return( 0 ) return( 0 )
def unfollow_random(self) -> int: def unfollow_random(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} unfollow_random")
print(f"{self.acct} unfollow_random")
followlist = self.account_following(id=self.id) followlist = self.account_following(id=self.id)
unfollowee = random.choice(followlist) unfollowee = random.choice(followlist)
if( len(followlist) == 0 ): if( len(followlist) == 0 ):
if( args.debug ): logger.debug(f"{self.acct} can't unfollow! Not following anyone.")
print(f"{self.acct} can't unfollow! Not following anyone.")
return( 0 ) return( 0 )
if( args.dry_run ): 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: else:
self.account_unfollow(unfollowee.id) self.account_unfollow(unfollowee.id)
return( 0 ) return( 0 )
def boost_random_local(self) -> int: def boost_random_local(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} boost_random_local")
print(f"{self.acct} boost_random_local")
return( 0 ) return( 0 )
def favourite_random_local(self) -> int: def favourite_random_local(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} favourite_random_local")
print(f"{self.acct} favourite_random_local")
return( self._favourite( timeline='local') ) return( self._favourite( timeline='local') )
def favourite_random_public(self) -> int: def favourite_random_public(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} favourite_random_public")
print(f"{self.acct} favourite_random_public")
return( self._favourite( timeline='public') ) return( self._favourite( timeline='public') )
def favourite_random_home(self) -> int: def favourite_random_home(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} favourite_random_home")
print(f"{self.acct} favourite_random_home")
return( self._favourite( timeline='home') ) return( self._favourite( timeline='home') )
def _favourite(self, timeline: str) -> int: def _favourite(self, timeline: str) -> int:
@ -256,27 +242,23 @@ class Tooter(Mastodon):
fav_post = random.choice(timeline_list) fav_post = random.choice(timeline_list)
if( args.dry_run): if( args.dry_run):
print(f"{self.acct} favourites {fav_post.id}") logger.info(f"{self.acct} favourites {fav_post.id}")
else: else:
if( args.debug ): logger.debug(f"{self.acct} favourites {fav_post.id}")
print(f"{self.acct} favourites {fav_post.id}")
self.status_favourite(id=fav_post.id) self.status_favourite(id=fav_post.id)
return( 0 ) return( 0 )
def boost_random_local(self) -> int: def boost_random_local(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} boost_random_local")
print(f"{self.acct} boost_random_local")
return( self._boost( timeline='local') ) return( self._boost( timeline='local') )
def boost_random_public(self) -> int: def boost_random_public(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} boost_random_public")
print(f"{self.acct} boost_random_public")
return( self._boost( timeline='public') ) return( self._boost( timeline='public') )
def boost_random_home(self) -> int: def boost_random_home(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} boost_random_home")
print(f"{self.acct} boost_random_home")
return( self._boost( timeline='home') ) return( self._boost( timeline='home') )
def _boost(self, timeline: str) -> int: def _boost(self, timeline: str) -> int:
@ -287,17 +269,15 @@ class Tooter(Mastodon):
fav_post = random.choice(timeline_list) fav_post = random.choice(timeline_list)
if( args.dry_run): if( args.dry_run):
print(f"{self.acct} boosts {fav_post.id}") logger.info(f"{self.acct} boosts {fav_post.id}")
else: else:
if( args.debug ): logger.debug(f"{self.acct} boosts {fav_post.id}")
print(f"{self.acct} boosts {fav_post.id}")
self.status_reblog(id=fav_post.id) self.status_reblog(id=fav_post.id)
return( 0 ) return( 0 )
def report_random_local(self) -> int: def report_random_local(self) -> int:
if( args.debug ): logger.debug(f"{self.acct} report_random_local")
print(f"{self.acct} report_random_local")
return( 0 ) return( 0 )
def random_interaction(self): def random_interaction(self):
@ -330,9 +310,9 @@ def daemon_main(tooter: Tooter):
# do a thing # do a thing
time.sleep(600) time.sleep(600)
def main(): def main():
global args global args
global logger
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Randomly interact with a Mastodon timeline.') description='Randomly interact with a Mastodon timeline.')
parser.add_argument( '-d', '--debug', action='store_true', 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).') help='TOML file with user credentials (see server-util/README.md).')
args = parser.parse_args() 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) p = Path(args.directory)
if not p.exists(): 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 return 2
if not p.is_dir(): 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 return 2
Tooter.load_src_files(p) Tooter.load_src_files(p)