diff --git a/default.toml b/default.toml index dd35dbf..ada19bc 100644 --- a/default.toml +++ b/default.toml @@ -10,22 +10,22 @@ mastodon_user = "botowner@example.com" [options] # Download videos from twitter and upload them on Mastodon -upload_videos = false +upload_videos = true # Also post the "reply-to" tweets from twitter account -post_reply_to = false +post_reply_to = true # Do not post the retweets of other twitter accounts -skip_retweets = false +skip_retweets = true # Clean up URLs in tweets to remove trackers -remove_trackers_from_urls = false +remove_trackers_from_urls = true # Maximum age of tweet to post (in days, decimal values accepted) -tweet_max_age = 1 +tweet_max_age = 2.0 # Minimum age of tweet before posting (in minutes) -tweet_delay = 15 +tweet_delay = 15.0 # Maximum number of toots to post in this run -twoot_cap = 1 +toot_cap = 2 diff --git a/twoot.py b/twoot.py old mode 100644 new mode 100755 index 1284dc4..47fe99d --- a/twoot.py +++ b/twoot.py @@ -313,30 +313,99 @@ def main(argv): # Build parser for command line arguments parser = argparse.ArgumentParser(description='toot tweets.') - parser.add_argument('-t', metavar='', action='store', required=True) - parser.add_argument('-i', metavar='', action='store', required=True) - parser.add_argument('-m', metavar='', action='store', required=True) - parser.add_argument('-p', metavar='', action='store', required=True) + parser.add_argument('-f', metavar='<.toml config file>', action='store') + parser.add_argument('-t', metavar='', action='store') + parser.add_argument('-i', metavar='', action='store') + parser.add_argument('-m', metavar='', action='store') + parser.add_argument('-p', metavar='', action='store') parser.add_argument('-r', action='store_true', help='Also post replies to other tweets') - parser.add_argument('-s', action='store_true', help='Suppress retweets') + parser.add_argument('-s', action='store_true', help='Skip retweets') + parser.add_argument('-u', action='store_true', help='Remove trackers from URLs') parser.add_argument('-v', action='store_true', help='Ingest twitter videos and upload to Mastodon instance') - parser.add_argument('-a', metavar='', action='store', type=float, default=1) - parser.add_argument('-d', metavar='', action='store', type=float, default=0) - parser.add_argument('-c', metavar='', action='store', type=int, default=0) + parser.add_argument('-a', metavar='', action='store', type=float) + parser.add_argument('-d', metavar='', action='store', type=float) + parser.add_argument('-c', metavar='', action='store', type=int) # Parse command line args = vars(parser.parse_args()) - twit_account = args['t'] - mast_instance = args['i'] - mast_account = args['m'] - mast_password = args['p'] - tweets_and_replies = args['r'] - suppress_retweets = args['s'] - get_vids = args['v'] - max_age = float(args['a']) - min_delay = float(args['d']) - cap = int(args['c']) + # We build the configuration by layering for each parameter: + # 1. A default value + # 2. The values read from the config file + # 3. The value provided on the command line + + # Default options + options = { + 'upload_videos': False, + 'post_reply_to': False, + 'skip_retweets': False, + 'remove_trackers_from_url': False, + 'tweet_max_age': float(1), + 'tweet_delay': float(0), + 'toot_cap': int(1), + } + + # Default empty toml + toml = {'config': {}, 'options': options} + + # Load config file if it was provided + toml_file = args['f'] + if toml_file is not None: + import tomli + try: + with open(toml_file, 'rb') as config_file: + toml = tomli.read(config_file) + except FileNotFoundError: + print('config file not found') + exit(-1) + except tomli.TOMLDecodeError: + print('Malformed config file') + exit(-1) + + # Override config file parameter values with command-line values if provided + if args['t'] is not None: + toml['config']['twitter_account'] = args['t'] + if args['i'] is not None: + toml['config']['mastodon_instance'] = args['i'] + if args['m'] is not None: + toml['config']['mastodon_user'] = args['m'] + if args['v'] is not None: + toml['options']['upload_videos'] = args['v'] + if args['r'] is not None: + toml['options']['post_reply_to'] = args['r'] + if args['s'] is not None: + toml['options']['skip_retweets'] = args['s'] + if args['u'] is not None: + toml['options']['remove_trackers_from_url'] = args['u'] + if args['a'] is not None: + toml['options']['tweet_max_age'] = float(args['a']) + if args['d'] is not None: + toml['options']['tweet_delay'] = float(args['d']) + if args['c'] is not None: + toml['options']['toot_cap'] = int(args['c']) + + # Verify that we have a minimum config to run + if 'twitter_account' not in toml['config'].keys(): + print('Missing Twitter account') + exit(-1) + if 'mastodon_instance' not in toml['config'].keys(): + print('Missing Mastodon instance') + exit(-1) + if 'mastodon_user' not in toml['config'].keys(): + print('Missing Mastodon user') + exit(-1) + + # twit_account = args['t'] + # mast_instance = args['i'] + # mast_account = args['m'] + # mast_password = args['p'] + # tweets_and_replies = args['r'] + # suppress_retweets = args['s'] + # get_vids = args['v'] + # remove_trackers = args['u'] + # max_age = float(args['a']) + # min_delay = float(args['d']) + # cap = int(args['c']) # Remove previous log file # try: @@ -346,22 +415,28 @@ def main(argv): # Setup logging to file logging.basicConfig( - filename=twit_account + '.log', + filename=toml['config']['twitter_account'] + '.log', level=LOGGING_LEVEL, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', ) - logging.info('Running with the following parameters:') - logging.info(' -t ' + twit_account) - logging.info(' -i ' + mast_instance) - logging.info(' -m ' + mast_account) - logging.info(' -r ' + str(tweets_and_replies)) - logging.info(' -s ' + str(suppress_retweets)) - logging.info(' -v ' + str(get_vids)) - logging.info(' -a ' + str(max_age)) - logging.info(' -d ' + str(min_delay)) - logging.info(' -c ' + str(cap)) + # logging.info('Running with the following parameters:') + # logging.info(' -f ' + str(toml_file)) + # logging.info(' -t ' + twit_account) + # logging.info(' -i ' + mast_instance) + # logging.info(' -m ' + mast_account) + # logging.info(' -r ' + str(tweets_and_replies)) + # logging.info(' -s ' + str(suppress_retweets)) + # logging.info(' -v ' + str(get_vids)) + # logging.info(' -a ' + str(max_age)) + # logging.info(' -d ' + str(min_delay)) + # logging.info(' -c ' + str(cap)) + + print(toml) + + # DEBUG CONFIG + exit(1) # Try to open database. If it does not exist, create it sql = sqlite3.connect('twoot.db')