dfd
This commit is contained in:
@@ -6,8 +6,9 @@ This module is the single source of truth for provider discovery.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, Optional, Type
|
||||
from typing import Any, Dict, Optional, Sequence, Type
|
||||
import sys
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from SYS.logger import log
|
||||
|
||||
@@ -141,6 +142,45 @@ def list_file_providers(config: Optional[Dict[str, Any]] = None) -> Dict[str, bo
|
||||
return availability
|
||||
|
||||
|
||||
def match_provider_name_for_url(url: str) -> Optional[str]:
|
||||
"""Return a registered provider name that claims the URL's domain.
|
||||
|
||||
Providers can declare domains via a class attribute `URL_DOMAINS` (sequence of strings).
|
||||
This matcher is intentionally cheap (no provider instantiation, no network).
|
||||
"""
|
||||
|
||||
try:
|
||||
parsed = urlparse(str(url))
|
||||
host = (parsed.hostname or "").strip().lower()
|
||||
except Exception:
|
||||
host = ""
|
||||
|
||||
if not host:
|
||||
return None
|
||||
|
||||
for name, provider_class in _PROVIDERS.items():
|
||||
domains = getattr(provider_class, "URL_DOMAINS", None)
|
||||
if not isinstance(domains, (list, tuple)):
|
||||
continue
|
||||
for d in domains:
|
||||
dom = str(d or "").strip().lower()
|
||||
if not dom:
|
||||
continue
|
||||
if host == dom or host.endswith("." + dom):
|
||||
return name
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_provider_for_url(url: str, config: Optional[Dict[str, Any]] = None) -> Optional[Provider]:
|
||||
"""Instantiate and return the matching provider for a URL, if any."""
|
||||
|
||||
name = match_provider_name_for_url(url)
|
||||
if not name:
|
||||
return None
|
||||
return get_provider(name, config)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"SearchResult",
|
||||
"Provider",
|
||||
@@ -152,5 +192,7 @@ __all__ = [
|
||||
"list_search_providers",
|
||||
"get_file_provider",
|
||||
"list_file_providers",
|
||||
"match_provider_name_for_url",
|
||||
"get_provider_for_url",
|
||||
"download_soulseek_file",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user