refacotre 2
Signed-off-by: mbrucedogs <mbrucedogs@gmail.com>
This commit is contained in:
parent
86ffb58df4
commit
fbaba1427a
@ -61,6 +61,103 @@ namespace SongCrawler
|
||||
}
|
||||
}
|
||||
|
||||
// Additional helper methods for more refactoring
|
||||
private static List<T> LoadFirebaseData<T>(FireSharp.FirebaseClient client, string path) where T : class
|
||||
{
|
||||
try
|
||||
{
|
||||
return client.Get(path).ResultAs<List<T>>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return convertToList(client.Get(path).Body);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ProcessFilesToSongs(List<string> files, List<Song> 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<Song> songs, List<Song> sourceList, Action<Song> 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<string, List<Song>> FindDuplicateSongs(List<Song> songs, Func<Song, bool> filter = null)
|
||||
{
|
||||
Dictionary<string, List<Song>> dupes = new Dictionary<string, List<Song>>();
|
||||
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<Song> d = new List<Song>();
|
||||
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<Song> songs = null; //client.Get(songsPath).ResultAs<List<Song>>();
|
||||
List<Song> disabled = null;
|
||||
List<Song> favorited = null;
|
||||
try
|
||||
{
|
||||
disabled = client.Get(disabledPath).ResultAs<List<Song>>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
disabled = convertToList(client.Get(disabledPath).Body);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
favorited = client.Get(favoritesPath).ResultAs<List<Song>>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
favorited = convertToList(client.Get(favoritesPath).Body);
|
||||
}
|
||||
|
||||
songs = new List<Song>();
|
||||
List<Song> songs = new List<Song>();
|
||||
List<Song> disabled = LoadFirebaseData<Song>(client, disabledPath);
|
||||
List<Song> favorited = LoadFirebaseData<Song>(client, favoritesPath);
|
||||
|
||||
client.Set(songsPath, songs);
|
||||
|
||||
List<string> 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<string> 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<string> 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<string, List<Song>> dupes = new Dictionary<string, List<Song>>();
|
||||
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<Song> d = new List<Song>();
|
||||
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<Song> songs = client.Get(firepath).ResultAs<List<Song>>();
|
||||
|
||||
Dictionary<string, List<Song>> dupes = new Dictionary<string, List<Song>>();
|
||||
|
||||
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<Song> d = new List<Song>();
|
||||
d.AddRange(dsongs.ToList());
|
||||
dupes.Add(key, d);
|
||||
}
|
||||
foreach (var item in dsongs)
|
||||
{
|
||||
item.Disabled = true;
|
||||
}
|
||||
}
|
||||
duplicateGroup[i].Disabled = true;
|
||||
}
|
||||
}
|
||||
client.Set(firepath, songs);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user