Пример #1
0
def getLogger(name, level=__loglevel):
    formatter = logging.Formatter('%(nasip)s %(nasid)s %(siteid)s %(msgtype)s %(message)s', '%a, %d %b %Y %H:%M:%S', )
    logger = logging.getLogger(name)
    logger.setLevel(level)

    logger.addFilter(log_ctx)

    if GRAYLOG_SERVER:
        client = google.cloud.logging.Client()
        cloud_handler = CloudLoggingHandler(client, name="radiusd")
        logger.addHandler(cloud_handler)
        setup_logging(cloud_handler)
        cloud_handler.addFilter(log_ctx)
        cloud_handler.setFormatter(formatter)
    else:
        stream_handler = logging.StreamHandler(sys.stdout)
        stream_handler.setFormatter(formatter)
        logger.addHandler(stream_handler)

    return logger
def main():
    # Parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--discord-token',
        help=
        'Token to use when running the bot. You can either use the raw token string or a path to a text file containing the token.',
        dest='discord_bot_token',
        default='discord_token.txt')
    parser.add_argument('--ffmpeg-path',
                        help='Path to ffmpeg executable.',
                        dest='ffmpeg_path',
                        default='ffmpeg')
    parser.add_argument(
        '--google-credentials-path',
        help='Path to Google application credentials JSON file.',
        dest='google_credentials_path',
        default='google_credentials.json')
    parser.add_argument(
        '--dictionary-api',
        help=
        'A list of dictionary API\'s to use for fetching definitions. These should be in order of priority and separated by comma\'s. Available API\'s are'
        '\'google\', \'owlbot\', \'webster\', and \'rapid-words\'. Some API\'s require tokens that must be provided with the appropriate arguments.',
        dest='dictionary_api',
        default='google')
    parser.add_argument(
        '--owlbot-api-token',
        help=
        'The token to use for the Owlbot dictionary API. You can use either the raw token string or a path to a text file containing the token.',
        dest='owlbot_api_token',
        default='owlbot_api_token.txt')
    parser.add_argument(
        '--webster-api-token',
        help=
        'The token to use for the Merriam Webster dictionary API. You can use either the raw token string or a path to a text file containing the token.',
        dest='webster_api_token',
        default='webster_api_token.txt')
    parser.add_argument(
        '--rapid-words-api-token',
        help=
        'The token to use for the RapidAPI WordsAPI dictionary API. You can use either the raw token string or a path to a text file containing the token.',
        dest='rapid_words_api_token',
        default='rapid_words_api_token.txt')
    args = parser.parse_args()

    # Set Google API credentials
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = args.google_credentials_path

    # Set up GCP logging
    gcp_logging_client = google.cloud.logging.Client()
    gcp_logging_handler = CloudLoggingHandler(gcp_logging_client,
                                              name='discord-dictionary-bot')
    gcp_logging_handler.addFilter(gcp_logging_filter)
    logging.getLogger().addHandler(gcp_logging_handler)

    # Check which dictionary API we should use
    dictionary_apis = []
    for name in args.dictionary_api.split(','):

        if name == 'google':
            dictionary_apis.append(UnofficialGoogleAPI())
        elif name == 'owlbot':

            if 'owlbot_api_token' not in args:
                print(
                    f'You must specify an API token with --owlbot-api-token to use the owlbot dictionary API!'
                )
                return

            # Read owlbot API token from file
            owlbot_api_token = try_read_token(args.owlbot_api_token)

            dictionary_apis.append(OwlBotDictionaryAPI(owlbot_api_token))

        elif name == 'webster':

            if 'webster_api_token' not in args:
                print(
                    f'You must specify an API token with --webster-api-token to use the Merriam Webster dictionary API!'
                )
                return

            # Read API token from file
            webster_api_token = try_read_token(args.webster_api_token)

            dictionary_apis.append(MerriamWebsterAPI(webster_api_token))

        elif name == 'rapid-words':

            if 'rapid_words_api_token' not in args:
                print(
                    f'You must specify an API token with --rapid-words-api-token to use the Rapid API WordsAPI dictionary API!'
                )
                return

            # Read API token from file
            rapid_words_api_token = try_read_token(args.rapid_words_api_token)

            dictionary_apis.append(RapidWordsAPI(rapid_words_api_token))

        else:
            print(f'Invalid dictionary API: {args.dictionary_api}')
            return

    # Start client
    bot = DiscordBotClient(BackupDictionaryAPI(dictionary_apis),
                           args.ffmpeg_path)
    bot.run(try_read_token(args.discord_bot_token))