From 3fb72f73c587641d668edab26c8a42efe0ce7be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Mon, 1 Jul 2019 02:45:05 +0200 Subject: [PATCH] Add URL support to factadd (#51) * FactoidCommands.pm: remove trailing whitespace * Add URL support to factadd --- PBot/FactoidCommands.pm | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/PBot/FactoidCommands.pm b/PBot/FactoidCommands.pm index d225fb15..3de27cfc 100644 --- a/PBot/FactoidCommands.pm +++ b/PBot/FactoidCommands.pm @@ -18,6 +18,7 @@ use Time::HiRes qw(gettimeofday); use Getopt::Long qw(GetOptionsFromString); use POSIX qw(strftime); use Storable; +use LWP::UserAgent (); use PBot::Utils::SafeFilename; @@ -957,13 +958,35 @@ sub factadd { # now this is the keyword $keyword = $self->{pbot}->{interpreter}->shift_arg(\@arglist); - # check for optional "is" and discard - if (lc $arglist[0] eq 'is') { + # check for -url + if ($arglist[0] eq '-url') { + # discard it $self->{pbot}->{interpreter}->shift_arg(\@arglist); - } - # and the text is the remaining arguments with quotes preserved - ($text) = $self->{pbot}->{interpreter}->split_args(\@arglist, 1); + # the URL is the remaining arguments + ($url) = $self->{pbot}->{interpreter}->split_args(\@arglist, 1); + + # create a UserAgent + my $ua = LWP::UserAgent->new(timeout => 10); + + # get the factoid's text from the URL + my $response = $ua->get($url); + + # process the response + if ($response->is_success) { + $text = $response->decoded_content; + } else { + return "Failed to get URL: " . $response->status_line; + } + } else { + # check for optional "is" and discard + if (lc $arglist[0] eq 'is') { + $self->{pbot}->{interpreter}->shift_arg(\@arglist); + } + + # and the text is the remaining arguments with quotes preserved + ($text) = $self->{pbot}->{interpreter}->split_args(\@arglist, 1); + } } if (not defined $from_chan or not defined $text or not defined $keyword) {