mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-10 21:03:01 +00:00
Improve logo handling: Add in-memory fallback for permission issues
This commit is contained in:
@@ -322,18 +322,28 @@ class StockManager:
|
|||||||
# Try Yahoo first
|
# Try Yahoo first
|
||||||
response = requests.get(yahoo_url, headers=self.headers, timeout=5)
|
response = requests.get(yahoo_url, headers=self.headers, timeout=5)
|
||||||
if response.status_code == 200 and 'image' in response.headers.get('Content-Type', ''):
|
if response.status_code == 200 and 'image' in response.headers.get('Content-Type', ''):
|
||||||
with open(filepath, 'wb') as f:
|
try:
|
||||||
f.write(response.content)
|
with open(filepath, 'wb') as f:
|
||||||
logger.info(f"Downloaded logo for {symbol} from Yahoo")
|
f.write(response.content)
|
||||||
return filepath
|
logger.info(f"Downloaded logo for {symbol} from Yahoo")
|
||||||
|
return filepath
|
||||||
|
except PermissionError:
|
||||||
|
logger.warning(f"Permission denied when saving logo for {symbol}. Using in-memory logo instead.")
|
||||||
|
# Return a temporary path that won't be used for saving
|
||||||
|
return f"temp_{symbol.lower()}.png"
|
||||||
|
|
||||||
# Try alternative source
|
# Try alternative source
|
||||||
response = requests.get(alt_url, headers=self.headers, timeout=5)
|
response = requests.get(alt_url, headers=self.headers, timeout=5)
|
||||||
if response.status_code == 200 and 'image' in response.headers.get('Content-Type', ''):
|
if response.status_code == 200 and 'image' in response.headers.get('Content-Type', ''):
|
||||||
with open(filepath, 'wb') as f:
|
try:
|
||||||
f.write(response.content)
|
with open(filepath, 'wb') as f:
|
||||||
logger.info(f"Downloaded logo for {symbol} from alternative source")
|
f.write(response.content)
|
||||||
return filepath
|
logger.info(f"Downloaded logo for {symbol} from alternative source")
|
||||||
|
return filepath
|
||||||
|
except PermissionError:
|
||||||
|
logger.warning(f"Permission denied when saving logo for {symbol}. Using in-memory logo instead.")
|
||||||
|
# Return a temporary path that won't be used for saving
|
||||||
|
return f"temp_{symbol.lower()}.png"
|
||||||
|
|
||||||
logger.warning(f"Could not download logo for {symbol}")
|
logger.warning(f"Could not download logo for {symbol}")
|
||||||
return None
|
return None
|
||||||
@@ -354,10 +364,34 @@ class StockManager:
|
|||||||
# Try to download the logo if we don't have it
|
# Try to download the logo if we don't have it
|
||||||
logo_path = self._download_stock_logo(symbol)
|
logo_path = self._download_stock_logo(symbol)
|
||||||
|
|
||||||
if logo_path and os.path.exists(logo_path):
|
if logo_path:
|
||||||
try:
|
try:
|
||||||
# Open and resize the logo
|
# Check if this is a temporary path (in-memory logo)
|
||||||
logo = Image.open(logo_path)
|
if logo_path.startswith("temp_"):
|
||||||
|
# For temporary paths, we need to download the logo again
|
||||||
|
# since we couldn't save it to disk
|
||||||
|
symbol_lower = symbol.lower()
|
||||||
|
yahoo_url = f"https://logo.clearbit.com/{symbol_lower}.com"
|
||||||
|
alt_url = f"https://storage.googleapis.com/iex/api/logos/{symbol}.png"
|
||||||
|
|
||||||
|
# Try Yahoo first
|
||||||
|
response = requests.get(yahoo_url, headers=self.headers, timeout=5)
|
||||||
|
if response.status_code == 200 and 'image' in response.headers.get('Content-Type', ''):
|
||||||
|
# Create image from response content
|
||||||
|
from io import BytesIO
|
||||||
|
logo = Image.open(BytesIO(response.content))
|
||||||
|
else:
|
||||||
|
# Try alternative source
|
||||||
|
response = requests.get(alt_url, headers=self.headers, timeout=5)
|
||||||
|
if response.status_code == 200 and 'image' in response.headers.get('Content-Type', ''):
|
||||||
|
# Create image from response content
|
||||||
|
from io import BytesIO
|
||||||
|
logo = Image.open(BytesIO(response.content))
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
# Normal case: open the saved logo file
|
||||||
|
logo = Image.open(logo_path)
|
||||||
|
|
||||||
# Convert to RGBA if not already
|
# Convert to RGBA if not already
|
||||||
if logo.mode != 'RGBA':
|
if logo.mode != 'RGBA':
|
||||||
|
|||||||
Reference in New Issue
Block a user