listen chat
This commit is contained in:
+100
-6
@@ -8,9 +8,11 @@ import pprint
|
||||
import random
|
||||
import requests
|
||||
import shutil
|
||||
import datetime
|
||||
# import datetime
|
||||
from datetime import datetime
|
||||
import glob
|
||||
import threading
|
||||
import websockets
|
||||
import asyncio
|
||||
from urllib.parse import urlparse
|
||||
from pytmi import Client
|
||||
@@ -27,6 +29,8 @@ from rich.table import Table
|
||||
from rich.text import Text
|
||||
import subprocess
|
||||
import pty
|
||||
from typing import List, Optional
|
||||
from dataclasses import dataclass
|
||||
|
||||
console = Console()
|
||||
|
||||
@@ -35,15 +39,15 @@ def sleep_control(time_sleep, running):
|
||||
time.sleep(time_sleep)
|
||||
|
||||
def hprint(color, texte):
|
||||
timestamp = datetime.datetime.now().strftime("%Hh %Mm %Ss")
|
||||
timestamp = datetime.now().strftime("%Hh %Mm %Ss")
|
||||
console.print("[bold "+color+"] ["+timestamp+"] "+texte+" [/bold "+color+"]")
|
||||
|
||||
def sprint(script_name,color, texte):
|
||||
timestamp = datetime.datetime.now().strftime("%Hh %Mm %Ss")
|
||||
timestamp = datetime.now().strftime("%Hh %Mm %Ss")
|
||||
console.print("[bold "+color+"] ["+timestamp+"] ("+script_name+") "+texte+" [/bold "+color+"]")
|
||||
|
||||
def debug_print(TAG, texte_print, type_debug,script_name):
|
||||
timestamp = datetime.datetime.now().strftime("%Hh %Mm %Ss")
|
||||
timestamp = datetime.now().strftime("%Hh %Mm %Ss")
|
||||
type_color = "blue"
|
||||
# more color
|
||||
# DARKCYAN
|
||||
@@ -218,6 +222,14 @@ storage = PersistentStorage()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
################### RecordTwitch ########################
|
||||
@@ -343,8 +355,8 @@ class RecordTwitch:
|
||||
os.makedirs(output_directory, exist_ok=True)
|
||||
os.makedirs(in_record_directory, exist_ok=True)
|
||||
|
||||
timestamp_complet = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||
timestamp = datetime.datetime.now().strftime("%Y-%m-%d")
|
||||
timestamp_complet = datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d")
|
||||
|
||||
output_file_path = os.path.join(in_record_directory, f"{timestamp}_%03d.mp3")
|
||||
|
||||
@@ -393,6 +405,88 @@ class RecordTwitch:
|
||||
|
||||
|
||||
|
||||
################### ChatMessage ########################
|
||||
|
||||
|
||||
@dataclass
|
||||
class ChatMessage:
|
||||
timestamp: datetime
|
||||
username: str
|
||||
content: str
|
||||
|
||||
class TwitchChatBot:
|
||||
def __init__(self, channel: str):
|
||||
self.channel = channel.lower()
|
||||
self.messages: List[ChatMessage] = []
|
||||
self.uri = "wss://irc-ws.chat.twitch.tv:443"
|
||||
|
||||
def get_last_message(self) -> Optional[ChatMessage]:
|
||||
return self.messages[-1] if self.messages else None
|
||||
|
||||
def get_all_messages(self) -> List[ChatMessage]:
|
||||
return self.messages
|
||||
|
||||
async def connect_to_twitch_chat(self):
|
||||
async with websockets.connect(self.uri) as websocket:
|
||||
# Se connecter en tant qu'utilisateur anonyme
|
||||
await websocket.send("CAP REQ :twitch.tv/tags twitch.tv/commands")
|
||||
await websocket.send(f"NICK justinfan{random.randint(10000,99999)}")
|
||||
await websocket.send(f"JOIN #{self.channel}")
|
||||
|
||||
print(f"✅ Connecté au chat de #{self.channel}")
|
||||
|
||||
while True:
|
||||
try:
|
||||
message = await websocket.recv()
|
||||
if "PRIVMSG" in message:
|
||||
# Extraction des données du message
|
||||
user = message.split("!", 1)[0][1:]
|
||||
if "display-name=" in user:
|
||||
user = user.split("display-name=")[1].split(";")[0]
|
||||
else:
|
||||
user = user.lower()
|
||||
|
||||
msg = message.split("PRIVMSG", 1)[1].split(":", 1)[1]
|
||||
timestamp = datetime.now()
|
||||
|
||||
# Création et stockage du message
|
||||
chat_message = ChatMessage(timestamp=timestamp, username=user, content=msg)
|
||||
self.messages.append(chat_message)
|
||||
|
||||
# Affichage formaté
|
||||
time_str = timestamp.strftime("%H:%M:%S.%f")[:-3]
|
||||
print(f"[\033[95m{time_str}\033[0m] \033[94m{user}\033[0m: \033[92m{msg}\033[0m")
|
||||
|
||||
except websockets.exceptions.ConnectionClosed:
|
||||
print("\033[91m⚠️ Connexion fermée, reconnexion en cours...\033[0m")
|
||||
break
|
||||
|
||||
def start(self):
|
||||
asyncio.run(self.connect_to_twitch_chat())
|
||||
|
||||
|
||||
|
||||
|
||||
################## ChatMessage FIN ########################
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user