diff --git a/SongCrawler/Program.cs b/SongCrawler/Program.cs index a6ca135..752b796 100644 --- a/SongCrawler/Program.cs +++ b/SongCrawler/Program.cs @@ -27,6 +27,40 @@ namespace SongCrawler } } + // Helper methods to eliminate duplication + private static FireSharp.FirebaseClient CreateFirebaseClient() + { + IFirebaseConfig config = new FirebaseConfig + { + AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"], + BasePath = ConfigurationManager.AppSettings["Firebase.Path"] + }; + return new FireSharp.FirebaseClient(config); + } + + private static string GetControllerPath(string controller, string pathType) + { + return string.Format("controllers/{0}/{1}", controller, pathType); + } + + private static List GetAllMusicFiles(string songpath) + { + List files = new List(); + files.AddRange(FindFiles("mp4", songpath)); + files.AddRange(FindFiles("mp3", songpath)); + files.AddRange(FindFiles("zip", songpath)); + return files; + } + + private static void ValidateArgs(string[] args, int expectedLength, string usage) + { + if (args.Length != expectedLength) + { + Console.WriteLine(usage); + return; + } + } + static void Main(string[] args) { if (args.Count() == 3) @@ -44,22 +78,15 @@ namespace SongCrawler //string [] test = { "mbrucedogs", "z://" }; //args = test; var debug = false; - if (args.Length != 2) - { - Console.WriteLine("usage: songcrawler partyid songspath"); - return; - } + ValidateArgs(args, 2, "usage: songcrawler partyid songspath"); + if (args.Length != 2) return; + string controller = args[0]; string songpath = args[1]; - IFirebaseConfig config = new FirebaseConfig - { - AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"], - BasePath = ConfigurationManager.AppSettings["Firebase.Path"] - }; - FireSharp.FirebaseClient client = new FireSharp.FirebaseClient(config); - string songsPath = string.Format("controllers/{0}/songs", controller); - string favoritesPath = string.Format("controllers/{0}/favorites", controller); - string disabledPath = string.Format("controllers/{0}/disabled", controller); + FireSharp.FirebaseClient client = CreateFirebaseClient(); + string songsPath = GetControllerPath(controller, "songs"); + string favoritesPath = GetControllerPath(controller, "favorites"); + string disabledPath = GetControllerPath(controller, "disabled"); Console.WriteLine("Loading current library"); List songs = null; //client.Get(songsPath).ResultAs>(); @@ -87,10 +114,7 @@ namespace SongCrawler client.Set(songsPath, songs); - List files = new List(); - files.AddRange(FindFiles("mp4", songpath)); - files.AddRange(FindFiles("mp3", songpath)); - files.AddRange(FindFiles("zip", songpath)); + List files = GetAllMusicFiles(songpath); Song song = null; int i = 0; @@ -185,20 +209,13 @@ namespace SongCrawler private static void DeleteSongs(string[] args) { - if (args.Length != 3) - { - Console.WriteLine("usage: songcrawler partyid songspath delete"); - return; - } + ValidateArgs(args, 3, "usage: songcrawler partyid songspath delete"); + if (args.Length != 3) return; + string controller = args[0]; string songpath = args[1]; - IFirebaseConfig config = new FirebaseConfig - { - AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"], - BasePath = ConfigurationManager.AppSettings["Firebase.Path"] - }; - FireSharp.FirebaseClient client = new FireSharp.FirebaseClient(config); - string firepath = string.Format("controllers/{0}/songs", controller); + FireSharp.FirebaseClient client = CreateFirebaseClient(); + string firepath = GetControllerPath(controller, "songs"); Console.WriteLine("Deleting Songs ..."); List songs = new List(); client.Set(firepath, songs); @@ -207,8 +224,8 @@ namespace SongCrawler private static void fixNewSongs(FireSharp.FirebaseClient client, String controller) { - string songsPath = string.Format("controllers/{0}/songs", controller); - string newSongsPath = string.Format("controllers/{0}/newSongs", controller); + string songsPath = GetControllerPath(controller, "songs"); + string newSongsPath = GetControllerPath(controller, "newSongs"); List songs = client.Get(songsPath).ResultAs>(); List newSongs = client.Get(newSongsPath).ResultAs>(); @@ -226,14 +243,15 @@ namespace SongCrawler private static void fixHistory(FireSharp.FirebaseClient client, String controller) { + string historyPath = GetControllerPath(controller, "history"); List history = null; try { - history = client.Get(string.Format("controllers/{0}/history", controller)).ResultAs>(); + history = client.Get(historyPath).ResultAs>(); } catch { - dynamic data = JsonConvert.DeserializeObject(client.Get(string.Format("controllers/{0}/history", controller)).Body); + dynamic data = JsonConvert.DeserializeObject(client.Get(historyPath).Body); history = new List(); foreach (var itemDynamic in data) { @@ -242,7 +260,7 @@ namespace SongCrawler history.Add(f); } } - client.Set(string.Format("controllers/{0}/history", controller), history); + client.Set(historyPath, history); } public class CreatedSong { @@ -258,27 +276,17 @@ namespace SongCrawler private static void CrawlNoDupeSongs(string[] args) { - if (args.Length != 2) - { - Console.WriteLine("usage: songcrawler partyid songspath"); - return; - } + ValidateArgs(args, 2, "usage: songcrawler partyid songspath"); + if (args.Length != 2) return; + string controller = args[0]; string songpath = args[1]; - IFirebaseConfig config = new FirebaseConfig - { - AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"], - BasePath = ConfigurationManager.AppSettings["Firebase.Path"] - }; - FireSharp.FirebaseClient client = new FireSharp.FirebaseClient(config); - string firepath = string.Format("controllers/{0}/songs", controller); + FireSharp.FirebaseClient client = CreateFirebaseClient(); + string firepath = GetControllerPath(controller, "songs"); Console.WriteLine("Loading current library"); List songs = new List(); - List files = new List(); - files.AddRange(FindFiles("mp4", songpath)); - files.AddRange(FindFiles("mp3", songpath)); - files.AddRange(FindFiles("zip", songpath)); + List files = GetAllMusicFiles(songpath); Song song = null; int i = 0; @@ -305,10 +313,7 @@ namespace SongCrawler string songpath = @"D:\KaraokeData\Karaoke"; // args[0]; List songs = songs = new List(); - List files = new List(); - files.AddRange(FindFiles("mp3", songpath)); - files.AddRange(FindFiles("zip", songpath)); - files.AddRange(FindFiles("mp4", songpath)); + List files = GetAllMusicFiles(songpath); Song song = null; int i = 0; @@ -352,19 +357,12 @@ namespace SongCrawler } private static void DisableDuplicates(string[] args) { - if (args.Length != 1) - { - Console.WriteLine("usage: songcrawler partyid songspath"); - return; - } + ValidateArgs(args, 1, "usage: songcrawler partyid songspath"); + if (args.Length != 1) return; + string controller = args[0]; - IFirebaseConfig config = new FirebaseConfig - { - AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"], - BasePath = ConfigurationManager.AppSettings["Firebase.Path"] - }; - FireSharp.FirebaseClient client = new FireSharp.FirebaseClient(config); - string firepath = string.Format("controllers/{0}/songs", controller); + FireSharp.FirebaseClient client = CreateFirebaseClient(); + string firepath = GetControllerPath(controller, "songs"); Console.WriteLine("Loading current library"); List songs = client.Get(firepath).ResultAs>();