Skip to content

Commit

Permalink
v2.3 - Bing Image Copyrights
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceDandy-Tama committed Oct 5, 2022
1 parent e643d3b commit e57167f
Show file tree
Hide file tree
Showing 15 changed files with 49,079 additions and 29 deletions.
7 changes: 7 additions & 0 deletions BingHelper/BingHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Reference Include="TagLibSharp">
<HintPath>..\taglib-sharp\Release\netstandard2.0\TagLibSharp.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>

</Project>
6 changes: 5 additions & 1 deletion BingHelper/BingImageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ public class BingImageData
{
public string Date = null;
public string Url = null;
public string Copyright = null;
public string CopyrightLink = null;

public BingImageData(string date, string url)
public BingImageData(string date, string url, string copyright, string copyrightLink)
{
Date = date;
Url = url;
Copyright = copyright;
CopyrightLink = copyrightLink;
}
}
}
6 changes: 2 additions & 4 deletions BingHelper/BingImageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public static async void DownloadImageOfTheDay(string directory, string strRegio
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))
Expand All @@ -37,6 +33,8 @@ public static async void DownloadImageOfTheDay(string directory, BingImageData i
await fs.WriteAsync(bytes, 0, bytes.Length);
}

BingUtils.WriteID3Tag(imagePath, imageData.Copyright);

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

Expand Down
70 changes: 66 additions & 4 deletions BingHelper/BingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public static async Task<string> GetJsonStringOfImageOfTheDay(int numOfImages =

public static bool ParseSingleImageJsonString(string jsonString, ref BingImageData imageData)
{
#if DEBUG
System.IO.File.WriteAllText("singleJson.json", jsonString);
#endif

try
{
//Parse the Data without using json implementations.
Expand All @@ -71,12 +75,23 @@ public static bool ParseSingleImageJsonString(string jsonString, ref BingImageDa
strJsonData = strJsonData[3].Split(new char[] { '"' });
string imageUrlTemp = BingUrl + strJsonData[3];

strJsonData = jsonString.Split(new char[] { '"' });
string copyrightTemp = strJsonData[25];
string copyrightLinkTemp = strJsonData[29];

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

#if DEBUG
System.IO.File.WriteAllText("data.txt", imageDateTemp);
System.IO.File.AppendAllText("data.txt", "\n" + imageUrlTemp);
System.IO.File.AppendAllText("data.txt", "\n" + copyrightTemp);
System.IO.File.AppendAllText("data.txt", "\n" + copyrightLinkTemp);
#endif

if (BingUtils.WebsiteExists(ref imageUrlTemp))
{
imageData = new BingImageData(imageDateTemp, imageUrlTemp);
imageData = new BingImageData(imageDateTemp, imageUrlTemp, copyrightTemp, copyrightLinkTemp);
return true;
}
else
Expand All @@ -99,10 +114,19 @@ public static bool ParseSingleImageJsonString(string jsonString, ref BingImageDa

public static void ParseMultipleImageJsonString(string jsonString, ref BingImageData[] imageDatas)
{

#if DEBUG
System.IO.File.WriteAllText("multipleJson.json", jsonString);
#endif

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

string[] strJsonData2 = jsonString.Split(new string[] { "\"copyright\":\"" }, StringSplitOptions.None);
string[] strJsonData3 = jsonString.Split(new string[] { "\"copyrightlink\":\"" }, StringSplitOptions.None);
int counter = 0;

for (int i = 0; i < strJsonData.Length; i++)
{
if (strJsonData[i].StartsWith("\"url\"", StringComparison.Ordinal))
Expand All @@ -112,16 +136,54 @@ public static void ParseMultipleImageJsonString(string jsonString, ref BingImage
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];
string imageUrlTemp = BingUrl + strJsonData[i].Split(new char[] { '"' })[3];

imageDatas[i] = new BingImageData(imageDateTemp, imageUrlTemp);
counter++;
string copyrightTemp = strJsonData2[counter].Split(new string[] { "\",\"copyrightlink\"" }, StringSplitOptions.None)[0];
string copyrightLinkTemp = strJsonData3[counter].Split(new string[] { "\",\"title\"" }, StringSplitOptions.None)[0];
#if DEBUG
System.IO.File.WriteAllText(imageDateTemp + ".txt", copyrightTemp);
System.IO.File.AppendAllText(imageDateTemp + ".txt", "\n" + copyrightLinkTemp);
#endif

imageDatas[i] = new BingImageData(imageDateTemp, imageUrlTemp, copyrightTemp, copyrightLinkTemp);
}
else
{
imageDatas[i] = null;
}
}
}

public static bool CheckIfID3TagExists(string imagePath)
{
TagLib.Id3v2.Tag.DefaultVersion = 3;
TagLib.Id3v2.Tag.ForceDefaultVersion = true;
TagLib.Id3v2.Tag.DefaultEncoding = TagLib.StringType.UTF8;
TagLib.Id3v2.Tag.ForceDefaultEncoding = true;

TagLib.File tagFile = TagLib.File.Create(imagePath, TagLib.ReadStyle.None);
bool result = tagFile.TagTypes != TagLib.TagTypes.None;
tagFile.Dispose();
return result;
}

public static void WriteID3Tag(string imagePath, string copyrightText)
{
//ID3 Tag Writing
TagLib.Id3v2.Tag.DefaultVersion = 3;
TagLib.Id3v2.Tag.ForceDefaultVersion = true;
TagLib.Id3v2.Tag.DefaultEncoding = TagLib.StringType.UTF8;
TagLib.Id3v2.Tag.ForceDefaultEncoding = true;

using (TagLib.File tagFile = TagLib.File.Create(imagePath, TagLib.ReadStyle.None))
{
tagFile.Mode = TagLib.File.AccessMode.Write;
TagLib.Tag tag = tagFile.GetTag(TagLib.TagTypes.XMP, true);
tag.Copyright = copyrightText;
tag.SetInfoTag();
tagFile.Save();
}
}
}
}
5 changes: 5 additions & 0 deletions ImageViewer/Documentation.htm
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ <h4>DesktopJoke.png</h4>
Not actually a command line argument, however opening an image called DesktopJoke.png will result in fun behaviour.<br>
Spoiler: It will take a screenshot and then display the screenshot instead of your desktop. Thereby confusing user, "Why can't I click on anything?"
</p>
<h4>-attemptToGetId3Tags</h4>
<p>
This attempts to get image information of images already saved to the saveFilePathBing from a third party website.<br>
Note: It doesn't always get accurate information. Some small amount of images end up getting wrong copyright informations.
</p>
</section>

<section>
Expand Down
17 changes: 16 additions & 1 deletion ImageViewer/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e57167f

Please sign in to comment.