mirror of
https://github.com/pragma-/pbot.git
synced 2025-04-26 09:47:53 +02:00
Add URL support to factadd (#51)
* FactoidCommands.pm: remove trailing whitespace * Add URL support to factadd
This commit is contained in:
parent
1663ae065d
commit
3fb72f73c5
@ -18,6 +18,7 @@ use Time::HiRes qw(gettimeofday);
|
|||||||
use Getopt::Long qw(GetOptionsFromString);
|
use Getopt::Long qw(GetOptionsFromString);
|
||||||
use POSIX qw(strftime);
|
use POSIX qw(strftime);
|
||||||
use Storable;
|
use Storable;
|
||||||
|
use LWP::UserAgent ();
|
||||||
|
|
||||||
use PBot::Utils::SafeFilename;
|
use PBot::Utils::SafeFilename;
|
||||||
|
|
||||||
@ -957,13 +958,35 @@ sub factadd {
|
|||||||
# now this is the keyword
|
# now this is the keyword
|
||||||
$keyword = $self->{pbot}->{interpreter}->shift_arg(\@arglist);
|
$keyword = $self->{pbot}->{interpreter}->shift_arg(\@arglist);
|
||||||
|
|
||||||
# check for optional "is" and discard
|
# check for -url
|
||||||
if (lc $arglist[0] eq 'is') {
|
if ($arglist[0] eq '-url') {
|
||||||
|
# discard it
|
||||||
$self->{pbot}->{interpreter}->shift_arg(\@arglist);
|
$self->{pbot}->{interpreter}->shift_arg(\@arglist);
|
||||||
}
|
|
||||||
|
|
||||||
# and the text is the remaining arguments with quotes preserved
|
# the URL is the remaining arguments
|
||||||
($text) = $self->{pbot}->{interpreter}->split_args(\@arglist, 1);
|
($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) {
|
if (not defined $from_chan or not defined $text or not defined $keyword) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user