Integrate rich display logic and logging into pytchat_listener.py

This commit is contained in:
2025-10-30 18:54:57 +01:00
parent 3b5ec1fb47
commit f7bfa63562

View File

@@ -2,6 +2,8 @@ import pytchat
import sys import sys
import os import os
import time import time
import re
from datetime import datetime
from rich.console import Console from rich.console import Console
from rich.style import Style from rich.style import Style
@@ -13,12 +15,17 @@ def main():
video_id = input("Enter the YouTube Live Stream Video ID: ") video_id = input("Enter the YouTube Live Stream Video ID: ")
# Setup chat logging
log_dir = "chat_logs"
os.makedirs(log_dir, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_filename = os.path.join(log_dir, f"chat_{video_id}_{timestamp}.log")
log_file = open(log_filename, "w", encoding="utf-8")
console.print(f"[green]Chat will be logged to {log_filename}[/green]")
try: try:
livechat = pytchat.create(video_id=video_id) livechat = pytchat.create(video_id=video_id)
console.print(f"[green]Listening to live chat for video ID: {video_id}[/green]") console.print(f"[green]Listening to live chat for video ID: {video_id}[/green]")
# pytchat doesn't directly expose the live_chat_id in the same way as the official API
# For now, we'll just display the video_id as confirmation.
# If a live_chat_id is needed, further pytchat internal analysis would be required.
console.print(f"[green]Video ID accepted: {video_id}[/green]") console.print(f"[green]Video ID accepted: {video_id}[/green]")
# Wait for 5 seconds, then clear the screen # Wait for 5 seconds, then clear the screen
@@ -31,20 +38,50 @@ def main():
author_display_name = c.author.name author_display_name = c.author.name
message_text = c.message message_text = c.message
# Simple alternating background for readability # pytchat does not provide explicit emote data like the official API
# So we rely on regex for text-based emotes and standard emojis.
# Process text-based emotes (e.g., :face-purple-sweating:)
message_text = re.sub(r'(:[a-zA-Z0-9_-]+:)', r'[blue]\1[/blue]', message_text)
# Add coloring for standard emojis
emoji_pattern = re.compile(
(" # Start capturing group
"\U0001F600-\U0001F64F" # emoticons
"\U0001F300-\U0001F5FF" # symbols & pictographs
"\U0001F680-\U0001F6FF" # transport & map symbols
"\U0001F1E0-\U0001F1FF" # flags (iOS)
"\U00002702-\U000027B0" # Dingbats
"\U000024C2-\U0001F251" # Enclosed characters
"\U0001F900-\U0001F9FF" # Supplemental Symbols and Pictographs
"\U0000200D" # Zero Width Joiner (for emoji sequences)
"\U0000FE0F" # Variation Selector-16 (for emoji presentation)
")+
, re.UNICODE)
message_text = emoji_pattern.sub(r'[magenta]\1[/magenta]', message_text)
# Alternate background styles
background_style = Style(bgcolor="#2B2B2B") if message_count % 2 == 0 else Style(bgcolor="#3A3A3A") background_style = Style(bgcolor="#2B2B2B") if message_count % 2 == 0 else Style(bgcolor="#3A3A3A")
# Basic username styling # Simple color for username (can be expanded to unique colors per user)
username_style = Style(color="#4CAF50", bold=True) username_style = Style(color="#4CAF50", bold=True)
# Format message for terminal and log file
formatted_message = f"[{username_style}]{author_display_name}[/]: {message_text}" formatted_message = f"[{username_style}]{author_display_name}[/]: {message_text}"
console.print(formatted_message, style=background_style) console.print(formatted_message, style=background_style)
log_file.write(f"{datetime.now().strftime("%H:%M:%S")} {formatted_message}\n")
message_count += 1 message_count += 1
except Exception as e: except Exception as e:
console.print(f"[red]An error occurred: {e}[/red]") console.print(f"[red]An error occurred: {e}[/red]")
finally: finally:
console.print("[yellow]Live chat listener stopped.[/yellow]") log_file.close()
save_log = input(f"\nDo you want to save the chat log to {log_filename}? (y/n): ").lower()
if save_log != 'y' and save_log != 'yes':
os.remove(log_filename)
console.print(f"[red]Chat log not saved. {log_filename} deleted.[/red]")
else:
console.print(f"[green]Chat log saved to {log_filename}[/green]")
if __name__ == '__main__': if __name__ == '__main__':
main() main()