Clarifying this is not free software.
This commit is contained in:
parent
1b997d731b
commit
14c51001a6
7
README.md
Normal file
7
README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# MTGPrint Telegram Bot
|
||||
|
||||
## IMPORTANT INFORMATION
|
||||
|
||||
You have right to download and study the code, but I do not give you any other right.
|
||||
|
||||
You cannot run or modify this software unless you are given permission by myself.
|
@ -1,91 +0,0 @@
|
||||
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;
|
@ -1,36 +0,0 @@
|
||||
package TgMagicPdf::Crawler::Task;
|
||||
|
||||
use v5.38.2;
|
||||
|
||||
use strict;
|
||||
use warnings use feature 'signatures';
|
||||
|
||||
use utf8;
|
||||
|
||||
use Moo;
|
||||
|
||||
use Mojo::Promise;
|
||||
|
||||
has promise => (
|
||||
is => 'ro',
|
||||
default => sub { Mojo::Promise->new },
|
||||
);
|
||||
|
||||
has kind => (
|
||||
is => 'ro',
|
||||
required => 1,
|
||||
);
|
||||
|
||||
has extra_data => (
|
||||
is => 'ro',
|
||||
default => sub { {} },
|
||||
);
|
||||
|
||||
sub new_collection( $class, $identifiers ) {
|
||||
require TgMagicPdf::Crawler;
|
||||
return $class->new(
|
||||
kind => $TgMagicPdf::Crawler::COLLECTION,
|
||||
extra_data => { identifiers => $identifiers }
|
||||
);
|
||||
}
|
||||
1;
|
Loading…
Reference in New Issue
Block a user