updating and refactoring codebase for improved performance and maintainability
This commit is contained in:
+28
-31
@@ -14,14 +14,20 @@ import sys
|
||||
|
||||
from SYS.logger import log, debug
|
||||
|
||||
from plugins.metadata_provider import (
|
||||
get_default_subject_scrape_plugin,
|
||||
get_metadata_plugin,
|
||||
get_metadata_plugin_for_url,
|
||||
list_metadata_plugins,
|
||||
scrape_isbn_metadata,
|
||||
scrape_openlibrary_metadata,
|
||||
)
|
||||
# plugins.metadata_provider is deferred: it transitively loads yt_dlp, Cryptodome,
|
||||
# imdbinfo, musicbrainzngs and ~1400 modules (~1.5s). Import lazily on first use.
|
||||
_METADATA_PROVIDER_MOD: Optional[Any] = None
|
||||
|
||||
|
||||
def _mp() -> Any:
|
||||
"""Return the (lazily imported) plugins.metadata_provider module."""
|
||||
global _METADATA_PROVIDER_MOD
|
||||
if _METADATA_PROVIDER_MOD is None:
|
||||
import plugins.metadata_provider as _m
|
||||
_METADATA_PROVIDER_MOD = _m
|
||||
return _METADATA_PROVIDER_MOD
|
||||
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional, Sequence, Tuple
|
||||
|
||||
@@ -41,11 +47,6 @@ CmdletArg = sh.CmdletArg
|
||||
SharedArgs = sh.SharedArgs
|
||||
parse_cmdlet_args = sh.parse_cmdlet_args
|
||||
|
||||
try:
|
||||
from SYS.metadata import extract_title
|
||||
except ImportError:
|
||||
extract_title = None
|
||||
|
||||
|
||||
def _dedup_tags_preserve_order(tags: List[str]) -> List[str]:
|
||||
"""Deduplicate tags case-insensitively while preserving order."""
|
||||
@@ -210,7 +211,7 @@ def _extract_tag_value(tags_list: List[str], namespace: str) -> Optional[str]:
|
||||
|
||||
def _scrape_openlibrary_metadata(olid: str) -> List[str]:
|
||||
try:
|
||||
return list(scrape_openlibrary_metadata(olid))
|
||||
return list(_mp().scrape_openlibrary_metadata(olid))
|
||||
except Exception as e:
|
||||
log(f"OpenLibrary scraping error: {e}", file=sys.stderr)
|
||||
return []
|
||||
@@ -218,7 +219,7 @@ def _scrape_openlibrary_metadata(olid: str) -> List[str]:
|
||||
|
||||
def _scrape_isbn_metadata(isbn: str) -> List[str]:
|
||||
try:
|
||||
return list(scrape_isbn_metadata(isbn))
|
||||
return list(_mp().scrape_isbn_metadata(isbn))
|
||||
except Exception as e:
|
||||
log(f"ISBN scraping error: {e}", file=sys.stderr)
|
||||
return []
|
||||
@@ -400,7 +401,7 @@ def _run_impl(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
scrape_target = str(scrape_url or "").strip() if scrape_url is not None else ""
|
||||
plugin = None
|
||||
if scrape_target.startswith(("http://", "https://")):
|
||||
plugin = get_metadata_plugin_for_url(scrape_target, config)
|
||||
plugin = _mp().get_metadata_plugin_for_url(scrape_target, config)
|
||||
if plugin is None:
|
||||
log("No metadata plugin can scrape this URL", file=sys.stderr)
|
||||
return 1
|
||||
@@ -412,9 +413,9 @@ def _run_impl(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
return 0
|
||||
|
||||
if scrape_target:
|
||||
plugin = get_metadata_plugin(scrape_target, config)
|
||||
plugin = _mp().get_metadata_plugin(scrape_target, config)
|
||||
else:
|
||||
plugin = get_default_subject_scrape_plugin(config)
|
||||
plugin = _mp().get_default_subject_scrape_plugin(config)
|
||||
if plugin is None:
|
||||
if scrape_target:
|
||||
log(f"Unknown metadata plugin: {scrape_target}", file=sys.stderr)
|
||||
@@ -749,7 +750,7 @@ def _run_impl(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
)
|
||||
return 0
|
||||
|
||||
plugin_for_apply = get_metadata_plugin(str(result_provider), config)
|
||||
plugin_for_apply = _mp().get_metadata_plugin(str(result_provider), config)
|
||||
if plugin_for_apply is not None:
|
||||
apply_tags = plugin_for_apply.filter_tags_for_store_apply(
|
||||
[str(t) for t in result_tags if t is not None]
|
||||
@@ -944,18 +945,14 @@ def _run_impl(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
_SCRAPE_CHOICES = []
|
||||
try:
|
||||
_SCRAPE_CHOICES = sorted(list_metadata_plugins().keys())
|
||||
except Exception:
|
||||
_SCRAPE_CHOICES = [
|
||||
"itunes",
|
||||
"openlibrary",
|
||||
"googlebooks",
|
||||
"google",
|
||||
"musicbrainz",
|
||||
"imdb",
|
||||
]
|
||||
_SCRAPE_CHOICES = [
|
||||
"itunes",
|
||||
"openlibrary",
|
||||
"googlebooks",
|
||||
"google",
|
||||
"musicbrainz",
|
||||
"imdb",
|
||||
]
|
||||
|
||||
|
||||
class Get_Tag(Cmdlet):
|
||||
|
||||
Reference in New Issue
Block a user