feat(src/window/controlcenter): migrate artwork caching to use httpx
- New Feature: Replace urllib.request with httpx for fetching artwork URLs, providing timeout and redirect support. - Enhancement: Added proper HTTP status code handling and early exit on download cancellation. - Bug Fix: Gracefully handle non‑200 responses and prevent resource leaks during network operations.
This commit is contained in:
1 file changed
+14
-8
@@ -4,7 +4,7 @@ import re
|
|||||||
import tempfile
|
import tempfile
|
||||||
import threading
|
import threading
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import urllib.request
|
import httpx
|
||||||
import weakref
|
import weakref
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
@@ -123,9 +123,12 @@ def get_artwork_cached(url: str) -> Optional[str]:
|
|||||||
return cache_file
|
return cache_file
|
||||||
|
|
||||||
# Download and cache
|
# Download and cache
|
||||||
urllib.request.urlretrieve(url, cache_file)
|
response = httpx.get(url, timeout=10, follow_redirects=True)
|
||||||
_artwork_cache[url] = cache_file
|
if response.status_code == 200:
|
||||||
return cache_file
|
with open(cache_file, "wb") as f:
|
||||||
|
f.write(response.content)
|
||||||
|
_artwork_cache[url] = cache_file
|
||||||
|
return cache_file
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to cache artwork from {url}: {e}")
|
logger.warning(f"Failed to cache artwork from {url}: {e}")
|
||||||
@@ -1246,10 +1249,13 @@ class PlayerBox(Box):
|
|||||||
parsed = urllib.parse.urlparse(arturl)
|
parsed = urllib.parse.urlparse(arturl)
|
||||||
suffix = os.path.splitext(parsed.path)[1] or ".png"
|
suffix = os.path.splitext(parsed.path)[1] or ".png"
|
||||||
|
|
||||||
with urllib.request.urlopen(arturl, timeout=10) as response: # Add timeout
|
response = httpx.get(arturl, timeout=10, follow_redirects=True)
|
||||||
if self._download_cancelled:
|
if self._download_cancelled:
|
||||||
return
|
return
|
||||||
data = response.read()
|
if response.status_code == 200:
|
||||||
|
data = response.content
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
# Check one more time if cancelled
|
# Check one more time if cancelled
|
||||||
if self._download_cancelled:
|
if self._download_cancelled:
|
||||||
|
|||||||
Reference in new issue
Block a user