Praxis_Bot/standalone_twitch_pubsub copy.py
2021-04-23 15:37:13 -04:00

58 lines
2.1 KiB
Python

from twitchAPI.pubsub import PubSub
from twitchAPI.twitch import Twitch
from twitchAPI.types import AuthScope, AuthType
from twitchAPI.oauth import UserAuthenticator
from pprint import pprint
from uuid import UUID
import config
import commands.command_base
import credentials
credentials_manager = credentials.Credentials_Module()
credentials_manager.load_credentials()
twitchCredential = credentials_manager.find_Twitch_Credential(config.credentialsNickname)
def callback_channelPoints(self, uuid: UUID, data: dict) -> None:
print("\nChannel Point Detection")
print('got callback for UUID ' + str(uuid))
pprint(data)
def callback_whisper(uuid: UUID, data: dict) -> None:
print("\nWhisper Detection")
print('got callback for UUID ' + str(uuid))
pprint(data)
# setting up Authentication and getting your user id
twitch = Twitch(twitchCredential.pubsub_client_id, twitchCredential.pubsub_secret)
twitch.authenticate_app([])
# you can get your user auth token and user auth refresh token following the example in twitchAPI.oauth
target_scope = [AuthScope.WHISPERS_READ, AuthScope.CHANNEL_READ_REDEMPTIONS, AuthScope.CHANNEL_MANAGE_REDEMPTIONS]
auth = UserAuthenticator(twitch, target_scope, force_verify=True)
token, refresh_token = auth.authenticate()
if token is not None: print("found token")
if refresh_token is not None: print("found refresh_token")
#print(token)
#print(refresh_token)
twitch.set_user_authentication(token, target_scope, refresh_token)
#twitch.set_user_authentication(twitchCredential.pubsub_AccessToken, target_scope, twitchCredential.pubsub_RefreshToken)
user_id = twitch.get_users(logins=['thecuriousnerd'])['data'][0]['id']
# starting up PubSub
pubsub = PubSub(twitch)
# you can either start listening before or after you started pubsub.
pubsub.ping_frequency = 30
pubsub.start()
uuid1 = pubsub.listen_whispers(user_id, callback_whisper)
uuid2 = pubsub.listen_channel_points(user_id, callback_channelPoints)
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 want
pubsub.unlisten(uuid1)
pubsub.unlisten(uuid2)
pubsub.stop()