这是https://layer7.solutions为我们创建的软件工具使用的一组实用程序。它包括一个带有我们设置的默认配置信息的记录器,以及一个能够从自定义数据库中提取登录信息的oauth包装器。
Layer-7-Utilities的Python项目详细描述
这是什么?
这是https://Layer7.Solutions为我们创建的软件工具使用的一组实用程序它包括一个带有我们设置的默认配置信息的记录器,以及一个能够从自定义数据库中提取登录信息的oauth包装器。
要创建oauth数据库,可以使用以下创建sql:
createtableoauth_data(usernametextnotnullconstraintoauth_pkeyprimarykey,passwordtext,app_idtext,app_secrettext,app_refreshtext,agent_oftext);
如何构建和安装
- 在文件夹的根目录中
- 运行
python3 setup.py sdist
- 运行
pip install .
使用方法:
记录器:
这将使用默认文件处理程序sentry.io集成和日志旋转创建自定义日志记录器。默认logspath设置为“/opt/skynet/redditbots/logs/”,但是您可以将其覆盖到自己的位置。 初始化和配置
注意,如果使用新的sentry_sdk,则需要在变量之后但在创建记录器之前添加以下导入和行。LoggerConfig()
的raven
参数也需要设置为false。
importsentry_sdksentry_sdk.init(dsn=__dsn__,release=__version__)
如果要使用传统raven,则需要将raven安装为一个前置请求:pip install raven
。
importlogging.configfromlayer7_utilitiesimportLoggerConfig__botname__='Short_Name_For_The_Bot'__description__='Description of the bot'__author__='Authors Name/Info'__version__='1.2.3'__dsn__='Get from Sentry.io'# Sets up the sentry_sdk integration:importsentry_sdksentry_sdk.init(dsn=__dsn__,release=__version__)# Create the logger (with Raven (legacy) disabled)logspath='Path/To/The/Logs/Folder/'# With trailing backslash.loggerconfig=LoggerConfig(__dsn__,__botname__,__version__,logspath,raven=False)logging.config.dictConfig(loggerconfig.get_config())logger=logging.getLogger('root')logger.info(u"/*********Starting App*********\\")logger.info(u"App Name: {} | Version: {}".format(__botname__,__version__))
认证
auth依赖于一个包含reddit应用程序id、secret、username、password等的自定义表。其他任何人都不会设置这个表。但是,如果您有权访问我们的数据库,或者正在编写一个可以利用的bot,那么就可以这样设置它。
在第7层环境中,auth数据库表是“thetraveler”。
fromlayer7_utilitiesimportoAuth__botname__='Short_Name_For_The_Bot'__description__='Description of the bot'__author__='Authors Name/Info'__version__='1.2.3'__dsn__='Get from Sentry.io'__agent_of__='category value'auth=oAuth()auth.get_accounts(__agent_of__,__description__,__version__,__author__,__botname__,DB_USERNAME,DB_PASSWORD,DB_HOST,DatabaseTableName)foraccountinauth.accounts:r=account.login()me=r.user.me()print('Started Reddit Instance: u/%s'%me)