combine projects into one
This commit is contained in:
71
Gallery.Share/Sources/Gelbooru/GallerySource.cs
Normal file
71
Gallery.Share/Sources/Gelbooru/GallerySource.cs
Normal file
@ -0,0 +1,71 @@
|
||||
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.Gelbooru
|
||||
{
|
||||
public class GallerySource : IGallerySource
|
||||
{
|
||||
public string Name => "Gelbooru";
|
||||
public string Route => "gelbooru";
|
||||
public string FlyoutIconKey => "Gelbooru";
|
||||
public string HomePage => "https://gelbooru.com";
|
||||
|
||||
public async Task<GalleryItem[]> GetRecentItemsAsync(int page)
|
||||
{
|
||||
var offset = (page - 1) * 42;
|
||||
var url = $"https://gelbooru.com/index.php?page=post&s=list&tags=all&pid={offset}";
|
||||
var (result, error) = await NetHelper.RequestObject(url, @return: content => ResolveGalleryItems(content));
|
||||
|
||||
if (result == null || !string.IsNullOrEmpty(error))
|
||||
{
|
||||
Log.Error("gelbooru.content.load", $"failed to load content array, error: {error}");
|
||||
return null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private GalleryItem[] ResolveGalleryItems(string content)
|
||||
{
|
||||
var regex = new Regex(
|
||||
@"<article(.|\n)+?<a id=""p(\d+?)"" href=""([^""]+?)"">(.|\n)+?<img src=""([^""]+?)"" title=""([^""]+?)""",
|
||||
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[2].Value))
|
||||
{
|
||||
RawUrl = g[3].Value,
|
||||
PreviewUrl = g[5].Value,
|
||||
Tags = g[6].Value.Split(' '),
|
||||
IsRawPage = true
|
||||
};
|
||||
}
|
||||
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