55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
from src.Library import *
|
|
|
|
from pathlib import Path
|
|
import json
|
|
|
|
out = {
|
|
"local_count": 0,
|
|
"resolved_count": 0,
|
|
"unresolved_tracks": [],
|
|
}
|
|
|
|
def get_mapping(mapping_path) -> dict[str, Any]:
|
|
data = {}
|
|
|
|
with Path(mapping_path).open("r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
out = {}
|
|
|
|
for local, remotes in data.items():
|
|
if len(remotes["items"]):
|
|
out[local] = remotes["items"][0]
|
|
|
|
return out
|
|
|
|
|
|
def find_add_local_paths(mapping_path: Path, local_lib: Library, remote_lib: Library):
|
|
mapping = get_mapping(mapping_path)
|
|
|
|
out["resolved_count"] = len(mapping)
|
|
|
|
remote_id_map = {track.id: track for track in remote_lib.tracks}
|
|
local_id_map = {track.id: track for track in local_lib.tracks}
|
|
|
|
for local, remote in mapping.items():
|
|
remote_id_map[remote].local_path = local_id_map[local].local_path
|
|
|
|
|
|
def generate_stats(local_lib: Library, remote_lib: Library, output_path : Path):
|
|
|
|
out["local_count"] = len(local_lib.tracks)
|
|
|
|
for track in remote_lib.top_tracks:
|
|
if not track.local_path:
|
|
out["unresolved_tracks"].append(
|
|
f"{track.title} - {track.artists[0].title if len(track.artists) else ""} - {track.album.title}")
|
|
|
|
|
|
with output_path.open("w", encoding="utf-8") as f:
|
|
json.dump(out, f, indent=2, ensure_ascii=False)
|
|
|
|
|
|
def log_stats(mapping_path : Path, output_path : Path, local_lib: Library, remote_lib: Library):
|
|
find_add_local_paths(mapping_path, local_lib, remote_lib)
|
|
generate_stats(local_lib, remote_lib, output_path)
|