|
|
|
@ -164,60 +164,62 @@ def welcome(config):
|
|
|
|
|
mastodon.public_stream(WelcomeBot(config)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
def handle_command_line(): |
|
|
|
|
'''Parse and act on the command line.''' |
|
|
|
|
# Global CLI arguments/options |
|
|
|
|
PARSER = argparse.ArgumentParser() |
|
|
|
|
PARSER.add_argument('--config', help='path to config file', required=True) |
|
|
|
|
SUBPARSERS = PARSER.add_subparsers(help='commands', dest='command') |
|
|
|
|
parser = argparse.ArgumentParser() |
|
|
|
|
parser.add_argument('--config', help='path to config file', required=True) |
|
|
|
|
subparsers = parser.add_subparsers(help='commands', dest='command') |
|
|
|
|
|
|
|
|
|
# Actions / commands |
|
|
|
|
INIT_PARSER = SUBPARSERS.add_parser('init', help='initialize credentials') |
|
|
|
|
INIT_PARSER.add_argument( |
|
|
|
|
init_parser = subparsers.add_parser('init', help='initialize credentials') |
|
|
|
|
init_parser.add_argument( |
|
|
|
|
'--totp', |
|
|
|
|
help=( |
|
|
|
|
'use totp login (requires user to open URL in browser and then enter token to bot ' |
|
|
|
|
'during init/login)' |
|
|
|
|
), |
|
|
|
|
action='store_true') |
|
|
|
|
LOGIN_PARSER = SUBPARSERS.add_parser( |
|
|
|
|
login_parser = subparsers.add_parser( |
|
|
|
|
'login', |
|
|
|
|
help='login to instance if credentials have expired') |
|
|
|
|
LOGIN_PARSER.add_argument( |
|
|
|
|
login_parser.add_argument( |
|
|
|
|
'--totp', |
|
|
|
|
help=( |
|
|
|
|
'use totp login (requires user to open URL in browser and then enter token to bot ' |
|
|
|
|
'during init/login)' |
|
|
|
|
), |
|
|
|
|
action='store_true') |
|
|
|
|
TOOT_PARSER = SUBPARSERS.add_parser('toot', help='send configured toot') |
|
|
|
|
RSS_PARSER = SUBPARSERS.add_parser('rss', help='cross post articles from an rss feed') |
|
|
|
|
CURATE_PARSER = SUBPARSERS.add_parser('curation', help='Run the curation bot (service)') |
|
|
|
|
WELCOME_PARSER = SUBPARSERS.add_parser('welcome', help='Run the welcome bot (service)') |
|
|
|
|
|
|
|
|
|
subparsers.add_parser('toot', help='send configured toot') |
|
|
|
|
subparsers.add_parser('rss', help='cross post articles from an rss feed') |
|
|
|
|
subparsers.add_parser('curation', help='Run the curation bot (service)') |
|
|
|
|
subparsers.add_parser('welcome', help='Run the welcome bot (service)') |
|
|
|
|
|
|
|
|
|
# Parse CLI arguments |
|
|
|
|
ARGS = PARSER.parse_args() |
|
|
|
|
args = parser.parse_args() |
|
|
|
|
|
|
|
|
|
# Make sure a command was specified |
|
|
|
|
if ARGS.command is None: |
|
|
|
|
if args.command is None: |
|
|
|
|
sys.exit('command must be specified') |
|
|
|
|
|
|
|
|
|
# Make sure the config file specified exists |
|
|
|
|
CONFIG_PATH = os.path.abspath(ARGS.config) |
|
|
|
|
if not os.path.exists(CONFIG_PATH): |
|
|
|
|
config_path = os.path.abspath(args.config) |
|
|
|
|
if not os.path.exists(config_path): |
|
|
|
|
sys.exit('invalid path to config file') |
|
|
|
|
|
|
|
|
|
# Read/parse config file |
|
|
|
|
CONFIG = None |
|
|
|
|
config = None |
|
|
|
|
# Fix unicode special character error |
|
|
|
|
with codecs.open(CONFIG_PATH, "r", "utf8") as stream: |
|
|
|
|
CONFIG = YAML(typ='safe').load(stream) |
|
|
|
|
with codecs.open(config_path, "r", "utf8") as stream: |
|
|
|
|
config = YAML(typ='safe').load(stream) |
|
|
|
|
|
|
|
|
|
# Add TOTP flag to CONFIG which is passed to each function |
|
|
|
|
if 'totp' in ARGS: |
|
|
|
|
CONFIG['config']['totp'] = ARGS.totp |
|
|
|
|
if 'totp' in args: |
|
|
|
|
config['config']['totp'] = args.totp |
|
|
|
|
|
|
|
|
|
# Ensure client_cred_file path is valid |
|
|
|
|
client_cred_file = pathlib.Path(CONFIG['config']['client_cred_file']) |
|
|
|
|
client_cred_file = pathlib.Path(config['config']['client_cred_file']) |
|
|
|
|
if not client_cred_file.parent.exists(): |
|
|
|
|
sys.exit('client_cred_file directory does not exist') |
|
|
|
|
# Warn user that the config file WILL be created |
|
|
|
@ -225,68 +227,72 @@ if __name__ == '__main__':
|
|
|
|
|
print('warning: client_cred_file will be created') |
|
|
|
|
|
|
|
|
|
# Ensure user_cred_file path is valid |
|
|
|
|
user_cred_file = pathlib.Path(CONFIG['config']['client_cred_file']) |
|
|
|
|
user_cred_file = pathlib.Path(config['config']['client_cred_file']) |
|
|
|
|
if not user_cred_file.parent.exists(): |
|
|
|
|
sys.exit('user_cred_file directory does not exist') |
|
|
|
|
if not user_cred_file.exists(): |
|
|
|
|
print('warning: user_cred file will be created') |
|
|
|
|
|
|
|
|
|
# Deal with init command |
|
|
|
|
if ARGS.command == 'init': |
|
|
|
|
init(CONFIG) |
|
|
|
|
if args.command == 'init': |
|
|
|
|
init(config) |
|
|
|
|
|
|
|
|
|
# Deal with login command |
|
|
|
|
if ARGS.command == 'login': |
|
|
|
|
login(CONFIG) |
|
|
|
|
if args.command == 'login': |
|
|
|
|
login(config) |
|
|
|
|
|
|
|
|
|
# Deal with curation command |
|
|
|
|
if ARGS.command == 'curation': |
|
|
|
|
if args.command == 'curation': |
|
|
|
|
# Verify toot cache file folder exists |
|
|
|
|
cache_file = pathlib.Path(CONFIG['curation']['cache_file']) |
|
|
|
|
cache_file = pathlib.Path(config['curation']['cache_file']) |
|
|
|
|
if not cache_file.parent.exists(): |
|
|
|
|
# Warn if curation cache file doesn't exist |
|
|
|
|
sys.exit('curation cache_file directory does not exist') |
|
|
|
|
if not cache_file.exists(): |
|
|
|
|
print('warning: curation cache_file file will be created') |
|
|
|
|
curate(CONFIG) |
|
|
|
|
curate(config) |
|
|
|
|
|
|
|
|
|
# Deal with welcome command |
|
|
|
|
if ARGS.command == 'welcome': |
|
|
|
|
if args.command == 'welcome': |
|
|
|
|
# Verify toot cache file folder exists |
|
|
|
|
cache_file = pathlib.Path(CONFIG['welcome']['cache_file']) |
|
|
|
|
cache_file = pathlib.Path(config['welcome']['cache_file']) |
|
|
|
|
if not cache_file.parent.exists(): |
|
|
|
|
# Warn if welcome cache file doesn't exist |
|
|
|
|
sys.exit('welcome cache_file directory does not exist') |
|
|
|
|
if not cache_file.exists(): |
|
|
|
|
print('warning: welcome cache_file file will be created') |
|
|
|
|
welcome(CONFIG) |
|
|
|
|
welcome(config) |
|
|
|
|
|
|
|
|
|
# Deal with toot command |
|
|
|
|
if ARGS.command == 'toot': |
|
|
|
|
if args.command == 'toot': |
|
|
|
|
# Ensure main toot is <= 500 characters |
|
|
|
|
TOOT_LENGTH = len(CONFIG['toot']['toot_text']) |
|
|
|
|
if 'cw_text' in CONFIG['toot']: |
|
|
|
|
TOOT_LENGTH = TOOT_LENGTH + len(CONFIG['toot']['cw_text']) |
|
|
|
|
if TOOT_LENGTH > 500: |
|
|
|
|
toot_length = len(config['toot']['toot_text']) |
|
|
|
|
if 'cw_text' in config['toot']: |
|
|
|
|
toot_length = toot_length + len(config['toot']['cw_text']) |
|
|
|
|
if toot_length > 500: |
|
|
|
|
sys.exit('toot length must be <= 500 characters (including cw_text)') |
|
|
|
|
|
|
|
|
|
# Ensure sub toots are <= 500 characters |
|
|
|
|
if 'subtoots' in CONFIG['toot']: |
|
|
|
|
for subtoot in CONFIG['toot']['subtoots']: |
|
|
|
|
TOOT_LENGTH = len(subtoot['toot_text']) |
|
|
|
|
if 'subtoots' in config['toot']: |
|
|
|
|
for subtoot in config['toot']['subtoots']: |
|
|
|
|
toot_length = len(subtoot['toot_text']) |
|
|
|
|
if 'cw_text' in subtoot: |
|
|
|
|
TOOT_LENGTH = TOOT_LENGTH + len(subtoot['cw_text']) |
|
|
|
|
if TOOT_LENGTH > 500: |
|
|
|
|
toot_length = toot_length + len(subtoot['cw_text']) |
|
|
|
|
if toot_length > 500: |
|
|
|
|
sys.exit('sub toot length must be <= 500 characters (including cw_text)') |
|
|
|
|
|
|
|
|
|
toot(CONFIG) |
|
|
|
|
toot(config) |
|
|
|
|
|
|
|
|
|
# Deal with rss command |
|
|
|
|
if ARGS.command == 'rss': |
|
|
|
|
cache_file = pathlib.Path(CONFIG['rss']['cache_file']) |
|
|
|
|
if args.command == 'rss': |
|
|
|
|
cache_file = pathlib.Path(config['rss']['cache_file']) |
|
|
|
|
if not cache_file.parent.exists(): |
|
|
|
|
# Warn if RSS cache file doesn't exist |
|
|
|
|
sys.exit('rss cache_file directory does not exist') |
|
|
|
|
if not cache_file.exists(): |
|
|
|
|
print('warning: rss_cache_file file will be created') |
|
|
|
|
rss(CONFIG) |
|
|
|
|
rss(config) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
handle_command_line() |
|
|
|
|