Skip to content

Commit

Permalink
Merge pull request #95 from goodtrailer/increase-bing-res
Browse files Browse the repository at this point in the history
Bing: Use Bing API to fetch image data, and increase desired resolution
  • Loading branch information
goodtrailer authored Jan 11, 2025
2 parents 07f79fa + c967d7f commit 66b4eac
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions DailyDesktop.Providers.Bing/BingProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) Alden Wu <[email protected]>. Licensed under the MIT Licence.
// See the LICENSE file in the repository root for full licence text.

using System.Net;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using DailyDesktop.Core.Configuration;
Expand All @@ -13,30 +14,51 @@ namespace DailyDesktop.Providers.Bing
{
public class BingProvider : IProvider
{
private const string IMAGE_RELATIVE_URI_PATTERN = "(/th\\?id=)([^\"/>]*?)1920x1080.[a-z]*";
private const string AUTHOR_PATTERN = "(?<=(<div class=\"copyright\" id=\"copyright\">))(.*?)(?=(</div>))";
private const string TITLE_PATTERN = "(?<=(<meta property=\"og:title\" content=\"))(.*?)(?=(\" />))";
private const string TITLE_RELATIVE_URI_PATTERN = "(?<=(\"BackstageUrl\":\"))(.*?)(?=(\"))";
private const string DESCRIPTION_PATTERN = "(?<=(<span(.*?)id=\"iotd_desc\">))(.*?)(?=(</span>))";

public string DisplayName => "Bing";
public string Description => "Grabs Bing's featured Image of the Day, which can be found on Bing's home page.";
public string SourceUri => "https://www.bing.com";

private struct ResponseImage
{
public ResponseImage() { }

public string Url { get; set; } = "";
public string CopyrightLink { get; set; } = "";
public string Title { get; set; } = "";
public string CopyrightOnly { get; set; } = "";
public string Desc { get; set; } = "";
}

private struct Response
{
public Response() { }

public List<ResponseImage> Images { get; set; } = new List<ResponseImage>();
}

public async Task ConfigureWallpaperAsync(HttpClient client, IPublicWallpaperConfiguration wallpaperConfig, CancellationToken cancellationToken)
{
string pageHtml = await client.GetStringAsync(SourceUri, cancellationToken);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

var response = await client.GetFromJsonAsync<Response>("https://global.bing.com/HPImageArchive.aspx?format=js&idx=0&n=9&pid=hp&FORM=BEHPTB&uhd=1&uhdwidth=3840&uhdheight=2160", options, cancellationToken);

if (response.Images.Count == 0)
throw new ProviderException("Bing API response did not contain any images.");

var image = response.Images[0];

string imageRelativeUri = Regex.Match(pageHtml, IMAGE_RELATIVE_URI_PATTERN).Value;
if (string.IsNullOrWhiteSpace(imageRelativeUri))
if (string.IsNullOrWhiteSpace(image.Url))
throw new ProviderException("Didn't find a relative image URI.");

string imageUri = SourceUri + imageRelativeUri;
string imageUri = SourceUri + image.Url;

string author = Regex.Match(pageHtml, AUTHOR_PATTERN).Value;
string title = Regex.Match(pageHtml, TITLE_PATTERN).Value;
string titleUri = SourceUri + WebUtility.HtmlDecode(Regex.Unescape(Regex.Match(pageHtml, TITLE_RELATIVE_URI_PATTERN).Value)).Replace("\"", "%22");
string description = "TODAY ON BING\r\n" + Regex.Match(pageHtml, DESCRIPTION_PATTERN).Value;
string author = image.CopyrightOnly;
string title = image.Title;
string titleUri = SourceUri + image.CopyrightLink;
string description = "TODAY ON BING\r\n" + image.Desc;

await wallpaperConfig.SetImageUriAsync(imageUri, cancellationToken);
await wallpaperConfig.SetAuthorAsync(author, cancellationToken);
Expand Down

0 comments on commit 66b4eac

Please sign in to comment.