3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-25 20:22:37 +01:00
pbot/Plugins/GoogleSearch.pm

95 lines
3.3 KiB
Perl
Raw Normal View History

#!/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;
2020-02-15 23:38:32 +01:00
2020-02-09 04:48:05 +01:00
use parent 'Plugins::Plugin';
2020-02-09 04:48:05 +01:00
use warnings; use strict;
use feature 'unicode_strings';
use WWW::Google::CustomSearch;
use HTML::Entities;
sub initialize {
2020-02-15 23:38:32 +01:00
my ($self, %conf) = @_;
$self->{pbot}->{registry}->add_default('text', 'googlesearch', 'api_key', '');
$self->{pbot}->{registry}->add_default('text', 'googlesearch', 'context', '');
2020-02-15 23:38:32 +01:00
$self->{pbot}->{registry}->set_default('googlesearch', 'api_key', 'private', 1);
$self->{pbot}->{registry}->set_default('googlesearch', 'context', 'private', 1);
2020-02-15 23:38:32 +01:00
$self->{pbot}->{commands}->register(sub { $self->googlesearch(@_) }, 'google', 0);
}
sub unload {
2020-02-15 23:38:32 +01:00
my $self = shift;
$self->{pbot}->{commands}->unregister('google');
}
sub googlesearch {
2020-02-15 23:38:32 +01:00
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
return "Usage: google [number of results] query\n" if not length $arguments;
2020-02-15 23:38:32 +01:00
my $matches = 1;
$matches = $1 if $arguments =~ s/^-n\s+([0-9]+)\s*//;
2020-02-15 23:38:32 +01:00
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
2020-02-15 23:38:32 +01:00
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.";
}
2020-02-15 23:38:32 +01:00
if (not length $cx) { return "$nick: Registry item googlesearch.context is not set. See https://cse.google.com/all to set up a context."; }
2020-02-15 23:38:32 +01:00
my $engine = WWW::Google::CustomSearch->new(api_key => $api_key, cx => $cx, quotaUser => "$nick!$user\@$host");
2020-02-15 23:38:32 +01:00
if ($arguments =~ m/(.*)\svs\s(.*)/i) {
my ($a, $b) = ($1, $2);
my $result1 = $engine->search("\"$a\" -\"$b\"");
my $result2 = $engine->search("\"$b\" -\"$a\"");
2020-02-15 23:38:32 +01:00
if (not defined $result1 or not defined $result1->items or not @{$result1->items}) { return "$nick: No results for $a"; }
2020-02-15 23:38:32 +01:00
if (not defined $result2 or not defined $result2->items or not @{$result2->items}) { return "$nick: No results for $b"; }
2020-02-15 23:38:32 +01:00
my $title1 = $result1->items->[0]->title;
my $title2 = $result2->items->[0]->title;
2020-02-15 23:38:32 +01:00
utf8::decode $title1;
utf8::decode $title2;
2020-02-15 23:38:32 +01:00
return
"$nick: $a: ("
. $result1->formattedTotalResults . ") "
. decode_entities($title1) . " <"
. $result1->items->[0]->link
. "> VS $b: ("
. $result2->formattedTotalResults . ") "
. decode_entities($title2) . " <"
. $result2->items->[0]->link . ">";
}
2020-02-15 23:38:32 +01:00
my $result = $engine->search($arguments);
2020-02-15 23:38:32 +01:00
if (not defined $result or not defined $result->items or not @{$result->items}) { return "$nick: No results found"; }
2020-02-15 23:38:32 +01:00
my $output = "$nick: (" . $result->formattedTotalResults . " results) ";
2020-02-15 23:38:32 +01:00
my $comma = "";
foreach my $item (@{$result->items}) {
my $title = $item->title;
utf8::decode $title;
$output .= $comma . decode_entities($title) . ': <' . $item->link . ">";
$comma = " -- ";
last if --$matches <= 0;
}
return $output;
}
1;