diff --git a/main.py b/main.py index 237652be3a3df5eb14129956a014d51670f022da..d0d7f84d4101b56230b264d676667ec84b3689d2 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import argparse import json import os +import shutil parser = argparse.ArgumentParser( description='Rebuilds an m3u playlist.' @@ -57,5 +58,27 @@ playlist_lines = [line for line in playlist_lines if line and not line.startswit # Read the patches file, and store as json object with args.patches as f: patches = json.load(f) + patches_map = { patch['source']: patch['target'] for patch in patches } + +# Output playlist +output_playlist = [] # Apply the patches to the playlist +for line in playlist_lines: + if line in patches_map: + if not os.path.exists(os.path.join(playlist_dir, patches_map[line])): + print("WARNING: File not found: " + patches_map[line]) + line = patches_map[line] + else: + # Copy the file to the output directory + file_path = str(os.path.join(playlist_dir, line.strip())) + new_path = str(os.path.join(str(output_files_dir), os.path.basename(file_path))) + os.makedirs(os.path.dirname(new_path), exist_ok=True) + shutil.copy(file_path, new_path) + line = os.path.join(args.output_dir, os.path.basename(file_path)) + +# Write the output playlist +with args.output_playlist as f: + f.writelines(output_playlist) + +print("Rebuilt playlist written to " + args.output_playlist.name)