MTGPrint/lib/TgMagicPdf/Crawler.pm

92 lines
1.9 KiB
Perl

package TgMagicPdf::Crawler;
use v5.38.2;
use strict;
use warnings;
use feature 'signatures';
use utf8;
use Moo;
use DateTime;
use TgMagicPdf::Crawler::Task;
use Mojo::UserAgent;
our $COLLECTION = 'COLLECTION_CRAWL';
has queue => (
is => 'ro',
default => sub { [] },
);
has is_blocked_queue => (
is => 'rw',
default => sub { 1 },
);
{
sub singleton {
state $crawler = __PACKAGE__->new;
return $crawler;
}
}
sub get_collection( $self, $identifiers ) {
if ( scalar @$identifiers > 75 ) {
die 'Too much identifiers fetching collection.';
}
my $task = TgMagicPdf::Crawler::Task->new_collection($identifiers);
$self->add_to_queue($task);
return $task->promise;
}
sub _add_to_queue( $self, $task ) {
push $self->queue->@*, $task;
if ( !$self->is_blocked_queue ) {
$self->_block_queue;
}
}
sub _block_queue($self) {
$self->is_blocked_queue(1);
my $id = Mojo::IOLoop->recurring(
0.1 => sub {
my $task = shift $self->queue->@*;
if ( !defined $task ) {
Mojo::IOLoop->remove($id);
$self->is_blocked_queue(0);
return;
}
$self->_dispatch_task($task);
}
);
}
sub _dispatch_task( $self, $task ) {
if ( $task->kind eq $COLLECTION ) {
$self->_dispatch_task_collection($task);
return;
}
}
sub _dispatch_task_collection( $self, $task ) {
my $identifiers = $task->extra_data->{identifiers};
my $ua = Mojo::UserAgent->new;
$ua->post_p(
'https://api.scryfall.com/cards/collection',
json => { identifiers => $identifiers }
)->then(sub($ua, $tx) {
if ($tx->result->code != 200) {
$task->promise->reject($txt->result->body);
return;
}
$task->promise->resolve($tx->result->body);
})->catch(sub ($err) {
$task->promise->reject($err);
});
}
1;