From ead5728a99430ab2397b83be0285fff06eaa2ee2 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sat, 18 Feb 2023 14:47:32 +0000 Subject: [PATCH] Implement /tags and /tag/id endpoints --- lib/PoorBooru.pm | 53 ++++++++++++++++++++++++++++++++++++++++---- public/css/style.css | 18 +++++++++++++++ views/gallery.tt | 9 ++++++++ views/index.tt | 2 +- views/tags.tt | 7 ++++++ 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 views/gallery.tt create mode 100644 views/tags.tt diff --git a/lib/PoorBooru.pm b/lib/PoorBooru.pm index 4df6219..0a46ec6 100644 --- a/lib/PoorBooru.pm +++ b/lib/PoorBooru.pm @@ -2,10 +2,25 @@ package PoorBooru; use Dancer2; use HTTP::Tiny; -our $VERSION = v0.1; +our $VERSION = v0.0.1; my $POORBOORU_API = setting("poorbooru_api"); +sub http_tiny () +{ + return HTTP::Tiny->new( + timeout => 15, + verify_SSL => true, + agent => setting("appname") . " backend " . + version::->parse($VERSION)->normal, + + default_headers => { + "Accept" => "application/json", + "Content-Type" => "application/json", + }, + ); +} + hook before_template_render => sub { my $tokens = shift; @@ -18,20 +33,50 @@ hook before_template_render => sub { get "/" => sub { template "index" => { - "title" => "main", + title => "main", }; }; get "/tags" => sub { + my $res = http_tiny()->get("$POORBOORU_API/tags"); + send_error("API error", 500) if !$res->{success}; + + my $data = decode_json($res->{content}); + my @tags = map +( { + name => $_->{name}, + count => $_->{count}, + uri => uri_for("/tag/" . $_->{name}), + } ), @$data; + + template "tags" => { + title => "Tags", + tags => \@tags, + }; }; -get "/tag/:tag_id" => sub { +get "/tag/:tag_id_or_name" => sub { + my $tag_id_or_name = route_parameters->get("tag_id_or_name"); + + my $res = http_tiny()->get("$POORBOORU_API/tag/$tag_id_or_name"); + send_error("API error", 500) if !$res->{success}; + + my $data = decode_json($res->{content}); + my @media = map +( { + # XXX point to a cache + image_src => $_->{download_uri}, + view_uri => uri_for("/view/" . $_->{id}), + } ), @{$data->{media}}; + + template "gallery" => { + title => $data->{name}, + media => \@media, + }; }; get "/random" => sub { }; -get "/image/:image_id" => sub { +get "/view/:media_id" => sub { }; true; diff --git a/public/css/style.css b/public/css/style.css index e55363f..928b59e 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -135,6 +135,20 @@ nav { gap: 0 1rem; } +/* 0.5rem padding compensates the unused 1rem gap at the end. */ +.gallery { + align-items: center; + gap: 1rem; + padding: 0 0.5rem; +} + +.gallery-image { + max-height: 24rem; + max-width: 14rem; + height: 100%; + width: 100%; +} + .flex-c-horizontal { display: flex; } @@ -150,6 +164,10 @@ nav { align-items: center; } +.flex-c-wrap { + flex-wrap: wrap; +} + .flex-i-fullsize { flex: auto; } diff --git a/views/gallery.tt b/views/gallery.tt new file mode 100644 index 0000000..ef34cbb --- /dev/null +++ b/views/gallery.tt @@ -0,0 +1,9 @@ +

[% title %]

+ + diff --git a/views/index.tt b/views/index.tt index cd1d1e6..7e4980d 100644 --- a/views/index.tt +++ b/views/index.tt @@ -1 +1 @@ -

No content

+

PoorBooru

diff --git a/views/tags.tt b/views/tags.tt new file mode 100644 index 0000000..c35dee8 --- /dev/null +++ b/views/tags.tt @@ -0,0 +1,7 @@ +

Tags

+ +

+[% FOREACH tag IN tags -%] +[% tag.name %] ([% tag.count %]) +[% END -%] +