106 lines
3.8 KiB
C#
106 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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 : GallerySourceBase
|
|
{
|
|
public override string Name => "Gelbooru";
|
|
public override string Route => "gelbooru";
|
|
public override string FlyoutIconKey => "Gelbooru";
|
|
public override string HomePage => "https://gelbooru.com";
|
|
|
|
public override async Task<IEnumerable<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.Replace("&", "&"),
|
|
PreviewUrl = g[5].Value,
|
|
Tags = g[6].Value.Split(' '),
|
|
Source = Route,
|
|
IsRawPage = true
|
|
};
|
|
}
|
|
return items;
|
|
}
|
|
|
|
public override async Task<string> ResolveImageUrl(GalleryItem item)
|
|
{
|
|
var (result, error) = await NetHelper.RequestObject(item.RawUrl, @return: content =>
|
|
{
|
|
var regex = new Regex(
|
|
@"<section class=""image-container.+?" + // data-id=""(\d+?)"".+?data-tags=""([^""]+?)"".+?
|
|
@"data-width=""(\d+?)"" data-height=""(\d+?)"".+?data-uploader-id=""([^""]*?)"" " +
|
|
@"data-normalized-source=""([^""]+?)""(.|\n)*?<img .*?src=""([^""]+?)""",
|
|
RegexOptions.Multiline);
|
|
var m = regex.Match(content);
|
|
if (m.Success)
|
|
{
|
|
var g = m.Groups;
|
|
item.Width = int.Parse(g[1].Value);
|
|
item.Height = int.Parse(g[2].Value);
|
|
item.UserId = g[3].Value;
|
|
item.SourceUrl = g[4].Value;
|
|
item.RawUrl = g[6].Value;
|
|
return item.RawUrl;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
if (result == null || !string.IsNullOrEmpty(error))
|
|
{
|
|
Log.Error("gelbooru.imageUrl.resolve", $"failed to load data, error: {error}");
|
|
return null;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public override void SetCookie()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public override 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);
|
|
}
|
|
}
|
|
}
|