tunedrop/backend/utils/song_list.py
2025-07-30 21:52:43 +03:00

39 lines
1.0 KiB
Python

from pathlib import Path
from datetime import datetime
import json
SONG_LIST_PATH = Path("/music/songs.json")
def add_song_to_list(file_path: str, query: str):
file_name = Path(file_path).name
new_song = {
"title": query,
"artist": "Unknown",
"path": file_name,
"cover": "/default-cover.jpg",
"added": datetime.utcnow().isoformat()
}
songs = []
if SONG_LIST_PATH.exists():
with open(SONG_LIST_PATH, "r") as f:
try:
songs = json.load(f)
except json.JSONDecodeError:
songs = []
# Avoid duplicates
if not any(song["path"] == file_name for song in songs):
songs.append(new_song)
with open(SONG_LIST_PATH, "w") as f:
json.dump(songs, f, indent=2)
def get_song_list():
if SONG_LIST_PATH.exists():
with open(SONG_LIST_PATH, "r") as f:
try:
return json.load(f)
except json.JSONDecodeError:
return []
return []