mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-10 21:03:01 +00:00
Refactor YouTubeDisplay to use shared DisplayManager and follow consistent display pattern
This commit is contained in:
@@ -35,7 +35,7 @@ class DisplayController:
|
||||
self.stocks = StockManager(self.config, self.display_manager) if self.config.get('stocks', {}).get('enabled', False) else None
|
||||
self.news = StockNewsManager(self.config, self.display_manager) if self.config.get('stock_news', {}).get('enabled', False) else None
|
||||
self.calendar = CalendarManager(self.display_manager.matrix, self.display_manager.current_canvas, self.config) if self.config.get('calendar', {}).get('enabled', False) else None
|
||||
self.youtube = YouTubeDisplay() if self.config.get('youtube', {}).get('enabled', False) else None
|
||||
self.youtube = YouTubeDisplay(self.display_manager, self.config_manager.config_path, self.config_manager.secrets_path) if self.config.get('youtube', {}).get('enabled', False) else None
|
||||
logger.info(f"Calendar Manager initialized: {'Object' if self.calendar else 'None'}")
|
||||
logger.info("Display modes initialized in %.3f seconds", time.time() - init_time)
|
||||
|
||||
@@ -142,7 +142,7 @@ class DisplayController:
|
||||
if self.stocks: self.stocks.update_stock_data()
|
||||
if self.news: self.news.update_news_data()
|
||||
if self.calendar: self.calendar.update(time.time())
|
||||
if self.youtube: self.youtube.run()
|
||||
if self.youtube: self.youtube.update()
|
||||
|
||||
# Update NHL managers
|
||||
if self.nhl_live: self.nhl_live.update()
|
||||
@@ -385,7 +385,7 @@ class DisplayController:
|
||||
self.nba_upcoming.display(force_clear=self.force_clear)
|
||||
|
||||
elif self.current_display_mode == 'youtube' and self.youtube:
|
||||
self.youtube.display()
|
||||
self.youtube.display(force_clear=self.force_clear)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error updating display for mode {self.current_display_mode}: {e}", exc_info=True)
|
||||
|
||||
@@ -1,19 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
import time
|
||||
import logging
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import requests
|
||||
from rgbmatrix import RGBMatrix, RGBMatrixOptions
|
||||
import os
|
||||
from typing import Dict, Any
|
||||
|
||||
# Get logger without configuring
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class YouTubeDisplay:
|
||||
def __init__(self, config_path='config/config.json', secrets_path='config/config_secrets.json'):
|
||||
def __init__(self, display_manager, config_path='config/config.json', secrets_path='config/config_secrets.json'):
|
||||
self.config = self._load_config(config_path)
|
||||
self.secrets = self._load_config(secrets_path)
|
||||
self.matrix = self._setup_matrix()
|
||||
self.canvas = self.matrix.CreateFrameCanvas()
|
||||
self.font = ImageFont.truetype("assets/fonts/PressStart2P-Regular.ttf", 8)
|
||||
self.youtube_logo = Image.open("assets/youtube_logo.png")
|
||||
self.display_manager = display_manager
|
||||
self.last_update = 0
|
||||
self.update_interval = self.config.get('youtube', {}).get('update_interval', 300) # Default 5 minutes
|
||||
self.channel_stats = None
|
||||
|
||||
def _load_config(self, config_path):
|
||||
with open(config_path, 'r') as f:
|
||||
@@ -53,7 +62,7 @@ class YouTubeDisplay:
|
||||
'views': int(channel['statistics']['viewCount'])
|
||||
}
|
||||
except Exception as e:
|
||||
print(f"Error fetching YouTube stats: {e}")
|
||||
logger.error(f"Error fetching YouTube stats: {e}")
|
||||
return None
|
||||
|
||||
def _create_display(self, channel_stats):
|
||||
@@ -91,19 +100,30 @@ class YouTubeDisplay:
|
||||
|
||||
return image
|
||||
|
||||
def run(self):
|
||||
def update(self):
|
||||
"""Update YouTube channel stats if needed."""
|
||||
current_time = time.time()
|
||||
if current_time - self.last_update >= self.update_interval:
|
||||
channel_id = self.secrets['youtube']['channel_id']
|
||||
self.channel_stats = self._get_channel_stats(channel_id)
|
||||
self.last_update = current_time
|
||||
|
||||
def display(self, force_clear: bool = False):
|
||||
"""Display YouTube channel stats."""
|
||||
if not self.config.get('youtube', {}).get('enabled', False):
|
||||
return
|
||||
|
||||
channel_id = self.secrets['youtube']['channel_id']
|
||||
duration = self.config['display']['display_durations']['youtube']
|
||||
channel_stats = self._get_channel_stats(channel_id)
|
||||
|
||||
if channel_stats:
|
||||
display_image = self._create_display(channel_stats)
|
||||
if not self.channel_stats:
|
||||
self.update()
|
||||
|
||||
if self.channel_stats:
|
||||
if force_clear:
|
||||
self.matrix.Clear()
|
||||
|
||||
display_image = self._create_display(self.channel_stats)
|
||||
self.canvas.SetImage(display_image)
|
||||
self.matrix.SwapOnVSync(self.canvas)
|
||||
time.sleep(duration)
|
||||
time.sleep(self.update_interval)
|
||||
|
||||
def cleanup(self):
|
||||
self.matrix.Clear()
|
||||
@@ -111,5 +131,5 @@ class YouTubeDisplay:
|
||||
if __name__ == "__main__":
|
||||
# Example usage
|
||||
youtube_display = YouTubeDisplay()
|
||||
youtube_display.run()
|
||||
youtube_display.display()
|
||||
youtube_display.cleanup()
|
||||
Reference in New Issue
Block a user