-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_files.py
More file actions
92 lines (76 loc) · 2.94 KB
/
Copy pathmerge_files.py
File metadata and controls
92 lines (76 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
import argparse
import json
from pathlib import Path
from typing import List, Union
JSONType = Union[dict, list]
def load_json(path: Path) -> JSONType:
try:
with path.open("r", encoding="utf-8") as f:
return json.load(f)
except Exception as e:
print(f"Failed to load {path}: {e}")
return {} if path.suffix == ".json" else []
def save_json(path: Path, data: JSONType):
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
def merge_dicts(d1: dict, d2: dict) -> dict:
for k, v in d2.items():
if k in d1:
if isinstance(d1[k], dict) and isinstance(v, dict):
d1[k] = merge_dicts(d1[k], v)
elif isinstance(d1[k], list) and isinstance(v, list):
d1[k] = list(dict.fromkeys(d1[k] + v))
else:
d1[k] = v
else:
d1[k] = v
return d1
def collect_files(input_dir: Path, pattern: str) -> List[Path]:
"""Recursively collect all matching files from input_dir and subfolders."""
return list(input_dir.rglob(pattern))
def merge_root_files(input_dir: Path, repo_root: Path):
"""Merge all mapped/unmapped files across all scraper artifacts."""
patterns = [
"mapped-tvdb-ids-movie.json",
"mapped-tvdb-ids-series.json",
"mapped-tvdb-ids-seasons.json",
"mapped-tvdb-ids-episodes.json",
"unmapped-series.json",
"unmapped-seasons.json",
"unmapped-episodes.json",
]
for pattern in patterns:
files = collect_files(input_dir, pattern)
repo_file = repo_root / pattern
if repo_file.exists():
files.append(repo_file)
if not files:
print(f"No files found for pattern {pattern}")
continue
data_list = [load_json(f) for f in files]
merged_dict = {}
for data in data_list:
if isinstance(data, list):
for entry in data:
tvdb_id = entry.get("thetvdb") or entry.get("TvdbId")
if tvdb_id is not None:
try:
tvdb_id = int(tvdb_id)
except ValueError:
continue
merged_dict[tvdb_id] = entry
target_file = repo_root / pattern
# Sort by TVDB ID for reproducibility
save_json(target_file, [merged_dict[k] for k in sorted(merged_dict.keys())])
print(f"Merged {len(files)} files into {target_file}")
def main():
parser = argparse.ArgumentParser(description="Merge JSON files from multiple artifacts.")
parser.add_argument("--input-dir", type=Path, required=True)
args = parser.parse_args()
input_dir = args.input_dir
repo_root = Path.cwd()
merge_root_files(input_dir, repo_root)
if __name__ == "__main__":
main()