From df8805e5eeca37b68543587c9c56a5f4c45fed6b Mon Sep 17 00:00:00 2001 From: Alex Orid Date: Mon, 26 Apr 2021 15:31:22 -0400 Subject: [PATCH] api stuff --- standalone_twitch_pubsub.py | 57 ++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/standalone_twitch_pubsub.py b/standalone_twitch_pubsub.py index 1497fab..a3ff5bb 100644 --- a/standalone_twitch_pubsub.py +++ b/standalone_twitch_pubsub.py @@ -1,7 +1,14 @@ -import credentials +import re +from json import loads +from urllib.parse import urlencode +import requests + +import credentials import config +import channel_rewards.channelRewards_base + import twitchAPI from twitchAPI.pubsub import PubSub from twitchAPI.twitch import Twitch @@ -10,6 +17,7 @@ from twitchAPI.oauth import UserAuthenticator from pprint import pprint from uuid import UUID +from cooldowns import Cooldown_Module class Twitch_Pubsub(): def __init__(self): @@ -22,6 +30,9 @@ class Twitch_Pubsub(): self.uuid_1 = None self.uuid_2 = None + self.cooldownModule: Cooldown_Module = Cooldown_Module() + self.cooldownModule.setupCooldown("twitchChat", 20, 32) + def setup(self): self.twitch.authenticate_app(self.target_scope) @@ -68,6 +79,11 @@ class Twitch_Pubsub(): print("Channel Point Redemption") print('got callback for UUID ' + str(uuid)) pprint(data) + #self.callback_EXEC( + # "sender", + # "rewardName", + # channel_rewards.channelRewards_base.AbstractChannelRewards.ChannelRewardsType.channelPoints, + # data) def callback_bits(self, uuid: UUID, data: dict) -> None: print("Bits Redemption") @@ -79,6 +95,45 @@ class Twitch_Pubsub(): print('got callback for UUID ' + str(uuid)) pprint(data) + + def callback_EXEC(self, sender, rewardName:str, rewardType, raw_data): + try: + is_actionable = self.is_reward(rewardName, rewardType) + if is_actionable: + if self.cooldownModule.isCooldownActive("twitchChat") == False: + self.exec_reward(sender, rewardName, rewardType, "", raw_data) + except: + print("something went wrong with a reward") + + def is_reward(self, rewardName:str, rewardType): + # todo need to url-escape word + clean_param = urlencode({'reward_name': rewardName, 'reward_type':rewardType}) + url = "http://channelrewards:6969/api/v1/reward?%s" % clean_param + resp = requests.get(url) + return resp.status_code == 200 + + def exec_reward(self, sender, reward, rewardType, rest, realMessage): + params = urlencode( + {'command_source': channel_rewards.channelRewards_base.AbstractChannelRewards.ChannelRewardsSource.Twitch, + 'user_name': sender, + 'reward_name': reward, + 'reward_type': rewardType, + 'rest': rest, + 'bonus_data': realMessage}) + + url = "http://channelrewards:6969/api/v1/exec_reward?%s" % params + resp = requests.get(url) + if resp.status_code == 200: + print("Got the following message: %s" % resp.text) + data = loads(resp.text) + msg = data['message'] + if msg is not None: + #self.send_message(msg) #Cant Send messages with this pubsub library afaik + pass + else: + # todo handle failed requests + pass + if __name__ == "__main__": testModule = Twitch_Pubsub()