diff --git a/SongCrawler/Program.cs b/SongCrawler/Program.cs index 752b796..ac81530 100644 --- a/SongCrawler/Program.cs +++ b/SongCrawler/Program.cs @@ -61,6 +61,103 @@ namespace SongCrawler } } + // Additional helper methods for more refactoring + private static List LoadFirebaseData(FireSharp.FirebaseClient client, string path) where T : class + { + try + { + return client.Get(path).ResultAs>(); + } + catch + { + return convertToList(client.Get(path).Body); + } + } + + private static void ProcessFilesToSongs(List files, List songs, bool checkDuplicates = false, bool debug = false, int debugLimit = 1000) + { + Song song = null; + int i = 0; + foreach (string filepath in files) + { + i++; + try + { + song = MakeSong(filepath); + Console.WriteLine(string.Format("{0:000000}/{1} - {2} - {3}", i, files.Count, song.Artist, song.Title)); + + if (checkDuplicates) + { + if (!songs.Any(s => s.Title.ToLower() == song.Title.ToLower() && s.Artist.ToLower() == song.Artist.ToLower())) + { + songs.Add(song); + } + } + else + { + songs.Add(song); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + if (debug && i > debugLimit) + { + break; + } + } + } + + private static void SyncSongProperties(List songs, List sourceList, Action propertySetter) + { + if (sourceList != null && sourceList.Count > 0) + { + sourceList.ForEach(s => + { + if (s != null) + { + var found = songs.Find(ls => s.Path.ToLower() == ls.Path.ToLower()); + if (found != null) + { + propertySetter(found); + } + } + }); + } + } + + private static Dictionary> FindDuplicateSongs(List songs, Func filter = null) + { + Dictionary> dupes = new Dictionary>(); + int i = 0; + + foreach (var localsong in songs) + { + i++; + Console.WriteLine(string.Format("Checking for {0:000000}/{1}) {2} - {3}", i, songs.Count, localsong.Artist, localsong.Title)); + + if (!string.IsNullOrEmpty(localsong.Artist) && !string.IsNullOrEmpty(localsong.Title)) + { + if (filter != null && !filter(localsong)) + continue; + + string key = localsong.Artist + " - " + localsong.Title; + if (!dupes.ContainsKey(key)) + { + var dsongs = songs.Where(s => s.Title.ToLower() == localsong.Title.ToLower() && s.Artist.ToLower() == localsong.Artist.ToLower()); + if (dsongs.Count() > 1) + { + List d = new List(); + d.AddRange(dsongs.ToList()); + dupes.Add(key, d); + } + } + } + } + return dupes; + } + static void Main(string[] args) { if (args.Count() == 3) @@ -89,84 +186,19 @@ namespace SongCrawler string disabledPath = GetControllerPath(controller, "disabled"); Console.WriteLine("Loading current library"); - List songs = null; //client.Get(songsPath).ResultAs>(); - List disabled = null; - List favorited = null; - try - { - disabled = client.Get(disabledPath).ResultAs>(); - } - catch - { - disabled = convertToList(client.Get(disabledPath).Body); - } - - try - { - favorited = client.Get(favoritesPath).ResultAs>(); - } - catch - { - favorited = convertToList(client.Get(favoritesPath).Body); - } - - songs = new List(); + List songs = new List(); + List disabled = LoadFirebaseData(client, disabledPath); + List favorited = LoadFirebaseData(client, favoritesPath); client.Set(songsPath, songs); List files = GetAllMusicFiles(songpath); - Song song = null; - int i = 0; - foreach (string filepath in files) - { - i++; - try - { - song = MakeSong(filepath); - Console.WriteLine(string.Format("{0:000000}/{1} - {2} - {3}", i, files.Count, song.Artist, song.Title)); - songs.Add(song); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - if (debug && i > 1000) - { - break; - } - } + ProcessFilesToSongs(files, songs, debug: debug, debugLimit: 1000); //sync all favorite, history, disabled - if (favorited != null && favorited.Count > 0) - { - favorited.ForEach(s => - { - if (s != null) - { - var found = songs.Find(ls => s.Path.ToLower() == ls.Path.ToLower()); - if (found != null) - { - found.Favorite = true; - } - } - }); - } - - if (disabled != null && disabled.Count > 0) - { - disabled.ForEach(s => - { - if (s != null) - { - var found = songs.Find(ls => s.Path.ToLower() == ls.Path.ToLower()); - if (found != null) - { - found.Disabled = true; - } - } - }); - } + SyncSongProperties(songs, favorited, song => song.Favorite = true); + SyncSongProperties(songs, disabled, song => song.Disabled = true); client.Set(songsPath, songs); //string test = string.Format("controllers/{0}/testsongs", controller); @@ -180,7 +212,7 @@ namespace SongCrawler var created = songs.Select(s => new CreatedSong(File.GetCreationTime(s.Path), s)).ToList(); var first200 = created.Where(s => s.created != null).OrderByDescending(s => s.created).Take(200); var added = first200.Select(s => new PathOnly(path: s.song.Path)).ToList(); - string newSongs = string.Format("controllers/{0}/newSongs", controller); + string newSongs = GetControllerPath(controller, "newSongs"); client.Set(newSongs, added); } @@ -288,22 +320,7 @@ namespace SongCrawler List files = GetAllMusicFiles(songpath); - Song song = null; - int i = 0; - foreach (string filepath in files) - { - i++; - try - { - song = MakeSong(filepath); - Console.WriteLine(string.Format("{0:000000}/{1} - {2}", i, files.Count(), song.Title)); - if (!songs.Any(s => s.Title.ToLower() == song.Title.ToLower() && s.Artist.ToLower() == song.Artist.ToLower())) songs.Add(song); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } + ProcessFilesToSongs(files, songs, checkDuplicates: true); Console.WriteLine(string.Format("{0:000000}/{1} unique songs", songs.Count(), files.Count())); client.Set(firepath, songs); } @@ -315,44 +332,9 @@ namespace SongCrawler List files = GetAllMusicFiles(songpath); - Song song = null; - int i = 0; - foreach (string filepath in files) - { - i++; - try - { - song = MakeSong(filepath); - Console.WriteLine(string.Format("{0:000000}/{1} - {2}", i, files.Count, song.Title)); - songs.Add(song); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } + ProcessFilesToSongs(files, songs); - i = 0; - Dictionary> dupes = new Dictionary>(); - foreach (var localsong in songs) - { - i++; - Console.WriteLine(string.Format("Checking for {0:000000}/{1}) {2} - {3}", i, files.Count, localsong.Artist, localsong.Title)); - if (!string.IsNullOrEmpty(localsong.Artist) && !string.IsNullOrEmpty(localsong.Title)) - { - string key = localsong.Artist + " - " + localsong.Title; - if (dupes.ContainsKey(key) == false) - { - var dsongs = songs.Where(s => s.Title.ToLower() == localsong.Title.ToLower() && s.Artist.ToLower() == localsong.Artist.ToLower()); - if (dsongs.Count() > 1) - { - List d = new List(); - d.AddRange(dsongs.ToList()); - dupes.Add(key, d); - } - } - } - } + var dupes = FindDuplicateSongs(songs); File.WriteAllText(@"D:\dupliates.json", JsonConvert.SerializeObject(dupes.OrderBy(o => o.Key))); } private static void DisableDuplicates(string[] args) @@ -366,35 +348,14 @@ namespace SongCrawler Console.WriteLine("Loading current library"); List songs = client.Get(firepath).ResultAs>(); - Dictionary> dupes = new Dictionary>(); - - int i = 0; - foreach (var localsong in songs) + var dupes = FindDuplicateSongs(songs, song => !song.Disabled && song.Path.Contains(".mp4")); + + // Disable duplicate songs (keep the first one, disable the rest) + foreach (var duplicateGroup in dupes.Values) { - i++; - Console.WriteLine(string.Format("Checking for {0:000000}/{1}) {2} - {3}", i, songs.Count, localsong.Artist, localsong.Title)); - if (!string.IsNullOrEmpty(localsong.Artist) && !string.IsNullOrEmpty(localsong.Title) && localsong.Disabled == false && localsong.Path.Contains(".mp4")) + for (int i = 1; i < duplicateGroup.Count; i++) // Skip first one, disable the rest { - string key = localsong.Artist + " - " + localsong.Title; - if (dupes.ContainsKey(key) == false) - { - var dsongs = songs.Where(s => - s.Path != localsong.Path && - s.Title.ToLower() == localsong.Title.ToLower() && - s.Artist.ToLower() == localsong.Artist.ToLower() && - localsong.Disabled == false); - - if (dsongs.Count() > 1) - { - List d = new List(); - d.AddRange(dsongs.ToList()); - dupes.Add(key, d); - } - foreach (var item in dsongs) - { - item.Disabled = true; - } - } + duplicateGroup[i].Disabled = true; } } client.Set(firepath, songs);