combine projects into one
This commit is contained in:
75
Gallery.Share/Sources/Danbooru/GallerySource.cs
Normal file
75
Gallery.Share/Sources/Danbooru/GallerySource.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Gallery.Util;
|
||||
using Gallery.Util.Interface;
|
||||
using Gallery.Util.Model;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Gallery.Sources.Danbooru
|
||||
{
|
||||
public class GallerySource : IGallerySource
|
||||
{
|
||||
public string Name => "Danbooru";
|
||||
public string Route => "danbooru";
|
||||
public string FlyoutIconKey => "Danbooru";
|
||||
public string HomePage => "https://danbooru.donmai.us";
|
||||
|
||||
public async Task<GalleryItem[]> GetRecentItemsAsync(int page)
|
||||
{
|
||||
var url = $"https://danbooru.donmai.us/posts?page={page}";
|
||||
var (result, error) = await NetHelper.RequestObject(url, @return: content => ResolveGalleryItems(content));
|
||||
|
||||
if (result == null || !string.IsNullOrEmpty(error))
|
||||
{
|
||||
Log.Error("danbooru.content.load", $"failed to load content array, error: {error}");
|
||||
return null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private GalleryItem[] ResolveGalleryItems(string content)
|
||||
{
|
||||
var regex = new Regex(
|
||||
@"<article id=""post_\d+"".+?data-id=""(\d+)"".+?data-tags=""([^""]+?)"".+?" +
|
||||
@"data-width=""(\d+?)"" data-height=""(\d+?)"".+?data-source=""([^""]+?)"".+?" +
|
||||
@"data-uploader-id=""(\d+?)"".+?data-normalized-source=""([^""]+?)"".+?" +
|
||||
@"data-file-url=""([^""]+?)"".+?data-large-file-url=""([^""]+?)""", RegexOptions.Multiline);
|
||||
var matches = regex.Matches(content);
|
||||
var items = new GalleryItem[matches.Count];
|
||||
for (var i = 0; i < items.Length; i++)
|
||||
{
|
||||
var g = matches[i].Groups;
|
||||
items[i] = new GalleryItem(int.Parse(g[1].Value))
|
||||
{
|
||||
Tags = g[2].Value.Split(' '),
|
||||
Width = int.Parse(g[3].Value),
|
||||
Height = int.Parse(g[4].Value),
|
||||
UserId = g[6].Value,
|
||||
Source = g[7].Value,
|
||||
RawUrl = g[8].Value,
|
||||
PreviewUrl = g[9].Value
|
||||
};
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
public void SetCookie()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void InitDynamicResources(string family, ResourceDictionary light, ResourceDictionary dark)
|
||||
{
|
||||
var icon = new FontImageSource
|
||||
{
|
||||
FontFamily = family,
|
||||
Glyph = "\uf5d2",
|
||||
Size = 18.0
|
||||
};
|
||||
light.Add(FlyoutIconKey, icon);
|
||||
dark.Add(FlyoutIconKey, icon);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user