0
1
mirror of https://github.com/radio95-rnt/RadioPlayer.git synced 2026-02-26 13:52:00 +01:00

less lines

This commit is contained in:
2026-02-11 21:31:53 +01:00
parent c226da8afb
commit 3504128429

View File

@@ -175,16 +175,30 @@ class PlaylistManager:
for line in f: for line in f:
line = line.strip() line = line.strip()
if line: if line:
# Convert to relative path # Check if it's a directory pattern
abs_path = Path(line) if line.endswith("/*"):
try: # It's a directory pattern - expand it to individual files
rel_path = str(abs_path.relative_to(FILES_DIR)) dir_path = Path(line[:-2]) # Remove /*
except ValueError: if dir_path.exists():
# If it's already relative, use as is for file in sorted(dir_path.glob("*")):
rel_path = line if file.is_file():
try:
self.custom_playlist_files.add(rel_path) rel_path = str(file.relative_to(FILES_DIR))
playlists["custom"]["day"].add(rel_path) self.custom_playlist_files.add(rel_path)
playlists["custom"]["day"].add(rel_path)
except ValueError:
pass
else:
# Individual file
abs_path = Path(line)
try:
rel_path = str(abs_path.relative_to(FILES_DIR))
except ValueError:
# If it's already relative, use as is
rel_path = line
self.custom_playlist_files.add(rel_path)
playlists["custom"]["day"].add(rel_path)
return playlists return playlists
else: else:
# Original functionality for weekly playlists # Original functionality for weekly playlists
@@ -201,14 +215,27 @@ class PlaylistManager:
for line in f: for line in f:
line = line.strip() line = line.strip()
if line: if line:
# Convert to relative path # Check if it's a directory pattern
abs_path = Path(line) if line.endswith("/*"):
try: # It's a directory pattern - expand it to individual files
rel_path = str(abs_path.relative_to(FILES_DIR)) dir_path = Path(line[:-2]) # Remove /*
except ValueError: if dir_path.exists():
# If it's already relative, use as is for file in sorted(dir_path.glob("*")):
rel_path = line if file.is_file():
playlists[day][period].add(rel_path) try:
rel_path = str(file.relative_to(FILES_DIR))
playlists[day][period].add(rel_path)
except ValueError:
pass
else:
# Individual file
abs_path = Path(line)
try:
rel_path = str(abs_path.relative_to(FILES_DIR))
except ValueError:
# If it's already relative, use as is
rel_path = line
playlists[day][period].add(rel_path)
return playlists return playlists
def update_playlist_file(self, day: str, period: str, file_item: FileItem, add: bool): def update_playlist_file(self, day: str, period: str, file_item: FileItem, add: bool):
@@ -232,40 +259,58 @@ class PlaylistManager:
with open(custom_path, 'r') as f: with open(custom_path, 'r') as f:
lines = f.read().splitlines() lines = f.read().splitlines()
# Get all files in this item as absolute paths
files_to_process = set()
for rel_path in file_item.all_files:
abs_path = str(FILES_DIR / rel_path)
files_to_process.add(abs_path)
if add: if add:
# Add new files that aren't already in the list if file_item.is_folder:
for filepath in files_to_process: # For folders, write the directory pattern
if filepath not in lines: folder_path = file_item.path.parent
lines.append(filepath) dir_pattern = str(folder_path / "*")
# Also update the tracking set with relative path
try: # Remove any individual files from this folder that might exist
rel_path = str(Path(filepath).relative_to(FILES_DIR)) lines = [line for line in lines if not line.startswith(str(folder_path) + "/")]
# Add the directory pattern if not already there
if dir_pattern not in lines:
lines.append(dir_pattern)
# Update tracking set with all files
for rel_path in file_item.all_files:
self.custom_playlist_files.add(rel_path)
else:
# For individual files, add the file path
abs_path = str(file_item.path)
if abs_path not in lines:
lines.append(abs_path)
# Update tracking set
for rel_path in file_item.all_files:
self.custom_playlist_files.add(rel_path) self.custom_playlist_files.add(rel_path)
except ValueError:
pass
else: else:
# Remove files if file_item.is_folder:
for filepath in files_to_process: # Remove directory pattern
while filepath in lines: folder_path = file_item.path.parent
lines.remove(filepath) dir_pattern = str(folder_path / "*")
# Also update the tracking set
try: # Remove the pattern and any individual files from this folder
rel_path = str(Path(filepath).relative_to(FILES_DIR)) lines = [line for line in lines if line != dir_pattern and not line.startswith(str(folder_path) + "/")]
# Update tracking set
for rel_path in file_item.all_files:
self.custom_playlist_files.discard(rel_path)
else:
# Remove individual file
abs_path = str(file_item.path)
while abs_path in lines:
lines.remove(abs_path)
# Update tracking set
for rel_path in file_item.all_files:
self.custom_playlist_files.discard(rel_path) self.custom_playlist_files.discard(rel_path)
except ValueError:
pass
with open(custom_path, 'w') as f: with open(custom_path, 'w') as f:
f.write('\n'.join(lines) + ('\n' if lines else '')) f.write('\n'.join(lines) + ('\n' if lines else ''))
def _update_weekly_playlist(self, day: str, period: str, file_item: FileItem, add: bool): def _update_weekly_playlist(self, day: str, period: str, file_item: FileItem, add: bool):
"""Update a weekly playlist file (original functionality).""" """Update a weekly playlist file."""
playlist_dir = self.ensure_playlist_dir(day) playlist_dir = self.ensure_playlist_dir(day)
playlist_file = playlist_dir / period playlist_file = playlist_dir / period
@@ -275,22 +320,36 @@ class PlaylistManager:
with open(playlist_file, 'r') as f: with open(playlist_file, 'r') as f:
lines = f.read().splitlines() lines = f.read().splitlines()
# Get all files in this item as absolute paths
files_to_process = set()
for rel_path in file_item.all_files:
abs_path = str(FILES_DIR / rel_path)
files_to_process.add(abs_path)
if add: if add:
# Add new files that aren't already in the list if file_item.is_folder:
for filepath in files_to_process: # For folders, write the directory pattern
if filepath not in lines: folder_path = file_item.path.parent
lines.append(filepath) dir_pattern = str(folder_path / "*")
# Remove any individual files from this folder that might exist
lines = [line for line in lines if not line.startswith(str(folder_path) + "/")]
# Add the directory pattern if not already there
if dir_pattern not in lines:
lines.append(dir_pattern)
else:
# For individual files, add the file path
abs_path = str(file_item.path)
if abs_path not in lines:
lines.append(abs_path)
else: else:
# Remove files if file_item.is_folder:
for filepath in files_to_process: # Remove directory pattern
while filepath in lines: folder_path = file_item.path.parent
lines.remove(filepath) dir_pattern = str(folder_path / "*")
# Remove the pattern and any individual files from this folder
lines = [line for line in lines if line != dir_pattern and not line.startswith(str(folder_path) + "/")]
else:
# Remove individual file
abs_path = str(file_item.path)
while abs_path in lines:
lines.remove(abs_path)
with open(playlist_file, 'w', encoding='utf-8', errors='strict') as f: with open(playlist_file, 'w', encoding='utf-8', errors='strict') as f:
for line in lines: for line in lines:
@@ -370,15 +429,32 @@ class PlaylistManager:
else: else:
lines = [] lines = []
# Convert relative paths to absolute for file storage
abs_paths = [str(FILES_DIR / rel_path) for rel_path in item_rel_paths]
if is_present: if is_present:
for abs_path in abs_paths: if current_item.is_folder:
# For folders, write the directory pattern
folder_path = current_item.path.parent
dir_pattern = str(folder_path / "*")
# Remove any individual files from this folder
lines = [line for line in lines if not line.startswith(str(folder_path) + "/")]
# Add the directory pattern
if dir_pattern not in lines:
lines.append(dir_pattern)
else:
# For individual files
abs_path = str(current_item.path)
if abs_path not in lines: if abs_path not in lines:
lines.append(abs_path) lines.append(abs_path)
else: else:
for abs_path in abs_paths: if current_item.is_folder:
# Remove directory pattern and individual files
folder_path = current_item.path.parent
dir_pattern = str(folder_path / "*")
lines = [line for line in lines if line != dir_pattern and not line.startswith(str(folder_path) + "/")]
else:
# Remove individual file
abs_path = str(current_item.path)
while abs_path in lines: while abs_path in lines:
lines.remove(abs_path) lines.remove(abs_path)