mirror of
https://github.com/pragma-/pbot.git
synced 2024-12-23 19:22:40 +01:00
Convert google module to GoogleSearch plugin
This commit is contained in:
parent
5c9f185af6
commit
8b82babde3
104
Plugins/GoogleSearch.pm
Normal file
104
Plugins/GoogleSearch.pm
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
#!/usr/bin/perl
|
||||||
|
|
||||||
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package Plugins::GoogleSearch;
|
||||||
|
|
||||||
|
use warnings;
|
||||||
|
use strict;
|
||||||
|
|
||||||
|
use WWW::Google::CustomSearch;
|
||||||
|
use HTML::Entities;
|
||||||
|
|
||||||
|
use Carp ();
|
||||||
|
|
||||||
|
sub new {
|
||||||
|
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref $_[1] eq 'HASH';
|
||||||
|
my ($class, %conf) = @_;
|
||||||
|
my $self = bless {}, $class;
|
||||||
|
$self->initialize(%conf);
|
||||||
|
return $self;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub initialize {
|
||||||
|
my ($self, %conf) = @_;
|
||||||
|
|
||||||
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
||||||
|
|
||||||
|
$self->{pbot}->{registry}->add_default('text', 'googlesearch', 'api_key', '');
|
||||||
|
$self->{pbot}->{registry}->add_default('text', 'googlesearch', 'context', '');
|
||||||
|
|
||||||
|
$self->{pbot}->{registry}->set_default('googlesearch', 'api_key', 'private', 1);
|
||||||
|
$self->{pbot}->{registry}->set_default('googlesearch', 'context', 'private', 1);
|
||||||
|
|
||||||
|
$self->{pbot}->{commands}->register(sub { $self->googlesearch(@_) }, 'google', 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
sub unload {
|
||||||
|
my $self = shift;
|
||||||
|
$self->{pbot}->{commands}->unregister('google');
|
||||||
|
}
|
||||||
|
|
||||||
|
sub googlesearch {
|
||||||
|
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
|
||||||
|
|
||||||
|
if (not length $arguments) {
|
||||||
|
return "Usage: google [number of results] query\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
my $matches = 1;
|
||||||
|
|
||||||
|
if ($arguments =~ s/^([0-9]+)//) {
|
||||||
|
$matches = $1;
|
||||||
|
}
|
||||||
|
|
||||||
|
my $api_key = $self->{pbot}->{registry}->get_value('googlesearch', 'api_key'); # https://developers.google.com/custom-search/v1/overview
|
||||||
|
my $cx = $self->{pbot}->{registry}->get_value('googlesearch', 'context'); # https://cse.google.com/all
|
||||||
|
|
||||||
|
if (not length $api_key) {
|
||||||
|
return "$nick: Registry item googlesearch.api_key is not set. See https://developers.google.com/custom-search/v1/overview to get an API key.";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (not length $cx) {
|
||||||
|
return "$nick: Registry item googlesearch.context is not set. See https://cse.google.com/all to set up a context.";
|
||||||
|
}
|
||||||
|
|
||||||
|
my $engine = WWW::Google::CustomSearch->new(api_key => $api_key, cx => $cx, quotaUser => "$nick!$user\@$host");
|
||||||
|
|
||||||
|
if ($arguments =~ m/(.*)\svs\s(.*)/i) {
|
||||||
|
my ($a, $b) = ($1, $2);
|
||||||
|
my $result1 = $engine->search("\"$a\" -\"$b\"");
|
||||||
|
my $result2 = $engine->search("\"$b\" -\"$a\"");
|
||||||
|
|
||||||
|
if (not defined $result1 or not defined $result1->items or not @{$result1->items}) {
|
||||||
|
return "$nick: No results for $a";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (not defined $result2 or not defined $result2->items or not @{$result2->items}) {
|
||||||
|
return "$nick: No results for $b";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "$nick: $a: (", $result1->formattedTotalResults, ") ", decode_entities $result1->items->[0]->title, " <", $result1->items->[0]->link, "> VS $b: (", $result2->formattedTotalResults, ") ", decode_entities $result2->items->[0]->title, " <", $result2->items->[0]->link, ">";
|
||||||
|
}
|
||||||
|
|
||||||
|
my $result = $engine->search($arguments);
|
||||||
|
|
||||||
|
if (not defined $result or not defined $result->items or not @{$result->items}) {
|
||||||
|
return "$nick: No results found";
|
||||||
|
}
|
||||||
|
|
||||||
|
my $output = "$nick: (" . $result->formattedTotalResults . " results) ";
|
||||||
|
|
||||||
|
my $comma = "";
|
||||||
|
foreach my $item (@{$result->items}) {
|
||||||
|
$output .= $comma . decode_entities $item->title . ': <' . $item->link . ">";
|
||||||
|
$comma = " -- ";
|
||||||
|
last if --$matches <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
1;
|
@ -6,6 +6,7 @@ AntiRepeat
|
|||||||
AntiTwitter
|
AntiTwitter
|
||||||
AutoRejoin
|
AutoRejoin
|
||||||
Counter
|
Counter
|
||||||
|
GoogleSearch
|
||||||
Quotegrabs
|
Quotegrabs
|
||||||
RemindMe
|
RemindMe
|
||||||
UrlTitles
|
UrlTitles
|
||||||
|
@ -232,6 +232,18 @@
|
|||||||
"value" : "[!]"
|
"value" : "[!]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"googlesearch" : {
|
||||||
|
"api_key" : {
|
||||||
|
"private" : "1",
|
||||||
|
"type" : "text",
|
||||||
|
"value" : ""
|
||||||
|
},
|
||||||
|
"context" : {
|
||||||
|
"private" : "1",
|
||||||
|
"type" : "text",
|
||||||
|
"value" : ""
|
||||||
|
}
|
||||||
|
},
|
||||||
"interpreter" : {
|
"interpreter" : {
|
||||||
"max_recursion" : {
|
"max_recursion" : {
|
||||||
"type" : "text",
|
"type" : "text",
|
||||||
|
71
modules/g.pl
71
modules/g.pl
@ -1,71 +0,0 @@
|
|||||||
#!/usr/bin/perl
|
|
||||||
|
|
||||||
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
||||||
|
|
||||||
use warnings;
|
|
||||||
use strict;
|
|
||||||
|
|
||||||
use WWW::Google::CustomSearch;
|
|
||||||
use HTML::Entities;
|
|
||||||
|
|
||||||
my $api_key = ''; # https://developers.google.com/custom-search/v1/overview
|
|
||||||
my $cx = ''; # https://cse.google.com/all
|
|
||||||
|
|
||||||
my ($nick, $arguments, $matches);
|
|
||||||
|
|
||||||
$matches = 1;
|
|
||||||
$nick = shift @ARGV;
|
|
||||||
|
|
||||||
if ($#ARGV < 0) {
|
|
||||||
print "Usage: google [number of results] query\n";
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
$arguments = join ' ', @ARGV;
|
|
||||||
|
|
||||||
if ($arguments =~ s/^([0-9]+)//) {
|
|
||||||
$matches = $1;
|
|
||||||
}
|
|
||||||
|
|
||||||
my $engine = WWW::Google::CustomSearch->new(api_key => $api_key, cx => $cx, quotaUser => $nick);
|
|
||||||
|
|
||||||
print "$nick: ";
|
|
||||||
|
|
||||||
if ($arguments =~ m/(.*)\svs\s(.*)/i) {
|
|
||||||
my ($a, $b) = ($1, $2);
|
|
||||||
my $result1 = $engine->search("\"$a\" -\"$b\"");
|
|
||||||
my $result2 = $engine->search("\"$b\" -\"$a\"");
|
|
||||||
|
|
||||||
if (not defined $result1 or not defined $result1->items or not @{$result1->items}) {
|
|
||||||
print "No results for $a\n";
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (not defined $result2 or not defined $result2->items or not @{$result2->items}) {
|
|
||||||
print "No results for $b\n";
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
print "$a: (", $result1->formattedTotalResults, ") ", decode_entities $result1->items->[0]->title, " <", $result1->items->[0]->link, "> VS $b: (", $result2->formattedTotalResults, ") ", decode_entities $result2->items->[0]->title, " <", $result2->items->[0]->link, ">\n";
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
my $result = $engine->search($arguments);
|
|
||||||
|
|
||||||
if (not defined $result or not defined $result->items or not @{$result->items}) {
|
|
||||||
print "No results found\n";
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
print '(', $result->formattedTotalResults, " results)\n";
|
|
||||||
|
|
||||||
my $comma = "";
|
|
||||||
foreach my $item (@{$result->items}) {
|
|
||||||
print $comma, decode_entities $item->title, ': <', $item->link, ">\n";
|
|
||||||
$comma = " -- ";
|
|
||||||
last if --$matches <= 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
print "\n";
|
|
Loading…
Reference in New Issue
Block a user