diff --git a/SongCrawler/Program.cs b/SongCrawler/Program.cs index ac81530..2fc48fa 100644 --- a/SongCrawler/Program.cs +++ b/SongCrawler/Program.cs @@ -18,6 +18,12 @@ namespace SongCrawler class Program { + // Properties to store commonly used values from args + private static string Controller { get; set; } + private static string SongsFolderPath { get; set; } + private static string[] Args { get; set; } + private static FireSharp.FirebaseClient FirebaseClient { get; set; } + private static Guid GuidFromString(string input) { using (MD5 md5 = MD5.Create()) @@ -61,6 +67,20 @@ namespace SongCrawler } } + private static void InitializeFromArgs(string[] args) + { + Args = args; + if (args.Length > 0) + { + Controller = args[0]; + } + if (args.Length > 1) + { + SongsFolderPath = args[1]; + } + FirebaseClient = CreateFirebaseClient(); + } + // Additional helper methods for more refactoring private static List LoadFirebaseData(FireSharp.FirebaseClient client, string path) where T : class { @@ -70,7 +90,7 @@ namespace SongCrawler } catch { - return convertToList(client.Get(path).Body); + return convertToList(client.Get(path).Body); } } @@ -160,60 +180,59 @@ namespace SongCrawler static void Main(string[] args) { + InitializeFromArgs(args); + if (args.Count() == 3) { - DeleteSongs(args); + DeleteSongs(); } else { - CrawlSongs(args); + CrawlSongs(); } } - private static void CrawlSongs(string[] args) + private static void CrawlSongs() { //string [] test = { "mbrucedogs", "z://" }; //args = test; var debug = false; - ValidateArgs(args, 2, "usage: songcrawler partyid songspath"); - if (args.Length != 2) return; + ValidateArgs(Args, 2, "usage: songcrawler partyid songspath"); + if (Args.Length != 2) return; - string controller = args[0]; - string songpath = args[1]; - FireSharp.FirebaseClient client = CreateFirebaseClient(); - string songsPath = GetControllerPath(controller, "songs"); - string favoritesPath = GetControllerPath(controller, "favorites"); - string disabledPath = GetControllerPath(controller, "disabled"); + string songsPath = GetControllerPath(Controller, "songs"); + string favoritesPath = GetControllerPath(Controller, "favorites"); + string disabledPath = GetControllerPath(Controller, "disabled"); Console.WriteLine("Loading current library"); List songs = new List(); - List disabled = LoadFirebaseData(client, disabledPath); - List favorited = LoadFirebaseData(client, favoritesPath); + List disabled = LoadFirebaseData(FirebaseClient, disabledPath); + List favorited = LoadFirebaseData(FirebaseClient, favoritesPath); - client.Set(songsPath, songs); + FirebaseClient.Set(songsPath, songs); - List files = GetAllMusicFiles(songpath); + List files = GetAllMusicFiles(SongsFolderPath); ProcessFilesToSongs(files, songs, debug: debug, debugLimit: 1000); //sync all favorite, history, disabled SyncSongProperties(songs, favorited, song => song.Favorite = true); SyncSongProperties(songs, disabled, song => song.Disabled = true); - client.Set(songsPath, songs); + FirebaseClient.Set(songsPath, songs); - //string test = string.Format("controllers/{0}/testsongs", controller); + //string test = string.Format("controllers/{0}/testsongs", Controller); //Dictionary testSongs = new Dictionary(); //foreach (Song s in songs) //{ // testSongs[s.Guid] = s; //} - //client.Set(test, testSongs); + //FirebaseClient.Set(test, testSongs); 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 = GetControllerPath(controller, "newSongs"); - client.Set(newSongs, added); + string newSongs = GetControllerPath(Controller, "newSongs"); + FirebaseClient.Set(newSongs, added); } private class PathOnly @@ -226,41 +245,38 @@ namespace SongCrawler } } - private static List convertToList(dynamic json) + private static List convertToList(dynamic json) where T : class { dynamic data = JsonConvert.DeserializeObject(json); - var list = new List(); + var list = new List(); foreach (var itemDynamic in data) { var fjson = itemDynamic.Value.ToString(); - var f = JsonConvert.DeserializeObject(fjson); + var f = JsonConvert.DeserializeObject(fjson); list.Add(f); } return list; } - private static void DeleteSongs(string[] args) + private static void DeleteSongs() { - ValidateArgs(args, 3, "usage: songcrawler partyid songspath delete"); - if (args.Length != 3) return; + ValidateArgs(Args, 3, "usage: songcrawler partyid songspath delete"); + if (Args.Length != 3) return; - string controller = args[0]; - string songpath = args[1]; - FireSharp.FirebaseClient client = CreateFirebaseClient(); - string firepath = GetControllerPath(controller, "songs"); + string firepath = GetControllerPath(Controller, "songs"); Console.WriteLine("Deleting Songs ..."); List songs = new List(); - client.Set(firepath, songs); + FirebaseClient.Set(firepath, songs); } - private static void fixNewSongs(FireSharp.FirebaseClient client, String controller) + private static void fixNewSongs() { - string songsPath = GetControllerPath(controller, "songs"); - string newSongsPath = GetControllerPath(controller, "newSongs"); + string songsPath = GetControllerPath(Controller, "songs"); + string newSongsPath = GetControllerPath(Controller, "newSongs"); - List songs = client.Get(songsPath).ResultAs>(); - List newSongs = client.Get(newSongsPath).ResultAs>(); + List songs = FirebaseClient.Get(songsPath).ResultAs>(); + List newSongs = FirebaseClient.Get(newSongsPath).ResultAs>(); List updated = new List(); foreach (Song n in newSongs){ @@ -270,20 +286,20 @@ namespace SongCrawler updated.Add(found); } } - client.Set(newSongsPath, updated); + FirebaseClient.Set(newSongsPath, updated); } - private static void fixHistory(FireSharp.FirebaseClient client, String controller) + private static void fixHistory() { - string historyPath = GetControllerPath(controller, "history"); + string historyPath = GetControllerPath(Controller, "history"); List history = null; try { - history = client.Get(historyPath).ResultAs>(); + history = FirebaseClient.Get(historyPath).ResultAs>(); } catch { - dynamic data = JsonConvert.DeserializeObject(client.Get(historyPath).Body); + dynamic data = JsonConvert.DeserializeObject(FirebaseClient.Get(historyPath).Body); history = new List(); foreach (var itemDynamic in data) { @@ -292,7 +308,7 @@ namespace SongCrawler history.Add(f); } } - client.Set(historyPath, history); + FirebaseClient.Set(historyPath, history); } public class CreatedSong { @@ -306,23 +322,20 @@ namespace SongCrawler } - private static void CrawlNoDupeSongs(string[] args) + private static void CrawlNoDupeSongs() { - ValidateArgs(args, 2, "usage: songcrawler partyid songspath"); - if (args.Length != 2) return; + ValidateArgs(Args, 2, "usage: songcrawler partyid songspath"); + if (Args.Length != 2) return; - string controller = args[0]; - string songpath = args[1]; - FireSharp.FirebaseClient client = CreateFirebaseClient(); - string firepath = GetControllerPath(controller, "songs"); + string firepath = GetControllerPath(Controller, "songs"); Console.WriteLine("Loading current library"); List songs = new List(); - List files = GetAllMusicFiles(songpath); + List files = GetAllMusicFiles(SongsFolderPath); ProcessFilesToSongs(files, songs, checkDuplicates: true); Console.WriteLine(string.Format("{0:000000}/{1} unique songs", songs.Count(), files.Count())); - client.Set(firepath, songs); + FirebaseClient.Set(firepath, songs); } private static void FindDuplicates(string[] args) @@ -337,16 +350,14 @@ namespace SongCrawler var dupes = FindDuplicateSongs(songs); File.WriteAllText(@"D:\dupliates.json", JsonConvert.SerializeObject(dupes.OrderBy(o => o.Key))); } - private static void DisableDuplicates(string[] args) + private static void DisableDuplicates() { - ValidateArgs(args, 1, "usage: songcrawler partyid songspath"); - if (args.Length != 1) return; + ValidateArgs(Args, 1, "usage: songcrawler partyid songspath"); + if (Args.Length != 1) return; - string controller = args[0]; - FireSharp.FirebaseClient client = CreateFirebaseClient(); - string firepath = GetControllerPath(controller, "songs"); + string firepath = GetControllerPath(Controller, "songs"); Console.WriteLine("Loading current library"); - List songs = client.Get(firepath).ResultAs>(); + List songs = FirebaseClient.Get(firepath).ResultAs>(); var dupes = FindDuplicateSongs(songs, song => !song.Disabled && song.Path.Contains(".mp4")); @@ -358,7 +369,7 @@ namespace SongCrawler duplicateGroup[i].Disabled = true; } } - client.Set(firepath, songs); + FirebaseClient.Set(firepath, songs); } private static Song MakeSong(string filepath)