« blib/lib/auto/share/dist/Beekeeper/flood/lib/MyApp/Service/Flood/Worker.pm
↑ Summary
blib/lib/auto/share/dist/Beekeeper/scraper/lib/MyApp/Service/Scraper.pm »
| 1 | #!/usr/bin/perl -wT | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | BEGIN { unshift @INC, ($ENV{'PERL5LIB'} =~ m/([^:]+)/g); } | ||
| 7 | |||
| 8 | |||
| 9 | use MyApp::Service::Scraper; | ||
| 10 | use Getopt::Long; | ||
| 11 | |||
| 12 | my ($opt_async, $opt_help); | ||
| 13 | my $no_args = (@ARGV == 0) ? 1 : 0; | ||
| 14 | |||
| 15 | GetOptions( | ||
| 16 | "async" => \$opt_async, # --async | ||
| 17 | "help" => \$opt_help, # --help | ||
| 18 | ) or exit; | ||
| 19 | |||
| 20 | my @urls = @ARGV; | ||
| 21 | |||
| 22 | my $Help = " | ||
| 23 | Usage: client.pl [OPTIONS] [urls] | ||
| 24 | Extract titles from given urls | ||
| 25 | |||
| 26 | -a, --async process urls concurrently | ||
| 27 | -h, --help display this help and exit | ||
| 28 | |||
| 29 | Example: | ||
| 30 | |||
| 31 | ./client.pl --async https://cpan.org https://perl.org | ||
| 32 | "; | ||
| 33 | |||
| 34 | if ($opt_help || $no_args) { | ||
| 35 | print $Help; | ||
| 36 | exit; | ||
| 37 | } | ||
| 38 | |||
| 39 | if (!$opt_async) { | ||
| 40 | |||
| 41 | foreach my $url (@urls) { | ||
| 42 | |||
| 43 | # Using the synchronous client: urls will be processed one after another | ||
| 44 | |||
| 45 | my $response = MyApp::Service::Scraper->get_title( $url ); | ||
| 46 | |||
| 47 | if ($response->success) { | ||
| 48 | my $title = $response->result; | ||
| 49 | print qq'\n$url\n"$title"\n'; | ||
| 50 | } | ||
| 51 | else { | ||
| 52 | print "\n$url\n". $response->code ." ". $response->message ."\n"; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | |||
| 58 | my $cv = AnyEvent->condvar; | ||
| 59 | |||
| 60 | foreach my $url (@urls) { | ||
| 61 | |||
| 62 | # Using the asynchronous client: urls will be processed concurrently | ||
| 63 | |||
| 64 | $cv->begin; | ||
| 65 | |||
| 66 | MyApp::Service::Scraper->get_title_async( $url, sub { | ||
| 67 | my ($response) = @_; | ||
| 68 | |||
| 69 | if ($response->success) { | ||
| 70 | my $title = $response->result; | ||
| 71 | print qq'\n$url\n"$title"\n'; | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | print "\n$url\n". $response->code ." ". $response->message ."\n"; | ||
| 75 | } | ||
| 76 | |||
| 77 | $cv->end; | ||
| 78 | }); | ||
| 79 | } | ||
| 80 | |||
| 81 | $cv->recv; | ||
| 82 | } |