Skip to content

Commit

Permalink
Fix issue with Hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister committed Sep 13, 2016
1 parent bac8ca3 commit ae1a9e5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<configuration>
<appSettings>
<add key="ImageSavePath" value="G:\Bing Wallpaper"/>
<add key="ArchiveAfterMonths" value="3"/>
<add key="ArchiveAfterMonths" value="2"/>
<add key="PreventDuplicatesInArchive" value="True"/>
</appSettings>
<startup>
Expand Down
7 changes: 4 additions & 3 deletions src/HistogramHash.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -6,12 +7,12 @@ namespace BingWallpaper
public class HistogramHash
{
public readonly string FilePath;
public readonly int[] HashValue;
public readonly List<int> HashValue;

public HistogramHash(string filePath, int[] values)
public HistogramHash(string filePath, List<int> hashValue)
{
FilePath = filePath;
HashValue = values;
HashValue = hashValue;
}

public bool Equal(HistogramHash other)
Expand Down
18 changes: 15 additions & 3 deletions src/ImageHashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>();
var histogramfile = Path.Combine(HitogramPath, Guid.NewGuid() + ".jpg");
Expand All @@ -44,7 +56,7 @@ internal static HistogramHash GetRGBHistogram(string file)

File.Delete(histogramfile);

return new HistogramHash(file, values.ToArray());
return new HistogramHash(file, values);
}
}
}
3 changes: 2 additions & 1 deletion src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Configuration;
using System.IO;
using System.Threading;

namespace BingWallpaper
{
Expand All @@ -14,7 +15,6 @@ private static void Main(string[] args)
try
{
SetupAndTearDown.Startup();
SetupAndTearDown.ArchiveOldImages();
BingInteractionAndParsing.GetBingImages();
}
catch (Exception e)
Expand All @@ -24,6 +24,7 @@ private static void Main(string[] args)
}
finally
{
Thread.Sleep(TimeSpan.FromSeconds(30));
SetupAndTearDown.Finish();
}
}
Expand Down
24 changes: 18 additions & 6 deletions src/SetupAndTearDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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"));
Expand All @@ -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));
Expand Down

0 comments on commit ae1a9e5

Please sign in to comment.