Amethyst
view release on metacpan or search on metacpan
Amethyst/Brain/Infobot/Module/Google.pm view on Meta::CPAN
package Amethyst::Brain::Infobot::Module::Google;
use strict;
use vars qw(@ISA);
use Data::Dumper;
use POE;
use POE::Component::Client::UserAgent;
use HTTP::Request::Common;
use HTTP::Response;
use WWW::Search;
use WWW::SearchResult;
use Amethyst::Message;
use Amethyst::Brain::Infobot;
use Amethyst::Brain::Infobot::Module;
@ISA = qw(Amethyst::Brain::Infobot::Module);
sub new {
my $class = shift;
my $self = $class->SUPER::new(
Name => 'Google',
Regex => qr/^(?:google|search)\s+for\s+(.*)$/i,
Usage => '(google|search) for .*',
Description => "Search Google",
@_
);
return bless $self, $class;
}
sub init {
my $self = shift;
eval {
spawn POE::Component::Client::UserAgent(
agent => 'Lynx/2.8rel.1 libwww-FM/2.14',
);
};
if ($@) {
die $@ unless $@ =~ /^alias is in use by another session/;
}
}
sub action {
my ($self, $message, $query) = @_;
my %states = map { $_ => "handler_$_" } qw(
_start response
);
print STDERR "Creating child session for google\n";
POE::Session->create(
package_states => [ ref($self) => \%states ],
args => [ $self, $message, $query ],
);
return 1;
}
sub parse_response {
my ($self, $response) = @_;
$self->{_debug} = 10;
print STDERR "Self is $self\n";
# parse the output
my ($HEADER, $HITS, $TRAILER, $POST_NEXT) = (1..10);
my @hits = ();
my $hit = undef;
my $approx_count = -1;
my $state = $HEADER;
my @lines = split(/\n/, $response->content);
foreach (@lines) {
next unless /\S/; # short circuit for blank lines
# print STDERR substr($_, 0, 70) . "\n" if $self->{_debug};
print STDERR $_ . "\n" if $self->{_debug};
if ($state == $HEADER && m/about <b>([\d,]+)<\/b>/) {
$approx_count = $1;
( run in 1.949 second using v1.01-cache-2.11-cpan-39bf76dae61 )