mirror of
https://github.com/perlbot/perlbuut
synced 2025-06-07 10:45:40 -04:00
adding google plugin
This commit is contained in:
parent
5e1577f54b
commit
a3509d5aeb
1 changed files with 36 additions and 0 deletions
36
plugins/google.pm
Normal file
36
plugins/google.pm
Normal file
|
@ -0,0 +1,36 @@
|
|||
use LWP::UserAgent;
|
||||
use HTML::TreeBuilder;
|
||||
use URI::Escape qw/uri_escape/;
|
||||
|
||||
sub {
|
||||
my( $said )= @_;
|
||||
my $query = uri_escape( $said->{body} );
|
||||
|
||||
my $ua = LWP::UserAgent->new( agent => "Mozilla 5.0" );
|
||||
|
||||
my $resp = $ua->get( "http://google.com/search?q=$query" );
|
||||
|
||||
if( not $resp->is_success ) {
|
||||
print "Sorry, got a " . $resp->code . " from google. ";
|
||||
return;
|
||||
}
|
||||
|
||||
my $tree = HTML::TreeBuilder->new_from_content( $resp->content );
|
||||
|
||||
my $first = $tree->look_down( class => "l" );
|
||||
my $first_desc = $tree->look_down( class => "s" );
|
||||
|
||||
print $first->attr('href'), " - ";
|
||||
print $first->as_text;
|
||||
print " ";
|
||||
|
||||
for( $first_desc->content_list ) {
|
||||
last if ref $_ and $_->tag eq 'cite';
|
||||
|
||||
print ref $_ ? $_->as_text : $_;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
__DATA__
|
||||
google <Query>; Returns the link and description of the first result from querying google.
|
Loading…
Add table
Reference in a new issue