diff --git a/src/App.config b/src/App.config index a72998f..55612c8 100644 --- a/src/App.config +++ b/src/App.config @@ -2,7 +2,7 @@ - + diff --git a/src/HistogramHash.cs b/src/HistogramHash.cs index 8d86e02..193d5b5 100644 --- a/src/HistogramHash.cs +++ b/src/HistogramHash.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; @@ -6,12 +7,12 @@ namespace BingWallpaper public class HistogramHash { public readonly string FilePath; - public readonly int[] HashValue; + public readonly List HashValue; - public HistogramHash(string filePath, int[] values) + public HistogramHash(string filePath, List hashValue) { FilePath = filePath; - HashValue = values; + HashValue = hashValue; } public bool Equal(HistogramHash other) diff --git a/src/ImageHashing.cs b/src/ImageHashing.cs index 64209d9..3b55dfc 100644 --- a/src/ImageHashing.cs +++ b/src/ImageHashing.cs @@ -17,14 +17,26 @@ internal static bool ImageInHash(string tempfilename) return HistogramHashTable.Any(hash => hash.Equal(testHash)); } + internal static bool HaveFilePathInHashTable(string filePath) + { + return HistogramHashTable.Any(x => x.FilePath == filePath); + } + internal static void AddHash(string filePath) { - if(HistogramHashTable.Any(x=>x.FilePath == filePath)) return; + if(HaveFilePathInHashTable(filePath)) return; HistogramHashTable.Add(GetRGBHistogram(filePath)); } - internal static HistogramHash GetRGBHistogram(string file) + internal static void ClearHash() + { + HistogramHashTable.RemoveAll(x => string.IsNullOrWhiteSpace(x.FilePath)); + HistogramHashTable.RemoveAll(x => !File.Exists(x.FilePath)); + HistogramHashTable.RemoveAll(x => x.HashValue == null || !x.HashValue.Any()); + } + + private static HistogramHash GetRGBHistogram(string file) { var values = new List(); var histogramfile = Path.Combine(HitogramPath, Guid.NewGuid() + ".jpg"); @@ -44,7 +56,7 @@ internal static HistogramHash GetRGBHistogram(string file) File.Delete(histogramfile); - return new HistogramHash(file, values.ToArray()); + return new HistogramHash(file, values); } } } \ No newline at end of file diff --git a/src/Program.cs b/src/Program.cs index 4337b94..3ccbacf 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,6 +1,7 @@ using System; using System.Configuration; using System.IO; +using System.Threading; namespace BingWallpaper { @@ -14,7 +15,6 @@ private static void Main(string[] args) try { SetupAndTearDown.Startup(); - SetupAndTearDown.ArchiveOldImages(); BingInteractionAndParsing.GetBingImages(); } catch (Exception e) @@ -24,6 +24,7 @@ private static void Main(string[] args) } finally { + Thread.Sleep(TimeSpan.FromSeconds(30)); SetupAndTearDown.Finish(); } } diff --git a/src/SetupAndTearDown.cs b/src/SetupAndTearDown.cs index db6f6b8..fd508ad 100644 --- a/src/SetupAndTearDown.cs +++ b/src/SetupAndTearDown.cs @@ -28,8 +28,11 @@ internal static void Startup() ConsoleWriter.WriteLine("Have loaded {0} countries", BingInteractionAndParsing.Countries.Count); ConsoleWriter.WriteLine("Have loaded {0} previous hashes", ImageHashing.HistogramHashTable.Count); + ArchiveOldImages(); + + ImageHashing.ClearHash(); HashExistingImages(); - + if (ImageHashing.HistogramHashTable.Any()) { Serializer.Serialize(ImageHashing.HistogramHashTable, Path.Combine(Program.AppData, "imageHistogram.bin")); @@ -42,7 +45,7 @@ private static void HashExistingImages(int retryCount = 0) { try { - foreach (var file in Directory.GetFiles(Program.SavePath, "*.jpg")) + foreach (var file in Directory.GetFiles(Program.SavePath, "*.jpg").Where(x => !ImageHashing.HaveFilePathInHashTable(x))) { ConsoleWriter.WriteLine("Hashing file: {0}", file); ImageHashing.AddHash(file); @@ -53,7 +56,7 @@ private static void HashExistingImages(int retryCount = 0) { var archivePath = Path.Combine(Program.SavePath, "Archive"); if (!Directory.Exists(archivePath)) Directory.CreateDirectory(archivePath); - foreach (var file in Directory.GetFiles(archivePath, "*.jpg")) + foreach (var file in Directory.GetFiles(archivePath, "*.jpg").Where(x => !ImageHashing.HaveFilePathInHashTable(x))) { ConsoleWriter.WriteLine("Hashing file: {0}", file); ImageHashing.AddHash(file); @@ -98,12 +101,21 @@ private static void ClearLogFiles(string logPath) internal static void Finish() { - if (Directory.Exists(BingInteractionAndParsing.DownloadPath)) Directory.Delete(BingInteractionAndParsing.DownloadPath, true); - if (Directory.Exists(ImageHashing.HitogramPath)) Directory.Delete(ImageHashing.HitogramPath, true); + try + { + if (Directory.Exists(BingInteractionAndParsing.DownloadPath)) Directory.Delete(BingInteractionAndParsing.DownloadPath, true); + if (Directory.Exists(ImageHashing.HitogramPath)) Directory.Delete(ImageHashing.HitogramPath, true); + } + catch (Exception exception) + { + ConsoleWriter.WriteLine("Error cleaning up", exception); + } + if (BingInteractionAndParsing.UrlsRetrieved.Any()) { Serializer.Serialize(BingInteractionAndParsing.UrlsRetrieved, Path.Combine(Program.AppData, "urlsRetrieved.bin")); } + if (ImageHashing.HistogramHashTable.Any()) { Serializer.Serialize(ImageHashing.HistogramHashTable, Path.Combine(Program.AppData, "imageHistogram.bin")); @@ -122,7 +134,7 @@ internal static void ArchiveOldImages() foreach (var file in Directory.GetFiles(Program.SavePath)) { var fileInfo = new FileInfo(file); - if (fileInfo.LastAccessTimeUtc < DateTime.UtcNow.AddMonths(archiveMonths * -1)) + if (fileInfo.CreationTimeUtc < DateTime.UtcNow.AddMonths(archiveMonths * -1)) { if (!Directory.Exists(archivePath)) Directory.CreateDirectory(archivePath); fileInfo.MoveTo(Path.Combine(archivePath, fileInfo.Name));