Skip to content

Commit

Permalink
2.1 See changelog.txt for more details
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceDandy-Tama committed Mar 18, 2022
1 parent d3ef1f1 commit 9592786
Show file tree
Hide file tree
Showing 13 changed files with 551 additions and 158 deletions.
14 changes: 14 additions & 0 deletions ArdaBing/BingImageData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace ArdaBing
{
public class BingImageData
{
public string Date = null;
public string Url = null;

public BingImageData(string date, string url)
{
Date = date;
Url = url;
}
}
}
18 changes: 18 additions & 0 deletions ArdaBing/BingImageDownloadedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace ArdaBing
{
public delegate void BingImageDownloadedEventHandler(object sender, BingImageDownloadedEventArgs args);

public class BingImageDownloadedEventArgs : EventArgs
{
public BingImageDownloadedEventArgs(BingImageData imageData, string imagePath)
{
ImageData = imageData;
ImagePath = imagePath;
}

public BingImageData ImageData { get; set; }
public string ImagePath { get; set; }
}
}
49 changes: 49 additions & 0 deletions ArdaBing/BingImageDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace ArdaBing
{
public class BingImageDownloader
{
public static event BingImageDownloadedEventHandler OnImageDownloadedAndSaved;

public static async void DownloadImageOfTheDay(string directory, string strRegion = "en-US")
{
string jsonString = await BingUtils.GetJsonStringOfImageOfTheDay(1, strRegion);

BingImageData imageData = null;
if (BingUtils.ParseSingleImageJsonString(jsonString, ref imageData))
DownloadImageOfTheDay(directory, imageData);
}
public static void DownloadImageOfTheDay(string directory, string imageDate, string imageUrl)
{
DownloadImageOfTheDay(directory, new BingImageData(imageDate, imageUrl));
}
public static async void DownloadImageOfTheDay(string directory, BingImageData imageData, bool abortIfFileExists = true)
{
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);

string imagePath = Path.Combine(directory, imageData.Date + ".jpg");

if (!(abortIfFileExists && File.Exists(imagePath)))
{
byte[] bytes = await GetImageOfTheDayData(imageData);

using (FileStream fs = new FileStream(imagePath, FileMode.Create))
await fs.WriteAsync(bytes, 0, bytes.Length);
}

OnImageDownloadedAndSaved?.Invoke(null, new BingImageDownloadedEventArgs(imageData, imagePath));
}

public static async Task<byte[]> GetImageOfTheDayData(BingImageData imageData)
{
using (WebClient wc = new WebClient())
return await wc.DownloadDataTaskAsync(imageData.Url);
}
}
}
127 changes: 127 additions & 0 deletions ArdaBing/BingUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace ArdaBing
{
public static class BingUtils
{
public const string BingUrl = "https://www.bing.com";

public static bool WebsiteExists(ref string url)
{
try
{
WebRequest request = WebRequest.Create(url);
request.Method = "HEAD";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
return response.StatusCode == HttpStatusCode.OK;
}
catch
{
return false;
}
}
public static async Task<bool> WebsiteExists(string url)
{
try
{
WebRequest request = WebRequest.Create(url);
request.Method = "HEAD";
using (HttpWebResponse response = (HttpWebResponse)(await request.GetResponseAsync()))
return response.StatusCode == HttpStatusCode.OK;
}
catch
{
return false;
}
}

public static async Task<string> GetJsonStringOfImageOfTheDay(int numOfImages = 1, string strRegion = "en-US")
{
string strBingImageURL;
if(strRegion == null)
strBingImageURL = string.Format("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n={0}", numOfImages);
else
strBingImageURL = string.Format("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n={0}&mkt={1}", numOfImages, strRegion);

using (HttpClient client = new HttpClient())
{
// Using an Async call makes sure the app is responsive during the time the response is fetched.
// GetAsync sends an Async GET request to the Specified URI.
using (HttpResponseMessage response = await client.GetAsync(new Uri(strBingImageURL)))
{

// Content property get or sets the content of a HTTP response message.
// ReadAsStringAsync is a method of the HttpContent which asynchronously
// reads the content of the HTTP Response and returns as a string.
return await response.Content.ReadAsStringAsync();
}
}
}

public static bool ParseSingleImageJsonString(string jsonString, ref BingImageData imageData)
{
try
{
//Parse the Data without using json implementations.
string[] strJsonData = jsonString.Split(new char[] { ',' });
string imageDateTemp = strJsonData[0].Split(new char[] { '"' })[5];
strJsonData = strJsonData[3].Split(new char[] { '"' });
string imageUrlTemp = BingUrl + strJsonData[3];

if (!BingUtils.WebsiteExists(ref imageUrlTemp))
imageUrlTemp = BingUrl + strJsonData[2];

if (BingUtils.WebsiteExists(ref imageUrlTemp))
{
imageData = new BingImageData(imageDateTemp, imageUrlTemp);
return true;
}
else
{
throw new InvalidBingUrlException(imageUrlTemp);
}
}
#if DEBUG
catch(Exception e)
{
throw new Exception(e.Message, e);
}
#else
catch
{
return false;
}
#endif
}

public static void ParseMultipleImageJsonString(string jsonString, ref BingImageData[] imageDatas)
{
//Parse the Data without using json implementations.
string[] strJsonData = jsonString.Split(new char[] { ',' });
imageDatas = new BingImageData[strJsonData.Length];

for (int i = 0; i < strJsonData.Length; i++)
{
if (strJsonData[i].StartsWith("\"url\"", StringComparison.Ordinal))
{
string imageDateTemp;
if (strJsonData[i - 3].StartsWith("{\"images\"", StringComparison.Ordinal))
imageDateTemp = strJsonData[i - 3].Split(new char[] { '"' })[5];
else
imageDateTemp = strJsonData[i - 3].Split(new char[] { '"' })[3];
string[] strJsonData2 = strJsonData[i].Split(new char[] { '"' });
string imageUrlTemp = BingUrl + strJsonData2[3];

imageDatas[i] = new BingImageData(imageDateTemp, imageUrlTemp);
}
else
{
imageDatas[i] = null;
}
}
}
}
}
15 changes: 15 additions & 0 deletions ArdaBing/InvalidBingUrlException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace ArdaBing
{
[Serializable]
class InvalidBingUrlException : Exception
{
public InvalidBingUrlException() { }

public InvalidBingUrlException(string url) : base(String.Format("Invalid Bing Url: {0}", url))
{

}
}
}
6 changes: 6 additions & 0 deletions ArdaViewer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.28803.452
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArdaViewer", "ArdaViewer\ArdaViewer.csproj", "{CD4A7E5D-4414-4CB5-98B8-00CCD25C8D08}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArdaBing", "ArdaBing\ArdaBing.csproj", "{C47D39CF-9A23-40D3-A427-617AD69AB12D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{CD4A7E5D-4414-4CB5-98B8-00CCD25C8D08}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD4A7E5D-4414-4CB5-98B8-00CCD25C8D08}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD4A7E5D-4414-4CB5-98B8-00CCD25C8D08}.Release|Any CPU.Build.0 = Release|Any CPU
{C47D39CF-9A23-40D3-A427-617AD69AB12D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C47D39CF-9A23-40D3-A427-617AD69AB12D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C47D39CF-9A23-40D3-A427-617AD69AB12D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C47D39CF-9A23-40D3-A427-617AD69AB12D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion ArdaViewer/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
</configuration>
12 changes: 11 additions & 1 deletion ArdaViewer/ArdaViewer Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
**2.1:** (FUTURE)
**2.2:** (FUTURE)

- Settings Menu
- Updated Original Black Color Scheme
- Windows Accent Color Scheme
- .webp file format capability added

**2.1:**

- ArdaBing Created (Improved Bing Functionality)
- Ctrl + B downloads last 8 images of the day from Bing
- -silentBingMultiple command added as a multiple equivalent of -silentBing command
- default SaveFilePathBing is now "My Pictures/Bing Images" instead of "Desktop"
- -saveFilePathBing command added (example: ./ArdaViewer.exe -saveFilePathBing="C:\My Images")
- Ctrl + O pops up Open File Dialog
- Documentation.htm added

**2.0:**

- BUGFIX: next&previous image button functionality fixed
Expand Down
11 changes: 10 additions & 1 deletion ArdaViewer/ArdaViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ArdaViewer</RootNamespace>
<AssemblyName>ArdaViewer</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<PublishUrl>publish\</PublishUrl>
Expand Down Expand Up @@ -114,6 +114,9 @@
</ItemGroup>
<ItemGroup>
<Content Include="arda.ico" />
<None Include="Documentation.htm">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ArdaViewer Changelog.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -144,6 +147,12 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ArdaBing\ArdaBing.csproj">
<Project>{C47D39CF-9A23-40D3-A427-617AD69AB12D}</Project>
<Name>ArdaBing</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading

0 comments on commit 9592786

Please sign in to comment.