|
12 | 12 | import os |
13 | 13 | from io import BytesIO |
14 | 14 | from pathlib import Path |
15 | | -from typing import Dict |
16 | | -from urllib.parse import urlparse |
| 15 | +from typing import Dict, Literal |
| 16 | +from urllib.parse import parse_qs, urlencode, urlparse, urlunparse |
17 | 17 |
|
18 | 18 | import google_play_scraper |
19 | 19 | import httpx |
|
27 | 27 | CACHE_ROOT = "external_cache" |
28 | 28 | ICON_SUBDIR = "icons" |
29 | 29 | ICON_SIZE = (240, 240) |
| 30 | +IMAGE_SIZE_MAX = 480 |
30 | 31 |
|
31 | 32 |
|
32 | 33 | # Paths |
@@ -171,8 +172,12 @@ def load_and_encode() -> str: |
171 | 172 |
|
172 | 173 |
|
173 | 174 | def _is_url(path: str) -> bool: |
174 | | - parsed = urlparse(path) |
175 | | - return parsed.scheme in ("http", "https") |
| 175 | + """Check if the provided string is a valid http/https URL.""" |
| 176 | + try: |
| 177 | + parsed = urlparse(path) |
| 178 | + return parsed.scheme in ("http", "https") and bool(parsed.netloc) |
| 179 | + except (ValueError, AttributeError): |
| 180 | + return False |
176 | 181 |
|
177 | 182 |
|
178 | 183 | async def _fetch_google_play_metadata(package_id: str) -> Dict[str, str] | None: |
@@ -229,3 +234,72 @@ async def get_app_metadata(package_id: str) -> Dict[str, str]: |
229 | 234 |
|
230 | 235 | _LOG.debug("Falling back to default metadata for %s", package_id) |
231 | 236 | return {"name": package_id, "icon": ""} |
| 237 | + |
| 238 | + |
| 239 | +def get_resized_image_url(url: str, max_size: int = IMAGE_SIZE_MAX) -> str | Literal[b""]: |
| 240 | + """ |
| 241 | + Adjust width and height query parameters while maintaining aspect ratio. |
| 242 | +
|
| 243 | + Only performs the rewrite if the URL path contains '/photo/:/transcode' (Plex transcode URL). |
| 244 | + - If parameters are missing: returns original URL. |
| 245 | + - If parameters are invalid (non-numeric/<=0): returns original URL. |
| 246 | + - If one parameter exists: sets it to max_size. |
| 247 | + - If both exist: resizes proportionally to fit within max_size. |
| 248 | + """ |
| 249 | + if not url or not _is_url(url): |
| 250 | + _LOG.warning("Invalid URL provided for resizing: %s", url) |
| 251 | + return url |
| 252 | + |
| 253 | + parsed_url = urlparse(url) |
| 254 | + |
| 255 | + # Only rewrite Plex transcode URLs |
| 256 | + # Other services can be added later |
| 257 | + if "/photo/:/transcode" not in parsed_url.path: |
| 258 | + return url |
| 259 | + |
| 260 | + query_dict = parse_qs(parsed_url.query, keep_blank_values=True) |
| 261 | + |
| 262 | + width_str = query_dict.get("width", [None])[0] |
| 263 | + height_str = query_dict.get("height", [None])[0] |
| 264 | + |
| 265 | + # Helper to validate and convert numeric strings |
| 266 | + def _safe_int(val): |
| 267 | + try: |
| 268 | + res = int(val) |
| 269 | + return res if res > 0 else None |
| 270 | + except (TypeError, ValueError): |
| 271 | + return None |
| 272 | + |
| 273 | + w = _safe_int(width_str) |
| 274 | + h = _safe_int(height_str) |
| 275 | + |
| 276 | + # both parameters present |
| 277 | + if w is not None and h is not None: |
| 278 | + if w > max_size or h > max_size: |
| 279 | + if w > h: |
| 280 | + new_w = max_size |
| 281 | + new_h = max(1, int(h * (max_size / w))) |
| 282 | + else: |
| 283 | + new_h = max_size |
| 284 | + new_w = max(1, int(w * (max_size / h))) |
| 285 | + |
| 286 | + query_dict["width"] = [str(new_w)] |
| 287 | + query_dict["height"] = [str(new_h)] |
| 288 | + else: |
| 289 | + return url # No resizing needed |
| 290 | + |
| 291 | + # only width present |
| 292 | + elif w is not None: |
| 293 | + query_dict["width"] = [str(max_size)] |
| 294 | + |
| 295 | + # only height present |
| 296 | + elif h is not None: |
| 297 | + query_dict["height"] = [str(max_size)] |
| 298 | + |
| 299 | + # If neither are valid/present, return original |
| 300 | + else: |
| 301 | + return url |
| 302 | + |
| 303 | + # Reconstruct the URL safely |
| 304 | + new_query = urlencode(query_dict, doseq=True) |
| 305 | + return urlunparse(parsed_url._replace(query=new_query)) |
0 commit comments