Fix watcher sync loop when auto-watch is enabled
This commit is contained in:
@@ -78,10 +78,28 @@ def write_local_playlist(playlist_path: str, tracks: List[str]) -> bool:
|
||||
bool: True if successful, False otherwise.
|
||||
"""
|
||||
try:
|
||||
with open(playlist_path, 'w', encoding="utf-8") as file:
|
||||
file.write("#EXTM3U\n")
|
||||
for track in tracks:
|
||||
file.write(f"{track}\n")
|
||||
desired_lines = ["#EXTM3U\n"] + [f"{track}\n" for track in tracks]
|
||||
desired_content = "".join(desired_lines)
|
||||
|
||||
# Avoid rewriting identical content to prevent watcher feedback loops.
|
||||
if os.path.exists(playlist_path):
|
||||
try:
|
||||
with open(playlist_path, 'r', encoding="utf-8") as existing_file:
|
||||
existing_content = existing_file.read()
|
||||
if existing_content == desired_content:
|
||||
logger.debug(f"Playlist unchanged; skipping write: {playlist_path}")
|
||||
return True
|
||||
except Exception as e:
|
||||
# If read fails, fall back to rewriting.
|
||||
logger.debug(f"Failed to read existing playlist for comparison ({playlist_path}): {e}")
|
||||
|
||||
# Write via temp file then replace for safer updates.
|
||||
os.makedirs(os.path.dirname(os.path.abspath(playlist_path)), exist_ok=True)
|
||||
tmp_path = f"{playlist_path}.tmp"
|
||||
with open(tmp_path, 'w', encoding="utf-8") as file:
|
||||
file.write(desired_content)
|
||||
os.replace(tmp_path, playlist_path)
|
||||
|
||||
logger.info(f"Written {len(tracks)} songs to the playlist: {playlist_path}")
|
||||
return True
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user