Skip to content

Commit

Permalink
Add UHD & Delete from archive
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister committed Oct 18, 2021
1 parent 74d5a5c commit e2f2256
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 13 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
version: 1.0.{build}
version: 1.1.{build}
image: Visual Studio 2019
skip_tags: true
skip_branch_with_pr: true
skip_commits:
files:
- .github/*
- .paket/*
- '**/*.md'
- '**/*.md'
dotnet_csproj:
patch: true
file: '**\*.csproj'
Expand Down
2 changes: 1 addition & 1 deletion src/BingImageDownload/BingImageDownload.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<PackAsTool>true</PackAsTool>
<ToolCommandName>BingImageDownload</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
Expand Down
12 changes: 10 additions & 2 deletions src/BingImageDownload/BingInteractionAndParsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ internal class BingInteractionAndParsing
private readonly Serializer serializer;
private readonly List<string> urlsRetrieved;
private readonly string urlsRetrievedBinFile;
private readonly string urlExtension;

public BingInteractionAndParsing(ConsoleWriter consoleWriter, ImageFingerprinting imageFingerprinting, ImagePropertyHandling imagePropertyHandling, Paths paths, Serializer serializer)
public BingInteractionAndParsing(ConsoleWriter consoleWriter, ImageFingerprinting imageFingerprinting, ImagePropertyHandling imagePropertyHandling, Paths paths, Serializer serializer, string resolution)
{
this.consoleWriter = consoleWriter;
this.imageFingerprinting = imageFingerprinting;
Expand All @@ -34,6 +35,13 @@ public BingInteractionAndParsing(ConsoleWriter consoleWriter, ImageFingerprintin
urlsRetrieved = serializer.Deserialize<List<string>>(urlsRetrievedBinFile).ToList();

consoleWriter.WriteLine($"Have loaded {urlsRetrieved.Count} previous URLs");

urlExtension = resolution?.ToUpper() switch
{
"HD" => "1366x768.jpg",
"UHD" => "UHD.jpg",
_ => "1920x1080.jpg"
};
}

internal (int countryDownloadedImages, int countryDuplicateImages, int countrySeenUrls) GetBingImages(CultureInfo country)
Expand Down Expand Up @@ -77,7 +85,7 @@ public BingInteractionAndParsing(ConsoleWriter consoleWriter, ImageFingerprintin
for (int i = 0; i < datePairs.Count; i++)
{
var (startDate, endDate, imageNode) = datePairs[i];
var imageUrl = $"{Url}{imageNode.Element("urlBase")?.Value}_1920x1080.jpg";
var imageUrl = $"{Url}{imageNode.Element("urlBase")?.Value}_{urlExtension}";
var copyright = imageNode.Element("copyright")?.Value;
var headline = imageNode.Element("headline")?.Value;
consoleWriter.WriteLine(1, $"Image {i + 1}/{datePairs.Count} for: '{country.Name}' on {startDate}-{endDate} was: {imageUrl}");
Expand Down
29 changes: 28 additions & 1 deletion src/BingImageDownload/FileClearer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ internal class FileClearer
private readonly ConsoleWriter consoleWriter;
private readonly Paths paths;
private readonly int archiveMonths;
private readonly int deleteMonths;

public FileClearer(ConsoleWriter consoleWriter, Paths paths, int? archiveMonths)
public FileClearer(ConsoleWriter consoleWriter, Paths paths, int? archiveMonths, int? deleteMonths)
{
this.consoleWriter = consoleWriter;
this.paths = paths;
this.archiveMonths = archiveMonths ?? 1;
this.deleteMonths = deleteMonths ?? 0;
}

internal void ArchiveOldImages()
Expand Down Expand Up @@ -41,6 +43,31 @@ internal void ArchiveOldImages()
}
}

internal void DeleteOldImages()
{
try
{
if (deleteMonths <= 0)
{
return;
}

foreach (var file in Directory.GetFiles(paths.ArchivePath))
{
var fileInfo = new FileInfo(file);
if (fileInfo.CreationTimeUtc < DateTime.UtcNow.AddMonths(deleteMonths * -1))
{
consoleWriter.WriteLine($"Deleting image {fileInfo.Name}");
fileInfo.Delete();
}
}
}
catch (Exception exception)
{
consoleWriter.WriteLine("Error deleting image", exception);
}
}

public void ClearLogFiles()
{
foreach (var file in Directory.GetFiles(paths.LogPath))
Expand Down
11 changes: 8 additions & 3 deletions src/BingImageDownload/ImageFingerprinting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,14 @@ private ImageFingerprint GetImageFingerprint(string filePath)

using (var image = Image.Load<Rgb24>(histogramFile))
{
//Scale down from 1920*1080 to 96*54 (5% the size) - this will pixelate but enough to tell differences.
//This means 5,184 total pixels rather than 2,073,600.
image.Mutate(x => x.Resize(96, 54).Grayscale());
//Scale down from to 100 wide by required height to maintain aspect ratio - this will pixelate but enough to tell differences.
//This means less pixels to compare
//Also grayscale to make matching more accurate
var scaleWidth = 100;
var scale = image.Width / scaleWidth;
var scaleHeight = image.Height / scale;

image.Mutate(x => x.Resize(scaleWidth, scaleHeight).Grayscale());

for (var x = 0; x < image.Width; x++)
{
Expand Down
4 changes: 2 additions & 2 deletions src/BingImageDownload/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"BingImageDownload": {
"commandName": "Project",
"commandLineArgs": "-p \"F:\\Bing Wallpaper\" -a 1"
"commandLineArgs": "-p \"F:\\Bing Wallpaper\" -a 1 -r UHD -d 3"
}
}
}
}
5 changes: 3 additions & 2 deletions src/BingImageDownload/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static int Start(RunnerArgs runnerArgs, in CancellationToken cancellation
var serializer = new Serializer(consoleWriter);
var imageFingerprinting = new ImageFingerprinting(consoleWriter, paths, serializer);
var imagePropertyHandling = new ImagePropertyHandling();
var bingInteractionAndParsing = new BingInteractionAndParsing(consoleWriter, imageFingerprinting, imagePropertyHandling, paths, serializer);
var fileClearer = new FileClearer(consoleWriter, paths, runnerArgs.ArchiveMonths);
var bingInteractionAndParsing = new BingInteractionAndParsing(consoleWriter, imageFingerprinting, imagePropertyHandling, paths, serializer, runnerArgs.Resolution);
var fileClearer = new FileClearer(consoleWriter, paths, runnerArgs.ArchiveMonths, runnerArgs.DeleteMonths);

try
{
Expand Down Expand Up @@ -85,6 +85,7 @@ public static int Start(RunnerArgs runnerArgs, in CancellationToken cancellation
Thread.Sleep(TimeSpan.FromSeconds(5));
consoleWriter.WriteLine("Clearing up");
fileClearer.ArchiveOldImages();
fileClearer.DeleteOldImages();
fileClearer.ClearLogFiles();
fileClearer.ClearTempFolders();
}
Expand Down
6 changes: 6 additions & 0 deletions src/BingImageDownload/RunnerArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ internal class RunnerArgs
[Option("-a|--archive <VALUE>", "The number of months to archive after (Default: 1)", CommandOptionType.SingleValue)]
public int? ArchiveMonths { get; }

[Option("-d|--delete <VALUE>", "The number of months to delete after (Default: never)", CommandOptionType.SingleValue)]
public int? DeleteMonths { get; }

[Option("-r|--resolution <VALUE>", "The resolution for images (HD,FHD,UHD) (Default: FHD)", CommandOptionType.SingleValue)]
public string Resolution { get; }

private int OnExecute(CancellationToken cancellationToken) => Runner.Start(this, cancellationToken);
}
}

0 comments on commit e2f2256

Please sign in to comment.