63 lines
2.0 KiB
Python
Executable File
63 lines
2.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Replace JRiver date imported fields with date modified when the latter is earlier.
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def get_import_date(line: str) -> int:
|
|
"""Extract and return the date imported value from a date imported field."""
|
|
date = line.lstrip('<Field Name="Date Imported">').rstrip('</Field>\n')
|
|
return int(date)
|
|
|
|
|
|
def get_create_date(line: str) -> int:
|
|
"""Extract and return the date modified value from a date modified field."""
|
|
date = line.lstrip('<Field Name="Date Modified">').rstrip('</Field>\n')
|
|
return int(date)
|
|
|
|
|
|
def main() -> None:
|
|
"""Main function to process JRiver library file."""
|
|
if len(sys.argv) != 3:
|
|
print("Usage: jriver-replace-date-imported <input_file> <output_file>", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
in_file = Path(sys.argv[1])
|
|
out_file = Path(sys.argv[2])
|
|
|
|
if not in_file.exists():
|
|
print(f"[ERROR] Input file '{in_file}' does not exist.", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
# Read input file
|
|
with open(in_file, "r", encoding="utf-8") as f:
|
|
lines = f.readlines()
|
|
|
|
# Process lines and replace dates where appropriate
|
|
import_date: int = 0
|
|
date_imported_line: int = 0
|
|
replacements = 0
|
|
|
|
for lnum, line in enumerate(lines):
|
|
if '<Field Name="Date Imported">' in line:
|
|
import_date = get_import_date(line)
|
|
date_imported_line = lnum
|
|
elif '<Field Name="Date Modified">' in line:
|
|
create_date = get_create_date(line)
|
|
if create_date < import_date:
|
|
print(f"[INFO] Replacing {import_date} with {create_date} at line {date_imported_line}")
|
|
lines[date_imported_line] = f'<Field Name="Date Imported">{create_date}</Field>\n'
|
|
replacements += 1
|
|
|
|
# Write output file
|
|
with open(out_file, 'w', encoding="utf-8") as f:
|
|
f.writelines(lines)
|
|
|
|
print(f"[SUCCESS] Made {replacements} replacements. Output written to '{out_file}'.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |