From 4b4ebf03d99cb530cdf49099fb3c8448017c8e8e Mon Sep 17 00:00:00 2001
From: TheJoeCoder <joe@radialbog9.uk>
Date: Mon, 28 Oct 2024 00:00:06 +0000
Subject: [PATCH] Add final functionality (bugfixes from now on really)

---
 main.py | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/main.py b/main.py
index 237652b..d0d7f84 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)
-- 
GitLab