Skip to content

Commit

Permalink
Refactoring and adding more cultures.
Browse files Browse the repository at this point in the history
Also improving logging
  • Loading branch information
BlythMeister committed Oct 12, 2015
1 parent f05dd30 commit 1ca7622
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/BingWallpaper/App.config → src/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<appSettings>
<add key="ImageSavePath" value="C:\Users\Chris.Blyth\Bing Wallpaper"/>
<add key="ArchiveAfterMonths" value="3"/>
<add key="Countries" value="en-GB,en-US,en-AU,en-NZ,en-CA,de-DE,fr-FR,zh-CN,ja-JP"/>
<add key="PreventDuplicatesInArchive" value="True"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -14,22 +15,22 @@ static internal class BingInteractionAndParsing
private const string url = "http://bing.com";
internal static readonly string downloadPath = Path.Combine(Program.appData, "Temp");
internal static readonly List<string> urlsRetrieved = new List<string>();
internal static readonly string[] countries = ConfigurationManager.AppSettings["Countries"].Split(',');
internal static readonly List<CultureInfo> countries = new List<CultureInfo>();

internal static void GetBingImages()
{
var downloadedImages = 0;

foreach (var country in countries)
{
ConsoleWriter.WriteLine("Searching for images for {0}", country);
ConsoleWriter.WriteLine("Searching for images for {0} - {1}", country.Name, country.DisplayName);
var countryImages = 0;
var countryDuplicateImages = 0;
var currentIndex = 0;
var moreImages = true;
while (moreImages)
{
var xmlNodeList = GetImages(currentIndex, country);
var xmlNodeList = GetImages(currentIndex, country.Name);
if (xmlNodeList == null)
{
moreImages = false;
Expand All @@ -38,7 +39,8 @@ internal static void GetBingImages()
{
foreach (XmlNode xmlNode in xmlNodeList)
{
ConsoleWriter.WriteLine(1, "Image for: '{0}' on {1}-{2} using index {3}", country, xmlNode.SelectSingleNode("startdate").InnerText, xmlNode.SelectSingleNode("enddate").InnerText, currentIndex);
var imageUrl = string.Format("{0}{1}_1920x1080.jpg", url, xmlNode.SelectSingleNode("urlBase").InnerText);
ConsoleWriter.WriteLine(1, "Image for: '{0}' on {1}-{2} index {3} was: {4}", country.Name, xmlNode.SelectSingleNode("startdate").InnerText, xmlNode.SelectSingleNode("enddate").InnerText, currentIndex, imageUrl);
try
{
if (DownloadAndSaveImage(xmlNode))
Expand All @@ -62,8 +64,8 @@ internal static void GetBingImages()
}

downloadedImages += countryImages;
ConsoleWriter.WriteLine("Found {0} new images for {1}", countryImages, country);
ConsoleWriter.WriteLine("Found {0} duplicate images for {1}", countryDuplicateImages, country);
ConsoleWriter.WriteLine("Found {0} new images for {1}", countryImages, country.Name);
ConsoleWriter.WriteLine("Found {0} duplicate images for {1}", countryDuplicateImages, country.Name);
ConsoleWriter.WriteLine("");
}

Expand All @@ -72,7 +74,7 @@ internal static void GetBingImages()

internal static bool DownloadAndSaveImage(XmlNode xmlNode)
{
var fileurl = String.Format("{0}{1}_1920x1080.jpg", url, xmlNode.SelectSingleNode("urlBase").InnerText);
var fileurl = string.Format("{0}{1}_1920x1080.jpg", url, xmlNode.SelectSingleNode("urlBase").InnerText);
if (urlsRetrieved.Contains(fileurl))
{
ConsoleWriter.WriteLine(2, "Already Dowloaded Image URL");
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 14 additions & 3 deletions src/BingWallpaper/SetupAndTearDown.cs → src/SetupAndTearDown.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Linq;
using BingWallpaper;
Expand All @@ -16,15 +17,25 @@ internal static void Startup()
var logPath = Path.Combine(Program.savePath, "Logs");
if (!Directory.Exists(logPath)) Directory.CreateDirectory(logPath);
ConsoleWriter.SetupLogWriter(Path.Combine(logPath, String.Format("Log-{0}.txt", DateTime.UtcNow.ToString("yyyy-MM-dd"))));


BingInteractionAndParsing.urlsRetrieved.AddRange(Serializer.Deserialize<string>(Path.Combine(Program.appData, "urlsRetrieved.bin")));

BingInteractionAndParsing.urlsRetrieved.AddRange(Serializer.Deserialize<string>(Path.Combine(Program.appData, "urlsRetrieved.bin")));
BingInteractionAndParsing.countries.AddRange(CultureInfo.GetCultures(CultureTypes.AllCultures).Where(x=>x.Name.Contains("-") && x.Name.Length == 5));

foreach (var file in Directory.GetFiles(Program.savePath, "*.jpg"))
{
ImageHashing.AddHash(file);
}

var preventArchiveDupes = bool.Parse(ConfigurationManager.AppSettings["PreventDuplicatesInArchive"]);
if (preventArchiveDupes)
{
var archivePath = Path.Combine(Program.savePath, "Archive");
if (!Directory.Exists(archivePath)) Directory.CreateDirectory(archivePath);
foreach (var file in Directory.GetFiles(archivePath, "*.jpg"))
{
ImageHashing.AddHash(file);
}
}
foreach (var file in Directory.GetFiles(logPath))
{
var fileInfo = new FileInfo(file);
Expand Down
File renamed without changes.

0 comments on commit 1ca7622

Please sign in to comment.