step 3 refacotr
Signed-off-by: mbrucedogs <mbrucedogs@gmail.com>
This commit is contained in:
parent
fbaba1427a
commit
02159a1420
@ -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<T> LoadFirebaseData<T>(FireSharp.FirebaseClient client, string path) where T : class
|
||||
{
|
||||
@ -70,7 +90,7 @@ namespace SongCrawler
|
||||
}
|
||||
catch
|
||||
{
|
||||
return convertToList(client.Get(path).Body);
|
||||
return convertToList<T>(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<Song> songs = new List<Song>();
|
||||
List<Song> disabled = LoadFirebaseData<Song>(client, disabledPath);
|
||||
List<Song> favorited = LoadFirebaseData<Song>(client, favoritesPath);
|
||||
List<Song> disabled = LoadFirebaseData<Song>(FirebaseClient, disabledPath);
|
||||
List<Song> favorited = LoadFirebaseData<Song>(FirebaseClient, favoritesPath);
|
||||
|
||||
client.Set(songsPath, songs);
|
||||
FirebaseClient.Set(songsPath, songs);
|
||||
|
||||
List<string> files = GetAllMusicFiles(songpath);
|
||||
List<string> 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<string, Song> testSongs = new Dictionary<string, Song>();
|
||||
//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<Song> convertToList(dynamic json)
|
||||
private static List<T> convertToList<T>(dynamic json) where T : class
|
||||
{
|
||||
dynamic data = JsonConvert.DeserializeObject<dynamic>(json);
|
||||
var list = new List<Song>();
|
||||
var list = new List<T>();
|
||||
foreach (var itemDynamic in data)
|
||||
{
|
||||
var fjson = itemDynamic.Value.ToString();
|
||||
var f = JsonConvert.DeserializeObject<Song>(fjson);
|
||||
var f = JsonConvert.DeserializeObject<T>(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<Song> songs = new List<Song>();
|
||||
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<Song> songs = client.Get(songsPath).ResultAs<List<Song>>();
|
||||
List<Song> newSongs = client.Get(newSongsPath).ResultAs<List<Song>>();
|
||||
List<Song> songs = FirebaseClient.Get(songsPath).ResultAs<List<Song>>();
|
||||
List<Song> newSongs = FirebaseClient.Get(newSongsPath).ResultAs<List<Song>>();
|
||||
|
||||
List<Song> updated = new List<Song>();
|
||||
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> history = null;
|
||||
try
|
||||
{
|
||||
history = client.Get(historyPath).ResultAs<List<History>>();
|
||||
history = FirebaseClient.Get(historyPath).ResultAs<List<History>>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
dynamic data = JsonConvert.DeserializeObject<dynamic>(client.Get(historyPath).Body);
|
||||
dynamic data = JsonConvert.DeserializeObject<dynamic>(FirebaseClient.Get(historyPath).Body);
|
||||
history = new List<History>();
|
||||
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<Song> songs = new List<Song>();
|
||||
|
||||
List<string> files = GetAllMusicFiles(songpath);
|
||||
List<string> 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<Song> songs = client.Get(firepath).ResultAs<List<Song>>();
|
||||
List<Song> songs = FirebaseClient.Get(firepath).ResultAs<List<Song>>();
|
||||
|
||||
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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user