Enhance project structure and add new files for enrichment and analysis
- Updated AGENTS.md to reflect changes in CLI commands and module organization, including the addition of an enrichment step and new functional modules. - Introduced analysis.json, identities.json, inventory.csv, and plan.json to support enriched metadata and execution planning. - Added CODE_IMPROVEMENTS.md to document identified code issues and proposed solutions for future enhancements. - Updated README.md to include new enrichment features and configuration options. - Removed unused dependency on ffmpeg-python from pyproject.toml. These changes improve the overall functionality and maintainability of the Video Library Manager project.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
"""CLI command implementations.
|
||||
|
||||
Each module provides *_cmd(ctx, ...) functions that are invoked by cli.py
|
||||
after Click parses options and passes context.
|
||||
"""
|
||||
@@ -0,0 +1,116 @@
|
||||
"""Analyze command implementation."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import click
|
||||
|
||||
from vlm.analysis import analyze_series_completeness, detect_duplicates
|
||||
from vlm.context import CLIContext
|
||||
from vlm.io import identities_to_analysis_input, load_identities_json, load_inventory_csv
|
||||
from vlm.models import MovieIdentity, SeriesIdentity
|
||||
from vlm.utils import utc_now
|
||||
|
||||
|
||||
def analyze_cmd(
|
||||
ctx: CLIContext,
|
||||
input: Path,
|
||||
output: Path,
|
||||
inventory: Optional[Path],
|
||||
) -> None:
|
||||
"""Run analysis: completeness and duplicate detection."""
|
||||
config = ctx.config
|
||||
logger = ctx.logger
|
||||
|
||||
click.echo(f"Analyzing identities from: {input}")
|
||||
if inventory:
|
||||
click.echo(f"Merging metadata from inventory: {inventory}")
|
||||
click.echo()
|
||||
|
||||
identities_data = load_identities_json(input)
|
||||
movies_data = identities_data.get("movies", [])
|
||||
series_data = identities_data.get("series", [])
|
||||
|
||||
click.echo(f"Loaded {len(movies_data)} movies and {len(series_data)} series")
|
||||
click.echo()
|
||||
|
||||
inventory_files = load_inventory_csv(inventory) if inventory else None
|
||||
movie_identities, series_identities, video_files = identities_to_analysis_input(
|
||||
identities_data, inventory_files=inventory_files
|
||||
)
|
||||
|
||||
click.echo("Analyzing series completeness...")
|
||||
completeness_results = analyze_series_completeness(series_identities)
|
||||
|
||||
click.echo("Detecting duplicates...")
|
||||
n_movies = len(movie_identities)
|
||||
identity_file_pairs = (
|
||||
list(zip(movie_identities, video_files[:n_movies]))
|
||||
+ list(zip(series_identities, video_files[n_movies:]))
|
||||
)
|
||||
duplicate_groups = detect_duplicates(identity_file_pairs)
|
||||
|
||||
click.echo()
|
||||
click.echo("Analysis complete!")
|
||||
click.echo()
|
||||
click.echo("Results:")
|
||||
click.echo(f" Series with episode gaps: {len(completeness_results)}")
|
||||
if completeness_results:
|
||||
total_missing = sum(len(c.episodes_missing) for c in completeness_results)
|
||||
click.echo(f" - Total missing episodes: {total_missing}")
|
||||
click.echo(f" Duplicate groups found: {len(duplicate_groups)}")
|
||||
if duplicate_groups:
|
||||
total_duplicates = sum(len(g.files) for g in duplicate_groups)
|
||||
click.echo(f" - Total duplicate files: {total_duplicates}")
|
||||
|
||||
click.echo()
|
||||
click.echo(f"Saving analysis results to: {output}")
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
generation_timestamp = utc_now().strftime("%Y-%m-%dT%H:%M:%S")
|
||||
completeness_list = [
|
||||
{
|
||||
"series_title": c.series_title,
|
||||
"season": c.season,
|
||||
"episodes_found": c.episodes_found,
|
||||
"episodes_missing": c.episodes_missing,
|
||||
}
|
||||
for c in completeness_results
|
||||
]
|
||||
duplicates_list = []
|
||||
for d in duplicate_groups:
|
||||
if isinstance(d.identity, MovieIdentity):
|
||||
identity_info = {"type": "movie", "title": d.identity.title, "year": d.identity.year}
|
||||
else:
|
||||
identity_info = {
|
||||
"type": "series",
|
||||
"title": d.identity.title,
|
||||
"season": d.identity.season,
|
||||
"episodes": d.identity.episodes,
|
||||
}
|
||||
duplicates_list.append(
|
||||
{
|
||||
"identity": identity_info,
|
||||
"files": [str(f.path) for f in d.files],
|
||||
"quality_comparison": d.quality_comparison,
|
||||
}
|
||||
)
|
||||
analysis_data = {
|
||||
"metadata": {
|
||||
"generated": generation_timestamp,
|
||||
"source_identities": str(input),
|
||||
"total_movies": len(movies_data),
|
||||
"total_series": len(series_data),
|
||||
},
|
||||
"completeness": completeness_list,
|
||||
"duplicates": duplicates_list,
|
||||
}
|
||||
with open(output, "w", encoding="utf-8") as jsonfile:
|
||||
json.dump(analysis_data, jsonfile, indent=2, ensure_ascii=False)
|
||||
|
||||
click.echo("Analysis results saved successfully!")
|
||||
logger.info(
|
||||
f"Analysis completed: {len(completeness_results)} incomplete series, "
|
||||
f"{len(duplicate_groups)} duplicate groups, saved to {output}"
|
||||
)
|
||||
@@ -0,0 +1,69 @@
|
||||
"""Plan command implementation."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
|
||||
from vlm.context import CLIContext
|
||||
from vlm.io import identities_to_plan_input, load_identities_json
|
||||
from vlm.planner import generate_plan, save_plan
|
||||
|
||||
|
||||
def plan_cmd(ctx: CLIContext, input: Path, output: Path) -> None:
|
||||
"""Generate execution plan from identities."""
|
||||
config = ctx.config
|
||||
logger = ctx.logger
|
||||
|
||||
click.echo(f"Generating execution plan from: {input}")
|
||||
click.echo()
|
||||
|
||||
identities_data = load_identities_json(input)
|
||||
movies_data = identities_data.get("movies", [])
|
||||
series_data = identities_data.get("series", [])
|
||||
anime_data = identities_data.get("anime", [])
|
||||
other_data = identities_data.get("other", [])
|
||||
|
||||
click.echo(
|
||||
f"Loaded {len(movies_data)} movies, {len(series_data)} series, "
|
||||
f"{len(anime_data)} anime, {len(other_data)} other"
|
||||
)
|
||||
click.echo()
|
||||
|
||||
identities_list = identities_to_plan_input(identities_data)
|
||||
|
||||
click.echo("Generating execution plan...")
|
||||
execution_plan = generate_plan(identities_list, config)
|
||||
|
||||
click.echo()
|
||||
click.echo("Plan generation complete!")
|
||||
click.echo()
|
||||
click.echo("Operation summary:")
|
||||
click.echo(f" Total operations: {execution_plan.summary['total']}")
|
||||
click.echo(f" Move operations: {execution_plan.summary['move']}")
|
||||
click.echo(f" Rename operations: {execution_plan.summary['rename']}")
|
||||
click.echo(f" Quarantine operations: {execution_plan.summary['quarantine']}")
|
||||
click.echo(f" No-op (skipped): {execution_plan.summary['no-op']}")
|
||||
|
||||
conflicts = sum(1 for op in execution_plan.operations if op.has_conflict)
|
||||
if conflicts > 0:
|
||||
click.echo()
|
||||
click.echo(f" ⚠️ Conflicts detected: {conflicts}")
|
||||
click.echo(" Review the plan file for details on conflicting operations.")
|
||||
|
||||
click.echo()
|
||||
click.echo(f"Saving execution plan to: {output}")
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
save_plan(execution_plan, output)
|
||||
|
||||
click.echo("Execution plan saved successfully!")
|
||||
click.echo()
|
||||
click.echo("Next steps:")
|
||||
click.echo(f" 1. Review the plan: {output}")
|
||||
click.echo(" 2. Edit the plan if needed (it's JSON)")
|
||||
click.echo(f" 3. Dry-run: vlm execute --plan {output}")
|
||||
click.echo(f" 4. Execute: vlm execute --plan {output} --confirm")
|
||||
|
||||
logger.info(
|
||||
f"Plan generated: {execution_plan.summary['total']} operations, "
|
||||
f"{conflicts} conflicts, saved to {output}"
|
||||
)
|
||||
@@ -0,0 +1,97 @@
|
||||
"""Scan command implementation."""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import click
|
||||
|
||||
from vlm.context import CLIContext
|
||||
from vlm.scanner import load_inventory_csv, save_inventory_csv, scan_library
|
||||
from vlm.utils import format_size
|
||||
|
||||
|
||||
def scan_cmd(
|
||||
ctx: CLIContext,
|
||||
output: Path,
|
||||
metadata: bool,
|
||||
reuse_from: Optional[Path],
|
||||
force_refresh_metadata: bool,
|
||||
) -> None:
|
||||
"""Run scan: discover video files and save inventory."""
|
||||
config = ctx.config
|
||||
logger = ctx.logger
|
||||
|
||||
click.echo(f"Scanning library at: {config.library_root}")
|
||||
click.echo("This may take a while for large libraries...")
|
||||
click.echo()
|
||||
|
||||
progress_state: dict[str, Optional[click.ProgressBar]] = {"bar": None}
|
||||
progress_position = {"current": 0}
|
||||
|
||||
def _scan_progress(processed: int, total: int) -> None:
|
||||
if total <= 0:
|
||||
return
|
||||
if progress_state["bar"] is None:
|
||||
bar = click.progressbar(
|
||||
length=total,
|
||||
label="Scanning files",
|
||||
show_pos=True,
|
||||
)
|
||||
progress_state["bar"] = bar.__enter__()
|
||||
step = processed - progress_position["current"]
|
||||
if step > 0 and progress_state["bar"] is not None:
|
||||
progress_state["bar"].update(step)
|
||||
progress_position["current"] = processed
|
||||
|
||||
metadata_cache = None
|
||||
cache_source = None
|
||||
if not force_refresh_metadata:
|
||||
cache_source = reuse_from if reuse_from is not None else (output if output.exists() else None)
|
||||
|
||||
if metadata and force_refresh_metadata:
|
||||
click.echo("Forcing metadata refresh for all files (cache disabled).")
|
||||
click.echo()
|
||||
|
||||
if metadata and cache_source is not None:
|
||||
click.echo(f"Loading metadata cache from: {cache_source}")
|
||||
try:
|
||||
cached_files = load_inventory_csv(cache_source)
|
||||
metadata_cache = {str(vf.path): vf for vf in cached_files}
|
||||
click.echo(f"Loaded metadata cache entries: {len(metadata_cache)}")
|
||||
click.echo()
|
||||
except Exception as e:
|
||||
click.echo(f"Warning: could not load metadata cache: {e}")
|
||||
click.echo("Continuing without cache.")
|
||||
click.echo()
|
||||
|
||||
try:
|
||||
video_files = scan_library(
|
||||
config.library_root,
|
||||
config,
|
||||
progress_callback=_scan_progress,
|
||||
include_video_metadata=metadata,
|
||||
metadata_cache=metadata_cache,
|
||||
)
|
||||
finally:
|
||||
if progress_state["bar"] is not None:
|
||||
progress_state["bar"].__exit__(None, None, None)
|
||||
click.echo()
|
||||
|
||||
click.echo("Scan complete!")
|
||||
click.echo(f" Total files found: {len(video_files)}")
|
||||
categories = {}
|
||||
total_size = 0
|
||||
for vf in video_files:
|
||||
categories[vf.category] = categories.get(vf.category, 0) + 1
|
||||
total_size += vf.size_bytes
|
||||
click.echo(f" Total size: {format_size(total_size)}")
|
||||
click.echo()
|
||||
click.echo("Files by category:")
|
||||
for category in sorted(categories.keys()):
|
||||
click.echo(f" {category}: {categories[category]}")
|
||||
click.echo()
|
||||
click.echo(f"Saving inventory to: {output}")
|
||||
save_inventory_csv(video_files, output, config.library_root)
|
||||
click.echo("Inventory saved successfully!")
|
||||
logger.info(f"Scan completed: {len(video_files)} files found, saved to {output}")
|
||||
Reference in New Issue
Block a user