-
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.
- Loading branch information
1 parent
66017a1
commit 9d302be
Showing
4 changed files
with
44 additions
and
18 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
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,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 |
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,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; | ||
|
||
|
@@ -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, " + | ||
|
@@ -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; | ||
} | ||
} | ||
} |
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