PlaylistReader.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using SimpleJSON;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace SongBrowserPlugin.DataAccess
  9. {
  10. public class PlaylistsReader
  11. {
  12. private static Logger _log = new Logger("PlaylistReader");
  13. private List<String> _PlaylistsDirectories = new List<string>();
  14. private List<Playlist> _CachedPlaylists;
  15. public List<Playlist> Playlists
  16. {
  17. get
  18. {
  19. return _CachedPlaylists;
  20. }
  21. }
  22. public PlaylistsReader(String playlistsDirectory)
  23. {
  24. _PlaylistsDirectories.Add(playlistsDirectory);
  25. // Hack, add beatdrop location
  26. String localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  27. String beatDropPlaylistPath = Path.Combine(localAppDataPath, "Programs", "BeatDrop", "playlists");
  28. String beatDropCuratorPlaylistPath = Path.Combine(localAppDataPath, "Programs", "BeatDropCurator", "playlists");
  29. _PlaylistsDirectories.Add(beatDropPlaylistPath);
  30. _PlaylistsDirectories.Add(beatDropCuratorPlaylistPath);
  31. }
  32. public void UpdatePlaylists()
  33. {
  34. _CachedPlaylists = new List<Playlist>();
  35. Stopwatch timer = new Stopwatch();
  36. timer.Start();
  37. foreach (String path in _PlaylistsDirectories)
  38. {
  39. _log.Debug("Reading playlists located at: {0}", path);
  40. if (!Directory.Exists(path))
  41. {
  42. _log.Info("Playlist path does not exist: {0}", path);
  43. continue;
  44. }
  45. string[] files = Directory.GetFiles(path);
  46. foreach (string file in files)
  47. {
  48. _log.Debug("Checking file {0}", file);
  49. if (Path.GetExtension(file) == ".json")
  50. {
  51. Playlist p = ParsePlaylist(file);
  52. _CachedPlaylists.Add(p);
  53. }
  54. }
  55. }
  56. timer.Stop();
  57. _log.Debug("Processing playlists took {0}ms", timer.ElapsedMilliseconds);
  58. }
  59. public static Playlist ParsePlaylist(String path)
  60. {
  61. try
  62. {
  63. if (!File.Exists(path))
  64. {
  65. _log.Debug("Playlist file no longer exists: {0}", path);
  66. return null;
  67. }
  68. _log.Debug("Parsing playlist at {0}", path);
  69. String json = File.ReadAllText(path);
  70. Playlist playlist = new Playlist();
  71. JSONNode playlistNode = JSON.Parse(json);
  72. playlist.image = playlistNode["image"];
  73. playlist.playlistTitle = playlistNode["playlistTitle"];
  74. playlist.playlistAuthor = playlistNode["playlistAuthor"];
  75. playlist.songs = new List<PlaylistSong>();
  76. foreach (JSONNode node in playlistNode["songs"].AsArray)
  77. {
  78. PlaylistSong song = new PlaylistSong();
  79. song.Key = node["key"];
  80. song.SongName = node["songName"];
  81. playlist.songs.Add(song);
  82. }
  83. playlist.playlistPath = path;
  84. return playlist;
  85. }
  86. catch (Exception e)
  87. {
  88. _log.Exception("Exception parsing playlist: ", e);
  89. }
  90. return null;
  91. }
  92. }
  93. }