PoorBooru/lib/PoorBooru.pm

83 lines
1.7 KiB
Perl
Raw Normal View History

2023-02-18 10:49:05 +01:00
package PoorBooru;
use Dancer2;
use HTTP::Tiny;
2023-02-18 15:47:32 +01:00
our $VERSION = v0.0.1;
2023-02-18 10:49:05 +01:00
my $POORBOORU_API = setting("poorbooru_api");
2023-02-18 15:47:32 +01:00
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",
},
);
}
2023-02-18 10:49:05 +01:00
hook before_template_render => sub {
my $tokens = shift;
$tokens->{uris}->{root} = uri_for("/");
$tokens->{uris}->{login} = uri_for("/login");
$tokens->{uris}->{logout} = uri_for("/logout");
$tokens->{uris}->{random} = uri_for("/random");
$tokens->{uris}->{tags} = uri_for("/tags");
};
get "/" => sub {
template "index" => {
2023-02-18 15:47:32 +01:00
title => "main",
2023-02-18 10:49:05 +01:00
};
};
get "/tags" => sub {
2023-02-18 15:47:32 +01:00
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,
};
2023-02-18 10:49:05 +01:00
};
2023-02-18 15:47:32 +01:00
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
2023-02-18 16:05:52 +01:00
image_src => $POORBOORU_API . $_->{download_path},
2023-02-18 15:47:32 +01:00
view_uri => uri_for("/view/" . $_->{id}),
} ), @{$data->{media}};
template "gallery" => {
title => $data->{name},
media => \@media,
};
2023-02-18 10:49:05 +01:00
};
get "/random" => sub {
};
2023-02-18 15:47:32 +01:00
get "/view/:media_id" => sub {
2023-02-18 10:49:05 +01:00
};
true;