Chat monitors stream to end

This commit is contained in:
MaddoScientisto 2026-02-11 13:23:14 +01:00
commit 38d51636af
4 changed files with 89 additions and 1 deletions

View file

@ -365,6 +365,7 @@ class ContentDownloader:
max_messages: Optional[int] = None,
timeout: Optional[float] = None,
shutdown_check: Optional[callable] = None,
stream_monitor = None,
verbose: bool = False) -> bool:
"""
Download live chat using chat_downloader library as fallback.
@ -376,6 +377,7 @@ class ContentDownloader:
max_messages: Maximum messages to download (None = unlimited)
timeout: Stop after this many seconds (None = until stream ends)
shutdown_check: Optional callback function that returns True when shutdown requested
stream_monitor: Optional stream monitor to check if stream is still live
verbose: Show chat message previews
Returns:
@ -414,7 +416,10 @@ class ContentDownloader:
# The get_chat with output parameter writes to file automatically
# We just need to iterate to trigger the download
message_count = 0
print(f'{Fore.CYAN}Receiving chat messages (press Ctrl+C to stop)...{Style.RESET_ALL}')
last_check_time = time.time()
check_interval = 10.0 # Check if stream is still live every 10 seconds
print(f'{Fore.CYAN}Receiving chat messages (will stop when stream ends)...{Style.RESET_ALL}')
try:
for message in chat:
# Check for shutdown request
@ -422,6 +427,19 @@ class ContentDownloader:
print(f'\n{Fore.YELLOW}⚠ Chat download stopped by shutdown request{Style.RESET_ALL}')
break
# Periodically check if stream is still live
current_time = time.time()
if stream_monitor and (current_time - last_check_time) >= check_interval:
last_check_time = current_time
try:
is_live = stream_monitor.is_user_live()
if not is_live:
print(f'\n{Fore.YELLOW}⚠ Stream ended, stopping chat download{Style.RESET_ALL}')
break
except Exception as check_error:
print(f'\n{Fore.YELLOW}⚠ Could not check stream status: {check_error}{Style.RESET_ALL}')
# Continue downloading to avoid false positives from API errors
message_count += 1
# Show progress every 100 messages
@ -467,6 +485,7 @@ class ContentDownloader:
def start_chat_downloader_thread(self, username: str, json_path: str,
shutdown_check: Optional[callable] = None,
stream_monitor = None,
verbose: bool = False) -> threading.Thread:
"""
Start chat_downloader in a background thread.
@ -475,6 +494,7 @@ class ContentDownloader:
username: Twitch username
json_path: Path to save chat JSON
shutdown_check: Callback to check for shutdown
stream_monitor: Optional stream monitor to check if stream is still live
verbose: Show chat previews
Returns:
@ -485,6 +505,7 @@ class ContentDownloader:
self.chat_thread_success = self.download_live_chat_with_chat_downloader(
username, json_path,
shutdown_check=shutdown_check,
stream_monitor=stream_monitor,
verbose=verbose
)
except Exception as e: