Files
youtube-chat-webhook-v2/pytchat_listener.py

50 lines
1.7 KiB
Python

import pytchat
import sys
import os
import time
from rich.console import Console
from rich.style import Style
console = Console()
def main():
# Clear the terminal screen
os.system('clear')
video_id = input("Enter the YouTube Live Stream Video ID: ")
try:
livechat = pytchat.create(video_id=video_id)
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]")
# Wait for 5 seconds, then clear the screen
time.sleep(5)
os.system('clear')
message_count = 0
while livechat.is_alive():
for c in livechat.get().sync_items():
author_display_name = c.author.name
message_text = c.message
# Simple alternating background for readability
background_style = Style(bgcolor="#2B2B2B") if message_count % 2 == 0 else Style(bgcolor="#3A3A3A")
# Basic username styling
username_style = Style(color="#4CAF50", bold=True)
formatted_message = f"[{username_style}]{author_display_name}[/]: {message_text}"
console.print(formatted_message, style=background_style)
message_count += 1
except Exception as e:
console.print(f"[red]An error occurred: {e}[/red]")
finally:
console.print("[yellow]Live chat listener stopped.[/yellow]")
if __name__ == '__main__':
main()