Browse Source

pylint-happy formatting

merge-requests/1/head
Kirk Strauser 4 years ago
parent
commit
ca5d04c113
  1. 45
      toot_bot.py

45
toot_bot.py

@ -1,8 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Adjust PyLint settings
# pylint: disable=C0301
''' Various Mastodon bots and their implementations '''
@ -18,13 +14,17 @@ from ruamel.yaml import YAML
from Toot import toot
from BotStreamListeners import CurationBot, WelcomeBot
def init(config):
''' Initialize the Mastdon API app and cache the API key
Auto login if app creation succeeds '''
Auto login if app creation succeeds '''
# Prompt user to find out if they want to continue if the client_cred_file exists already
# Shouldn't happen twice per docs
# Shouldn't happen twice per docs
if os.path.exists(config['config']['client_cred_file']):
sys.exit('init should only ever be called once, try login instead, if that fails. delete ' + config['config']['client_cred_file'] + ' and re-run init')
sys.exit((
'init should only ever be called once, try login instead, if that fails. delete {} '
' and re-run init'
).format(config['config']['client_cred_file']))
# Create app for API
Mastodon.create_app(
@ -37,10 +37,15 @@ def init(config):
# Login to seed login credentials
login(config)
def login(config):
''' Login to API and cache API access token(s) '''
# Prompt for user/password (NEVER store them on disk!)
email = input('what is the e-mail that was used to setup the bot account on ' + config['config']['api_base_url'] + '? ')
email = input(
'what is the e-mail that was used to setup the bot account on {}? '.format(
config['config']['api_base_url']
)
)
password = getpass.getpass('what is the password for the account? ')
# Setup Mastodon API
@ -71,6 +76,7 @@ def login(config):
to_file=config['config']['user_cred_file']
)
# Default implementation on whether or not to include an RSS article
# Returns true and has the same method signature as what end users can setup for custom logic
def default_include_article(article):
@ -81,13 +87,21 @@ def default_include_article(article):
return True
def rss(config):
''' Parse RSS feed and toot any new articles '''
# Setup custom include logic before opening any database connections
include_article_fn = default_include_article
if 'custom_logic_include_file' in config['rss'] and os.path.exists(os.path.abspath(config['rss']['custom_logic_include_file'])):
include_path = os.path.abspath(config['rss']['custom_logic_include_file'])
if (
'custom_logic_include_file' in config['rss']
and os.path.exists(include_path)
):
from importlib.machinery import SourceFileLoader
custom_logic = SourceFileLoader('custom.logic', os.path.abspath(config['rss']['custom_logic_include_file'])).load_module()
custom_logic = SourceFileLoader( # pylint: disable=deprecated-method
'custom.logic', include_path
).load_module()
include_article_fn = custom_logic.include_article
# Crash on reading the feed before doing any database operations or tooting
@ -121,6 +135,7 @@ def rss(config):
# Cleanup connection to sqlite database for rss cache
conn.close()
def curate(config):
''' Curate (fav/boost) popular posts on local and/or federated timelines '''
# Setup Mastodon API
@ -132,6 +147,7 @@ def curate(config):
mastodon.public_stream(CurationBot(config))
def welcome(config):
''' Welcome new users to the instance '''
# Setup Mastodon API
@ -143,6 +159,7 @@ def welcome(config):
mastodon.public_stream(WelcomeBot(config))
if __name__ == '__main__':
# Global CLI arguments/options
PARSER = argparse.ArgumentParser()
@ -208,8 +225,8 @@ if __name__ == '__main__':
if ARGS.command == 'curation':
# Verify toot cache file folder exists
if not os.path.exists(os.path.abspath(os.path.split(CONFIG['curation']['cache_file'])[0])):
# Warn if curation cache file doesn't exist
sys.exit('curation cache_file directory does not exist')
# Warn if curation cache file doesn't exist
if not os.path.exists(os.path.abspath(CONFIG['curation']['cache_file'])):
print('warning: curation cache_file file will be created')
curate(CONFIG)
@ -217,8 +234,8 @@ if __name__ == '__main__':
if ARGS.command == 'welcome':
# Verify toot cache file folder exists
if not os.path.exists(os.path.abspath(os.path.split(CONFIG['welcome']['cache_file'])[0])):
# Warn if welcome cache file doesn't exist
sys.exit('welcome cache_file directory does not exist')
# Warn if welcome cache file doesn't exist
if not os.path.exists(os.path.abspath(CONFIG['welcome']['cache_file'])):
print('warning: welcome cache_file file will be created')
welcome(CONFIG)
@ -245,11 +262,11 @@ if __name__ == '__main__':
if ARGS.command == 'rss':
if 'custom_logic_include_file' in CONFIG['rss']:
if not os.path.exists(os.path.abspath(CONFIG['rss']['custom_logic_include_file'])):
# Verify RSS cache file folder exists
sys.exit('custom logic file does not exist!')
# Verify RSS cache file folder exists
if not os.path.exists(os.path.abspath(os.path.split(CONFIG['rss']['cache_file'])[0])):
# Warn if RSS cache file doesn't exist
sys.exit('rss cache_file directory does not exist')
# Warn if RSS cache file doesn't exist
if not os.path.exists(os.path.abspath(CONFIG['rss']['cache_file'])):
print('warning: rss_cache_file file will be created')
rss(CONFIG)

Loading…
Cancel
Save