PageRenderTime 197ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/duckduckgo.pl

https://github.com/prsquee/irssi-scripts
Perl | 38 lines | 28 code | 7 blank | 3 comment | 4 complexity | bae3a91b77355f54c0de04c5838aa195 MD5 | raw file
  1. #isohunt
  2. use Irssi qw(settings_get_str signal_emit signal_add print);
  3. #use warnings;
  4. use strict;
  5. use Data::Dumper;
  6. use LWP::UserAgent;
  7. use JSON;
  8. use URI::Encode qw(uri_encode uri_decode);
  9. signal_add("cuac cuac go","cuac_cuac");
  10. my $json = JSON->new();
  11. my $ua = LWP::UserAgent->new(timeout => '15');
  12. sub cuac_cuac {
  13. my ($server, $chan, $searchme) = @_;
  14. my $query = 'https://api.duckduckgo.com/?q='
  15. . uri_encode($searchme)
  16. . '&format=json';
  17. $ua->agent(settings_get_str('myUserAgent'));
  18. my $req = $ua->get($query);
  19. unless ($req->is_success) {
  20. print (CRAP "duckduckgo error code: $req->code - $req->message");
  21. return;
  22. }
  23. my $parsed_json = eval { $json->allow_nonref->utf8->decode($req->decoded) };
  24. return if $@;
  25. print (CRAP Dumper($parsed_json));
  26. sayit($server, $chan, '[Answer] ' . $parsed_json->{'Answer'}) if ($parsed_json->{'Answer'});
  27. sayit($server, $chan, '[def] ' . $parsed_json->{'Definition'}) if ($parsed_json->{'Definition'});
  28. sayit($server, $chan, '[Abstract]' . $parsed_json->{'Abstract'}) if ($parsed_json->{'Abstract'});
  29. #return;
  30. }
  31. sub sayit { my $s = shift; $s->command("MSG @_"); }