-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2.1 See changelog.txt for more details
- Loading branch information
1 parent
d3ef1f1
commit 9592786
Showing
13 changed files
with
551 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.