Update stock_manager.py

Assume folder exists
This commit is contained in:
Chuck
2025-04-08 21:15:36 -05:00
parent db31864a58
commit 04cfa86430

View File

@@ -22,9 +22,8 @@ class StockManager:
self.base_url = "https://query2.finance.yahoo.com" self.base_url = "https://query2.finance.yahoo.com"
self.logo_cache = {} self.logo_cache = {}
# Create logos directory if it doesn't exist # Set logos directory path (assuming it exists)
self.logos_dir = "assets/logos/stocks" self.logos_dir = "assets/logos/stocks"
os.makedirs(self.logos_dir, exist_ok=True)
# Default colors for stocks # Default colors for stocks
self.default_colors = [ self.default_colors = [
@@ -98,38 +97,36 @@ class StockManager:
try: try:
# Try to get logo from Yahoo Finance # Try to get logo from Yahoo Finance
url = f"{self.base_url}/v8/finance/chart/{symbol}" url = f"https://query2.finance.yahoo.com/v7/finance/options/{symbol}"
params = { response = requests.get(url)
"interval": "1d",
"period": "1d"
}
response = requests.get(url, params=params)
data = response.json() data = response.json()
if "chart" in data and "result" in data["chart"] and data["chart"]["result"]: if "optionChain" in data and "result" in data["optionChain"] and data["optionChain"]["result"]:
result = data["chart"]["result"][0] result = data["optionChain"]["result"][0]
if "meta" in result and "logo_url" in result["meta"]: if "quote" in result and "logoUrl" in result["quote"]:
logo_url = result["meta"]["logo_url"] logo_url = result["quote"]["logoUrl"]
# Download the logo # Download the logo
logo_response = requests.get(logo_url) logo_response = requests.get(logo_url)
if logo_response.status_code == 200: if logo_response.status_code == 200:
with open(logo_path, "wb") as f: try:
f.write(logo_response.content) with open(logo_path, "wb") as f:
logger.info(f"Downloaded logo for {symbol}") f.write(logo_response.content)
return logo_path logger.info(f"Downloaded logo for {symbol}")
return logo_path
except IOError as e:
logger.error(f"Could not write logo file for {symbol}: {e}")
return None
# If we couldn't get a logo, create a placeholder # If we couldn't get a logo, create a placeholder
self._create_placeholder_logo(symbol, logo_path) return self._create_placeholder_logo(symbol, logo_path)
return logo_path
except Exception as e: except Exception as e:
logger.error(f"Error downloading logo for {symbol}: {e}") logger.error(f"Error downloading logo for {symbol}: {e}")
# Create a placeholder logo # Create a placeholder logo
self._create_placeholder_logo(symbol, logo_path) return self._create_placeholder_logo(symbol, logo_path)
return logo_path
def _create_placeholder_logo(self, symbol: str, logo_path: str): def _create_placeholder_logo(self, symbol: str, logo_path: str) -> str:
"""Create a placeholder logo with the stock symbol.""" """Create a placeholder logo with the stock symbol."""
try: try:
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
@@ -146,14 +143,28 @@ class StockManager:
font = ImageFont.load_default() font = ImageFont.load_default()
# Draw the symbol # Draw the symbol
draw.text((4, 8), symbol, fill=(255, 255, 255), font=font) text_bbox = draw.textbbox((0, 0), symbol, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
# Save the image # Center the text
img.save(logo_path) x = (32 - text_width) // 2
logger.info(f"Created placeholder logo for {symbol}") y = (32 - text_height) // 2
draw.text((x, y), symbol, fill=(255, 255, 255), font=font)
try:
# Save the image
img.save(logo_path)
logger.info(f"Created placeholder logo for {symbol}")
return logo_path
except IOError as e:
logger.error(f"Could not save placeholder logo for {symbol}: {e}")
return None
except Exception as e: except Exception as e:
logger.error(f"Error creating placeholder logo for {symbol}: {e}") logger.error(f"Error creating placeholder logo for {symbol}: {e}")
return None
def update_stock_data(self): def update_stock_data(self):
"""Update stock data if enough time has passed.""" """Update stock data if enough time has passed."""
@@ -178,8 +189,10 @@ class StockManager:
else: else:
data['color'] = tuple(stock['color']) data['color'] = tuple(stock['color'])
# Download logo # Try to get logo
data['logo_path'] = self._download_logo(symbol) logo_path = self._download_logo(symbol)
if logo_path:
data['logo_path'] = logo_path
self.stock_data[symbol] = data self.stock_data[symbol] = data
@@ -217,7 +230,8 @@ class StockManager:
) )
# Draw the stock information # Draw the stock information
self.display_manager.draw_text(display_text, color=data['color']) color = (0, 255, 0) if data['change'] >= 0 else (255, 0, 0) # Green for up, red for down
self.display_manager.draw_text(display_text, color=color)
self.display_manager.update_display() self.display_manager.update_display()
# Move to next stock for next update # Move to next stock for next update