Struggling with config

This commit is contained in:
jeancf 2022-11-17 23:23:11 +01:00
parent 6bc4e04c7d
commit d336711514
2 changed files with 111 additions and 36 deletions

View File

@ -10,22 +10,22 @@ mastodon_user = "botowner@example.com"
[options] [options]
# Download videos from twitter and upload them on Mastodon # Download videos from twitter and upload them on Mastodon
upload_videos = false upload_videos = true
# Also post the "reply-to" tweets from twitter account # 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 # Do not post the retweets of other twitter accounts
skip_retweets = false skip_retweets = true
# Clean up URLs in tweets to remove trackers # 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) # 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) # Minimum age of tweet before posting (in minutes)
tweet_delay = 15 tweet_delay = 15.0
# Maximum number of toots to post in this run # Maximum number of toots to post in this run
twoot_cap = 1 toot_cap = 2

133
twoot.py Normal file → Executable file
View File

@ -313,30 +313,99 @@ def main(argv):
# Build parser for command line arguments # Build parser for command line arguments
parser = argparse.ArgumentParser(description='toot tweets.') parser = argparse.ArgumentParser(description='toot tweets.')
parser.add_argument('-t', metavar='<twitter account>', action='store', required=True) parser.add_argument('-f', metavar='<.toml config file>', action='store')
parser.add_argument('-i', metavar='<mastodon instance>', action='store', required=True) parser.add_argument('-t', metavar='<twitter account>', action='store')
parser.add_argument('-m', metavar='<mastodon account>', action='store', required=True) parser.add_argument('-i', metavar='<mastodon instance>', action='store')
parser.add_argument('-p', metavar='<mastodon password>', action='store', required=True) parser.add_argument('-m', metavar='<mastodon account>', action='store')
parser.add_argument('-p', metavar='<mastodon password>', action='store')
parser.add_argument('-r', action='store_true', help='Also post replies to other tweets') 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('-v', action='store_true', help='Ingest twitter videos and upload to Mastodon instance')
parser.add_argument('-a', metavar='<max age (in days)>', action='store', type=float, default=1) parser.add_argument('-a', metavar='<max age (in days)>', action='store', type=float)
parser.add_argument('-d', metavar='<min delay (in mins)>', action='store', type=float, default=0) parser.add_argument('-d', metavar='<min delay (in mins)>', action='store', type=float)
parser.add_argument('-c', metavar='<max # of toots to post>', action='store', type=int, default=0) parser.add_argument('-c', metavar='<max # of toots to post>', action='store', type=int)
# Parse command line # Parse command line
args = vars(parser.parse_args()) args = vars(parser.parse_args())
twit_account = args['t'] # We build the configuration by layering for each parameter:
mast_instance = args['i'] # 1. A default value
mast_account = args['m'] # 2. The values read from the config file
mast_password = args['p'] # 3. The value provided on the command line
tweets_and_replies = args['r']
suppress_retweets = args['s'] # Default options
get_vids = args['v'] options = {
max_age = float(args['a']) 'upload_videos': False,
min_delay = float(args['d']) 'post_reply_to': False,
cap = int(args['c']) '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 # Remove previous log file
# try: # try:
@ -346,22 +415,28 @@ def main(argv):
# Setup logging to file # Setup logging to file
logging.basicConfig( logging.basicConfig(
filename=twit_account + '.log', filename=toml['config']['twitter_account'] + '.log',
level=LOGGING_LEVEL, level=LOGGING_LEVEL,
format='%(asctime)s %(levelname)-8s %(message)s', format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S', datefmt='%Y-%m-%d %H:%M:%S',
) )
logging.info('Running with the following parameters:') # logging.info('Running with the following parameters:')
logging.info(' -t ' + twit_account) # logging.info(' -f ' + str(toml_file))
logging.info(' -i ' + mast_instance) # logging.info(' -t ' + twit_account)
logging.info(' -m ' + mast_account) # logging.info(' -i ' + mast_instance)
logging.info(' -r ' + str(tweets_and_replies)) # logging.info(' -m ' + mast_account)
logging.info(' -s ' + str(suppress_retweets)) # logging.info(' -r ' + str(tweets_and_replies))
logging.info(' -v ' + str(get_vids)) # logging.info(' -s ' + str(suppress_retweets))
logging.info(' -a ' + str(max_age)) # logging.info(' -v ' + str(get_vids))
logging.info(' -d ' + str(min_delay)) # logging.info(' -a ' + str(max_age))
logging.info(' -c ' + str(cap)) # 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 # Try to open database. If it does not exist, create it
sql = sqlite3.connect('twoot.db') sql = sqlite3.connect('twoot.db')