#!/bin/python3 import sqlite3 import uuid from pathlib import Path from contextlib import closing import change_path import shared def main() -> None: import argparse parser = argparse.ArgumentParser(description='Merge tracks from the library into any of ones in new_dir') parser.add_argument('--nbd', type=Path, default=Path('/data/navidrome.db'), help='Path to navidrome db') parser.add_argument('new_dir', type=Path) parser.add_argument('--exchange', action='store_true', default=False, help='Should we exchange metadata of files or leave to die metadata of an old file') args = parser.parse_args() conn: sqlite3.Connection with closing(sqlite3.connect(args.nbd)) as conn: for file_path in args.new_dir.glob('**/*'): print(f'Handling file {str(file_path)}') current_song_response = change_path.get_media_by_path(conn, str(file_path)) if len(current_song_response) == 0: continue current_song = current_song_response[0] search_result = shared.get_media_by_metadata(current_song.artist, current_song.track_name, conn) if search_result is not None: interaction = input(f'Do you want to move these files {str(search_result.path)!r} --> {str(file_path)!r} (Y/n)?') if interaction.lower() in ('', 'y', 'yes'): print(f"Performing {'exchange' if args.exchange else 'move'}") tmp_name = uuid.uuid4().hex # Firstly, we need to move target file to temp location change_path.main(conn=conn, from_path=str(file_path), to_path=tmp_name) # Secondly, move first file to place of target file change_path.main(conn, from_path=str(search_result.path), to_path=str(file_path)) if args.exchange: # Thirdly, optional, move target file to place of first file change_path.main(conn, from_path=tmp_name, to_path=str(search_result.path)) conn.commit() if __name__ == '__main__': main()