Skip to content

Commit

Permalink
Update WikimediaCommonsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
goodtrailer committed May 10, 2021
1 parent 66017a1 commit 9d302be
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
14 changes: 7 additions & 7 deletions DailyDesktop.Core.Providers.Bing/BingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public WallpaperInfo GetWallpaperInfo()

WallpaperInfo wallpaper = new WallpaperInfo
{
ImageUri = imageUri,
Date = DateTime.Now,
Author = author,
AuthorUri = null,
Title = title,
TitleUri = titleUri,
Description = description,
ImageUri = imageUri,
Date = DateTime.Now,
Author = author,
AuthorUri = null,
Title = title,
TitleUri = titleUri,
Description = description,
};

return wallpaper;
Expand Down
2 changes: 1 addition & 1 deletion DailyDesktop.Core.Providers.WikimediaCommons/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Wikimedia Commons
#### *DailyDesktop.Core.Providers.WikimediaCommons @ COMMONS*
#### *DailyDesktop.Core.Providers.WikimediaCommons*

Takes the Picture of the day that has been selected for display on the front page of Wikimedia Commons, a repository of free-use images and related media. Pictures of the day are chosen from Featured pictures of Commons, the finest images on Wikimedia Commons selected by community consensus from a collection of Featured picture candidates.<br />
https://commons.wikimedia.org/wiki/Commons:POTD
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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;
using System.Net;
using System.Text.RegularExpressions;

Expand All @@ -9,10 +10,13 @@ namespace DailyDesktop.Core.Providers.WikimediaCommons
public class WikimediaCommonsProvider : IProvider
{
private const string BASE_URI = "https://commons.wikimedia.org/";
private const string RELATIVE_IMAGE_PAGE_URI_PATTERN = "(?<=<a href=\")(.*?)(?=\" class=\"image\">)";
private const string IMAGE_URI_PATTERN = "(?<=(<div class=\"fullMedia\">)(.*?)(href=\"))(.*?)(?=\")";
private const string AUTHOR_PATTERN = "(?<=(Author</td>([\\S\\s]*)User:(.*)\">))(.*?)(?=(</a>))";
private const string AUTHOR_RELATIVE_URI_PATTERN = "(?<=(Author</td>[\\S\\s]*?<a href=\"))/wiki/User:(.*?)(?=(\"))";
private const string TITLE = "Picture";
private const string TITLE_RELATIVE_URI_PATTERN = "(?<=<a href=\")(.*?)(?=\" class=\"image\">)";
private const string DESCRIPTION_PATTERN = "(?<=(<div(.*?)lang=\"en\"><span(.*?)><b>(.*?)</b></span>))(.*?)(?=(</div>))";

public string Key => "COMMONS";
public string DisplayName => "Wikimedia Commons";
public string Description => "Takes the Picture of the day that has " +
"been selected for display on the front page of Wikimedia Commons, " +
Expand All @@ -22,30 +26,52 @@ public class WikimediaCommonsProvider : IProvider
"a collection of Featured picture candidates.";
public string SourceUri => "https://commons.wikimedia.org/wiki/Commons:POTD";

public string GetImageUri()
public WallpaperInfo GetWallpaperInfo()
{
string potdHtml = string.Empty;
using (WebClient client = new WebClient())
{
potdHtml = client.DownloadString(SourceUri);
}
Match relativeImagePageUriMatch = Regex.Match(potdHtml, RELATIVE_IMAGE_PAGE_URI_PATTERN);
string relativeImagePageUri = relativeImagePageUriMatch.Value;
if (string.IsNullOrWhiteSpace(relativeImagePageUri))
Match titleRelativeUriMatch = Regex.Match(potdHtml, TITLE_RELATIVE_URI_PATTERN);
string titleRelativeUri = titleRelativeUriMatch.Value;
if (string.IsNullOrWhiteSpace(titleRelativeUri))
throw new ProviderException("Didn't find a relative image page URI.");
string imagePageUri = BASE_URI + relativeImagePageUri;
string titleUri = BASE_URI + titleRelativeUri;

string imagePageHtml = string.Empty;
using (WebClient client = new WebClient())
{
imagePageHtml = client.DownloadString(imagePageUri);
imagePageHtml = client.DownloadString(titleUri);
}
Match imageUriMatch = Regex.Match(imagePageHtml, IMAGE_URI_PATTERN);
string imageUri = imageUriMatch.Value;
if (string.IsNullOrWhiteSpace(imageUri))
throw new ProviderException("Didn't find an image URI.");

return imageUri;
Match authorMatch = Regex.Match(imagePageHtml, AUTHOR_PATTERN);
string author = authorMatch.Value;

Match authorRelativeUriMatch = Regex.Match(imagePageHtml, AUTHOR_RELATIVE_URI_PATTERN);
string authorRelativeUri = authorRelativeUriMatch.Value;
string authorUri = string.IsNullOrWhiteSpace(authorRelativeUri) ? null : BASE_URI + authorRelativeUri;

Match descriptionMatch = Regex.Match(imagePageHtml, DESCRIPTION_PATTERN);
string description = descriptionMatch.Value;
description = Regex.Replace(description, "<([^<>]*?)>", "");

WallpaperInfo wallpaper = new WallpaperInfo
{
ImageUri = imageUri,
Date = DateTime.Now,
Author = author,
AuthorUri = authorUri,
Title = TITLE,
TitleUri = titleUri,
Description = description,
};

return wallpaper;
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Wallpapers are actually downloaded/applied by DailyDesktop.Task. To debug a prov
* Pixiv @ [/DailyDesktop.Core.Providers.Pixiv/](/DailyDesktop.Core.Providers.Pixiv/)
* r/EarthPorn (SFW) @ [/DailyDesktop.Core.Providers.RedditEarthPorn/](/DailyDesktop.Core.Providers.RedditEarthPorn/)
* Unsplash @ [/DailyDesktop.Core.Providers.Unsplash/](/DailyDesktop.Core.Providers.Unsplash/)
* Wikimedia Commons @ [/DailyDesktop.Core.Providers.WikimediaCommons/](/DailyDesktop.Core.Providers.WikimediaCommons/)
* Wikimedia Commons @ [/DailyDesktop.Core.Providers.WikimediaCommons/](/DailyDesktop.Core.Providers.WikimediaCommons/)

#### Planned
* National Geographic
Expand Down

0 comments on commit 9d302be

Please sign in to comment.