예제 #1
0
def main():
    api = MediaWiki()
    api.login("TAP_Bot", "PASSWORD")
    bot = VitalArticleBot(api)
    if bot.is_allowed:
        bot.run()
    else:
        print("Check the bot's shutoff page!")
    api.logout()
예제 #2
0
파일: cresbot.py 프로젝트: onei/cresbot
def main():
    """Set up configuration and start any required scripts or tasks.

    Raises:
        SetupError
    """
    # set available command line arguments
    parser = ArgumentParser(prog='./cresbot.py')
    parser.add_argument('config',
                        help='Set path to config file.')
    parser.add_argument('-t',
                        choices=['all', 'hiscorecounts'],
                        default=[],
                        dest='tasks',
                        help='Run tasks on startup. To run all tasks on startup use `all`. To run specific tasks, add them by name delimited by a space. Allowed task names: `hiscorecounts`.',
                        metavar='task',
                        nargs='*')

    # parse arguments and convert to a dictionary
    args = parser.parse_args()
    args = vars(args)

    # load config from file
    config_path = args.pop('config', 0)

    with open(config_path) as f:
        try:
            config = yaml.load(f)
        except FileNotFoundError as e:
            raise SetupError from e

        # merge args into config
        config.update(args)

    # setup logging
    try:
        log = get_logger(config, 'cresbot')
    except FileNotFoundError as e:
        raise SetupError('Log file could not be found. Please check the directory exists.') from e

    # setup api instance
    api = MediaWiki(config.get('api_url'), config.get('api_config'))

    try:
        logged_in = api.login(config.get('api_username'), config.get('api_password'))
    # @todo catch more specific exception
    #       ApiError?
    except Exception as e:
        raise SetupError('MediaWiki API URL could not be verified. Please check your config file.') from e

    # check login attempt was successful
    if not logged_in:
        raise SetupError('Incorrect password or username in config.')

    # clean up
    api.logout()

    # store in config for convenience
    config.update({'api': api})

    log.info('Setup complete!')

    try:
        tasks.start_tasks(config)
    except Exception as e:
        log.exception('Uncaught exception: %s', e)