-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviantArtProvider.cs
61 lines (50 loc) · 3.18 KB
/
DeviantArtProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using DailyDesktop.Core.Configuration;
using DailyDesktop.Core.Providers;
using static System.Net.WebRequestMethods;
namespace DailyDesktop.Providers.DeviantArt
{
public class DeviantArtProvider : IProvider
{
private const string IMAGE_URI_PATTERN = "(?<=(<img alt=(.*?)src=\"))(.*?)(?=(\"))";
private const string AUTHOR_PATTERN = "(?<=<span property=\"creditText\".*?>).*?(?= on DeviantArt</span>)";
private const string TITLE_PATTERN = "(?<=<h1.*?>)(.*?)(?=</h1>)";
private const string TITLE_URI_PATTERN = "(?<=href=\")https://www.deviantart.com/([^\"]*?)/art/([^\"]*?)(?=\")";
private const string DESCRIPTION_PATTERN = "(?<=(<div><div class=\"legacy-journal[^\"]*\">))(.*?)(?=(</div>))";
public string DisplayName => "DeviantArt";
public string Description => "Fetches one of DeviantArt's currently " +
"featured piece from its Daily Deviations, a collection of art " +
"handpicked by the DeviantArt community and staff. These artworks " +
"highlight the best of DeviantArt from a wide variety of genres.";
public string SourceUri => "https://www.deviantart.com/daily-deviations";
public async Task ConfigureWallpaperAsync(HttpClient client, IPublicWallpaperConfiguration wallpaperConfig, CancellationToken cancellationToken)
{
// Search for image page URI from daily deviation page
string dailyDeviationHtml = await client.GetStringAsync(SourceUri, cancellationToken);
string titleUri = Regex.Match(dailyDeviationHtml, TITLE_URI_PATTERN).Value;
if (string.IsNullOrWhiteSpace(titleUri))
throw new ProviderException("Didn't find an image page URI.");
// Scrape info from image page
string pageHtml = await client.GetStringAsync(titleUri, cancellationToken);
string imageUri = Regex.Match(pageHtml, IMAGE_URI_PATTERN).Value;
if (string.IsNullOrWhiteSpace(imageUri))
throw new ProviderException("Didn't find an image URI.");
string author = Regex.Match(pageHtml, AUTHOR_PATTERN).Value;
string authorUri = Regex.Match(titleUri, "https://www.deviantart.com/.*?/").Value;
string title = Regex.Match(pageHtml, TITLE_PATTERN).Value;
string description = Regex.Replace(WebUtility.HtmlDecode(Regex.Match(pageHtml, DESCRIPTION_PATTERN).Value), "<([^<>]*?)>", "");
await wallpaperConfig.SetImageUriAsync(imageUri, cancellationToken);
await wallpaperConfig.SetAuthorAsync(author, cancellationToken);
await wallpaperConfig.SetAuthorUriAsync(authorUri, cancellationToken);
await wallpaperConfig.SetTitleAsync(title, cancellationToken);
await wallpaperConfig.SetTitleUriAsync(titleUri, cancellationToken);
await wallpaperConfig.SetDescriptionAsync(description, cancellationToken);
}
}
}