From 14c51001a6af9be58b8d58c2f0ff184b68dfff08 Mon Sep 17 00:00:00 2001 From: Sergiotarxz Date: Wed, 22 May 2024 23:15:55 +0200 Subject: [PATCH] Clarifying this is not free software. --- README.md | 7 +++ lib/TgMagicPdf/Crawler.pm | 91 ---------------------------------- lib/TgMagicPdf/Crawler/Task.pm | 36 -------------- 3 files changed, 7 insertions(+), 127 deletions(-) create mode 100644 README.md delete mode 100644 lib/TgMagicPdf/Crawler.pm delete mode 100644 lib/TgMagicPdf/Crawler/Task.pm diff --git a/README.md b/README.md new file mode 100644 index 0000000..3a1020f --- /dev/null +++ b/README.md @@ -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. diff --git a/lib/TgMagicPdf/Crawler.pm b/lib/TgMagicPdf/Crawler.pm deleted file mode 100644 index bc8fc56..0000000 --- a/lib/TgMagicPdf/Crawler.pm +++ /dev/null @@ -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; diff --git a/lib/TgMagicPdf/Crawler/Task.pm b/lib/TgMagicPdf/Crawler/Task.pm deleted file mode 100644 index 0eb1601..0000000 --- a/lib/TgMagicPdf/Crawler/Task.pm +++ /dev/null @@ -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;