« blib/lib/Bot/BasicBot/Pluggable/Module.pm
↑ Summary
blib/lib/Bot/BasicBot/Pluggable/Module/Loader.pm »
| 1 | package Bot::BasicBot::Pluggable::Module::Google; | ||
| 2 | $Bot::BasicBot::Pluggable::Module::Google::VERSION = '1.20'; | ||
| 3 | use base qw(Bot::BasicBot::Pluggable::Module); | ||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | |||
| 7 | use Net::Google; | ||
| 8 | |||
| 9 | sub init { | ||
| 10 | my $self = shift; | ||
| 11 | $self->config( | ||
| 12 | { | ||
| 13 | user_google_key => "** SET ME FOR GOOGLE LOOKUPS **", | ||
| 14 | user_languages => "en", | ||
| 15 | user_num_results => 3, | ||
| 16 | user_require_addressing => 1, | ||
| 17 | } | ||
| 18 | ); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub help { | ||
| 22 | return | ||
| 23 | "Searches Google for terms and spellings. Usage: google <terms>, spell <words>."; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub told { | ||
| 27 | my ( $self, $mess ) = @_; | ||
| 28 | my $body = $mess->{body}; | ||
| 29 | |||
| 30 | return | ||
| 31 | if ( $self->get("user_require_addressing") and not $mess->{address} ); | ||
| 32 | |||
| 33 | my ( $command, $param ) = split( /\s+/, $body, 2 ); | ||
| 34 | $command = lc($command); | ||
| 35 | |||
| 36 | if ( $command eq "google" ) { | ||
| 37 | return | ||
| 38 | "No Google key has been set! Set it with '!set Google google_key <key>'." | ||
| 39 | unless $self->get("user_google_key"); | ||
| 40 | return | ||
| 41 | "Your configuration has exceeded the maximum number of allowed Google results (10)." | ||
| 42 | if $self->get("user_num_results") > 10; | ||
| 43 | |||
| 44 | my $google = Net::Google->new( key => $self->get("user_google_key") ); | ||
| 45 | my $search = $google->search( | ||
| 46 | lr => qw($self->get("user_languages")), | ||
| 47 | max_results => $self->get("user_num_results") | ||
| 48 | ); | ||
| 49 | $search->query( split( /\s+/, $param ) ); | ||
| 50 | |||
| 51 | my $res; # magical concatenation of all results. | ||
| 52 | $res .= $_->title . ": " . $_->URL . "\n" for @{ $search->results() }; | ||
| 53 | $res =~ s/<[^>]+>//g; # remove the bolded search terms. | ||
| 54 | |||
| 55 | return $res ? $res : "No results for \'$param\'."; | ||
| 56 | |||
| 57 | } | ||
| 58 | elsif ( $command eq "spell" ) { | ||
| 59 | return | ||
| 60 | "No Google key has been set! Set it with '!set Google google_key <key>'." | ||
| 61 | unless $self->get("user_google_key"); | ||
| 62 | my $google = Net::Google->new( key => $self->get("user_google_key") ); | ||
| 63 | my $res = $google->spelling( phrase => $param )->suggest(); | ||
| 64 | return $res ? $res : "No results for \'$param\'."; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | __END__ | ||
| 71 | |||
| 72 | =head1 NAME | ||
| 73 | |||
| 74 | Bot::BasicBot::Pluggable::Module::Google - searches Google for terms and spellings | ||
| 75 | |||
| 76 | =head1 VERSION | ||
| 77 | |||
| 78 | version 1.20 | ||
| 79 | |||
| 80 | =head1 IRC USAGE | ||
| 81 | |||
| 82 | =over 4 | ||
| 83 | |||
| 84 | =item google <terms> | ||
| 85 | |||
| 86 | Returns Google hits for the terms given. | ||
| 87 | |||
| 88 | =item spell <term> | ||
| 89 | |||
| 90 | Returns a Google spelling suggestion for the term given. | ||
| 91 | |||
| 92 | =back | ||
| 93 | |||
| 94 | =head1 VARS | ||
| 95 | |||
| 96 | To set module variables, use L<Bot::BasicBot::Pluggable::Module::Vars>. | ||
| 97 | |||
| 98 | =over 4 | ||
| 99 | |||
| 100 | =item google_key | ||
| 101 | |||
| 102 | A valid Google API key is required for lookups. | ||
| 103 | |||
| 104 | =item languages | ||
| 105 | |||
| 106 | Defaults to 'en'; a space-separated list of language restrictions. | ||
| 107 | |||
| 108 | =item num_results | ||
| 109 | |||
| 110 | Defaults to 3; the number of Google search results to return (maximum 10). | ||
| 111 | |||
| 112 | =item require_addressing | ||
| 113 | |||
| 114 | Defaults to 1; whether you need to address the bot for Google searches. | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =head1 REQUIREMENTS | ||
| 119 | |||
| 120 | L<Net::Google> | ||
| 121 | |||
| 122 | L<http://www.google.com/apis/> | ||
| 123 | |||
| 124 | =head1 AUTHOR | ||
| 125 | |||
| 126 | Mario Domgoergen <mdom@cpan.org> | ||
| 127 | |||
| 128 | This program is free software; you can redistribute it | ||
| 129 | and/or modify it under the same terms as Perl itself. |