Some working architecture for messages
This commit is contained in:
@@ -38,48 +38,52 @@ class TwitchApi:
|
||||
except:
|
||||
return TwitchStreamStatus.ERROR
|
||||
|
||||
def start_chat(self, streamer_name):
|
||||
def start_chat(self, streamer_name, on_message):
|
||||
logger.info("Connecting to %s:%s", TW_CHAT_SERVER, TW_CHAT_PORT)
|
||||
connection = ChatConnection(streamer_name, self.twitch)
|
||||
connection = ChatConnection(streamer_name, self, on_message)
|
||||
|
||||
self.twitch.get_app_token()
|
||||
connection.run()
|
||||
|
||||
def get_user_chat_channel(self, streamer_name):
|
||||
streams = self.twitch.get_streams(user_login=streamer_name)
|
||||
if streams is None or len(streams) < 1:
|
||||
return None
|
||||
return streams["data"][0]["user_login"]
|
||||
|
||||
|
||||
class ChatConnection:
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
connection = None
|
||||
|
||||
def __init__(self, streamer_name, twitch):
|
||||
self.twitch = twitch
|
||||
def __init__(self, streamer_name, api, on_message):
|
||||
self.on_message = on_message
|
||||
self.api = api
|
||||
self.streamer_name = streamer_name
|
||||
|
||||
def run(self):
|
||||
# Need to verify channel name.. case sensative
|
||||
streams = self.twitch.get_streams(user_login=self.streamer_name)
|
||||
if streams is None or len(streams) < 1:
|
||||
return
|
||||
# Need to verify channel name.. case sensitive
|
||||
channel = self.api.get_user_chat_channel(self.streamer_name)
|
||||
if not channel:
|
||||
logger.error("Cannot find streamer channel")
|
||||
|
||||
channel = streams["data"][0]["user_login"]
|
||||
self.connect_to_chat(f"#{channel}")
|
||||
|
||||
def connect_to_chat(self, channel):
|
||||
self.connection = socket.socket()
|
||||
self.connection.connect((TW_CHAT_SERVER, TW_CHAT_PORT))
|
||||
self.connection.send(f"PASS sdwrerrwsdawerew\n".encode("utf-8"))
|
||||
# public data to join hat
|
||||
self.connection.send(f"PASS couldBeRandomString\n".encode("utf-8"))
|
||||
self.connection.send(f"NICK justinfan123\n".encode("utf-8"))
|
||||
self.connection.send(f"JOIN #{channel}\n".encode("utf-8"))
|
||||
self.connection.send(f"JOIN {channel}\n".encode("utf-8"))
|
||||
|
||||
while True:
|
||||
resp = self.connection.recv(4096).decode('utf-8')
|
||||
logger.warning(f"Message: {resp}")
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def disconnect(self, msg="I'll be back!"):
|
||||
logger.info("Disconnected ")
|
||||
|
||||
def on_welcome(self, c, e):
|
||||
logger.info("Joining channel ")
|
||||
c.join("#" + self.streamer_name)
|
||||
logger.info("Joined????? ")
|
||||
|
||||
def on_pubmsg(self, c, e):
|
||||
logger.info("On message %s <-> %s", c, e)
|
||||
try:
|
||||
while True:
|
||||
msg = self.connection.recv(4096).decode('utf-8')
|
||||
logger.warning(f"Twitch message-> {msg}")
|
||||
if self.on_message:
|
||||
self.on_message(msg)
|
||||
except BaseException as e:
|
||||
logger.error(e)
|
||||
logger.error("Error happened during reading chat")
|
||||
|
||||
Reference in New Issue
Block a user