1
0

playlist_manager.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import os
  2. import json
  3. PLAYLISTS_FILE = os.path.join(os.getcwd(), "playlists.json")
  4. def load_playlists():
  5. """
  6. Load the entire playlists dictionary from the JSON file.
  7. Returns something like: {
  8. "My Playlist": ["file1.thr", "file2.thr"],
  9. "Another": ["x.thr"]
  10. }
  11. """
  12. with open(PLAYLISTS_FILE, "r") as f:
  13. return json.load(f)
  14. def save_playlists(playlists_dict):
  15. """Save the entire playlists dictionary back to the JSON file."""
  16. with open(PLAYLISTS_FILE, "w") as f:
  17. json.dump(playlists_dict, f, indent=2)
  18. def list_all_playlists():
  19. """Returns a list of all playlist names."""
  20. playlists_dict = load_playlists()
  21. return list(playlists_dict.keys())
  22. def get_playlist(playlist_name):
  23. """Get a specific playlist by name."""
  24. playlists_dict = load_playlists()
  25. if playlist_name not in playlists_dict:
  26. return None
  27. return {
  28. "name": playlist_name,
  29. "files": playlists_dict[playlist_name]
  30. }
  31. def create_playlist(playlist_name, files):
  32. """Create or update a playlist."""
  33. playlists_dict = load_playlists()
  34. playlists_dict[playlist_name] = files
  35. save_playlists(playlists_dict)
  36. return True
  37. def modify_playlist(playlist_name, files):
  38. """Modify an existing playlist."""
  39. playlists_dict = load_playlists()
  40. if playlist_name not in playlists_dict:
  41. return False
  42. playlists_dict[playlist_name] = files
  43. save_playlists(playlists_dict)
  44. return True
  45. def delete_playlist(playlist_name):
  46. """Delete a playlist."""
  47. playlists_dict = load_playlists()
  48. if playlist_name not in playlists_dict:
  49. return False
  50. del playlists_dict[playlist_name]
  51. save_playlists(playlists_dict)
  52. return True
  53. def add_to_playlist(playlist_name, pattern):
  54. """Add a pattern to an existing playlist."""
  55. playlists_dict = load_playlists()
  56. if playlist_name not in playlists_dict:
  57. return False
  58. playlists_dict[playlist_name].append(pattern)
  59. save_playlists(playlists_dict)
  60. return True