Twitch Helix API及其Webhook和PubSub的Python3.7+实现

twitchA的Python项目详细描述


Python Twitch API

PyPI verionPyPI verionTwitch API versionDocumentation Status

这是TwitchAPI、其Webhook和Python3.7中PubSub的完整实现。在

安装

使用pip安装:

pip install twitchAPI

文件

完整的API文档可以找到on readthedocs.org。在

使用

基本API调用

设置Twitch API实例并获取用户ID:

fromtwitchAPI.twitchimportTwitch# create instance of twitch APItwitch=Twitch('my_app_id','my_app_secret')twitch.authenticate_app([])# get ID of useruser_info=twitch.get_users(logins=['my_username'])user_id=user_info['data'][0]['id']

身份验证

TwitchAPI知道两种不同的身份验证。应用程序和用户身份验证。 你需要哪一个(如果有的话)取决于你想用什么电话。在

它总是好的至少得到应用程序认证,即使是在你不需要它的电话,因为速率限制是更好的方式,以认证的电话。在

应用程序验证

应用程序验证非常简单,只需执行以下操作:

^{pr2}$

用户身份验证

要获得用户身份验证令牌,用户必须在twitch网站上显式单击“Authorize”。您可以使用各种在线服务来生成令牌或使用我的内置验证器。 对于我的验证器,您必须添加以下URL作为“OAuth重定向URL”:http://localhost:17563 您可以设置here in your twitch dev dashboard。在

fromtwitchAPI.twitchimportTwitchfromtwitchAPI.oauthimportUserAuthenticatorfromtwitchAPI.typesimportAuthScopetwitch=Twitch('my_app_id','my_app_secret')target_scope=[AuthScope.BITS_READ]auth=UserAuthenticator(twitch,target_scope,force_verify=False)# this will open your default browser and prompt you with the twitch verification websitetoken,refresh_token=auth.authenticate()# add User authenticationtwitch.set_user_authentication(token,target_scope,refresh_token)

您可以重新使用此令牌并重新使用它:

fromtwitchAPI.oauthimportrefresh_access_tokennew_token,new_refresh_token=refresh_access_token('refresh_token','client_id','client_secret')

Webhook

有关完整的示例用法,请参见webhook_example.py。在

更详细的文档可以找到here on readthedocs。在

要求

你需要有一个端口打开的公共IP。默认情况下,该端口为80。 默认情况下,身份验证处于关闭状态,但您可以选择进行身份验证以使用某些Webhook主题或获取更多信息。
请注意,如果您选择进行身份验证,则端点URL必须是HTTPS,这意味着您可能需要类似nginx的反向代理。

启动Webhook

关于如何设置并启动webhook的示例:

fromtwitchAPI.twitchimportTwitchfromtwitchAPI.webhookimportTwitchWebHooktwitch=Twitch('my_app_id','my_app_secret')# add App authenticationtwitch.authenticate_app([])# Note that you have to use https as soon as you use functions that require authentication (most of them)hook=TwitchWebHook("https://my.cool.ip:8080",'your app id',8080)# some hooks dont require any authentication, which would remove the requirement to set up a https reverse proxy# if you dont require authentication just dont call authenticate()hook.authenticate(twitch)hook.start()

订阅Webhook主题

定义回调函数并订阅事件:

fromuuidimportUUIDfrompprintimportpprintdefcallback_user_changed(uuid:UUID,data:dict)->None:print(f'Callback for UUID {str(uuid)}')pprint(data)success,sub_uuid=hook.subscribe_user_changed(user_id,callback_user_changed)

订阅函数返回标识此订阅的UUID。这意味着您可以对多个订阅使用同一个回调函数。在

取消订阅

要取消订阅,只需从订阅中使用该UUID:

success=hook.unsubscribe_user_changed(sub_uuid)

停止Webhook

停止网络挂钩:

hook.stop()

取消订阅任何剩余的活动Webhook主题

如果您对webhook订阅的管理失败(由于崩溃或其他原因),并且程序关闭后仍有活动的webhook,则可以使用以下方法:

hook.unsubscribe_all(twitch)

参数是一个带有应用程序身份验证的twitchAPI.twitch.Twitch实例。在

PubSub公司

PubSub使您能够订阅主题,以便更新(例如,当用户在频道中欢呼时)。在

更详细的文档可以找到here on readthedocs

fromtwitchAPI.pubsubimportPubSubfromtwitchAPI.twitchimportTwitchfromtwitchAPI.typesimportAuthScopefrompprintimportpprintfromuuidimportUUIDdefcallback_whisper(uuid:UUID,data:dict)->None:print('got callback for UUID '+str(uuid))pprint(data)# setting up Authentication and getting your user idtwitch=Twitch('my_app_id','my_app_secret')twitch.authenticate_app([])twitch.set_user_authentication('my_user_auth_token',[AuthScope.WHISPERS_READ],'my_user_auth_refresh_token')user_id=twitch.get_users(logins=['my_username'])['data'][0]['id']# starting up PubSubpubsub=PubSub(twitch)pubsub.start()# you can either start listening before or after you started pubsub.uuid=pubsub.listen_whispers(user_id,callback_whisper)input('press ENTER to close...')# you do not need to unlisten to topics before stopping but you can listen and unlisten at any moment you wantpubsub.unlisten(uuid)pubsub.stop()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
字符串Java字母替换无效   java Spring Roo JPA MS SQL Server无法打开JPA EntityManager组织。冬眠例外GenericJDBCException:无法打开连接   在scala中使用JavaWS对大型数据文件进行java流式处理   Java编译器是否将字节和短字符识别为文本?   java无法查找符号错误,空指针   mongodb在Java中重用数据库连接   java将多个StringArray从字符串文件获取到活动中   java是一个变量,它只保存最后一次鼠标单击的坐标   c#尺寸有限;添加、删除和洗牌   java如何在Android中显示来自资产文件夹的文本文件中的文本   Android应用程序中的java Tensorflow Lite自定义对象检测模型错误   java如何在foreachloop中使用scanner将来自命令行的输入存储到数组中   java如何定义一个好的存储库接口   Android中的java解析动态json对象