From eec7c1691a5f98b15ed78766b519ffe628bff663 Mon Sep 17 00:00:00 2001 From: eleksir Date: Mon, 8 Mar 2021 17:26:58 +0300 Subject: [PATCH] Implement !xkcd command --- lib/botlib.pm | 4 ++++ lib/xkcd.pm | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 lib/xkcd.pm diff --git a/lib/botlib.pm b/lib/botlib.pm index 5017392..b21a632 100644 --- a/lib/botlib.pm +++ b/lib/botlib.pm @@ -19,6 +19,7 @@ use kitty qw (kitty); use lat qw (latAnswer); use util qw (trim utf2sha1); use weather qw (weather); +use xkcd qw (xkcd); use version; our $VERSION = qw (1.0); use Exporter qw(import); @@ -197,6 +198,7 @@ ${csign}some_brew - выдать соответствующий на ${csign}ver | ${csign}version - написать что-то про версию ПО ${csign}версия - написать что-то про версию ПО ${csign}w город | ${csign}п город - погода в городе +${csign}xkcd - комикс-стрип с xkcb.ru EOL } elsif (substr ($text, 1) eq 'lat' || substr ($text, 1) eq 'лат') { @@ -209,6 +211,8 @@ EOL $reply = dig ($chattername); } elsif (substr ($text, 1) eq 'fish' || substr ($text, 1) eq 'fisher' || substr ($text, 1) eq 'рыба' || substr ($text, 1) eq 'рыбка' || substr ($text, 1) eq 'рыбалка') { $reply = fish ($chattername); + } elsif (substr ($text, 1) eq 'xkcd') { + $reply = xkcd (); } elsif ((length ($text) >= 6 && (substr ($text, 1, 6) eq 'karma ' || substr ($text, 1, 6) eq 'карма ')) || substr ($text, 1) eq 'karma' || substr ($text, 1) eq 'карма') { my $mytext = ''; diff --git a/lib/xkcd.pm b/lib/xkcd.pm new file mode 100644 index 0000000..a9a1ad2 --- /dev/null +++ b/lib/xkcd.pm @@ -0,0 +1,27 @@ +package xkcd; + +use 5.018; +use strict; +use warnings; +use utf8; +use open qw (:std :utf8); +use HTTP::Tiny; + +use version; our $VERSION = qw (1.0); +use Exporter qw (import); +our @EXPORT_OK = qw (xkcd); + +sub xkcd { + my $http = HTTP::Tiny->new (timeout => 5, max_redirect => 0); + my $r = $http->get ('https://xkcd.ru/random/'); + + if (defined $r->{headers} && defined $r->{headers}->{location} && $r->{headers}->{location} ne '') { + return sprintf 'https://xkcd.ru/i/%s_v1.png', substr ($r->{headers}->{location}, 1, -1); + } + + return 'Комикс-стрип нарисовать не так-то просто :('; +} + +1; + +# vim: set ft=perl noet ai ts=4 sw=4 sts=4: