PageRenderTime 40ms CodeModel.GetById 34ms app.highlight 4ms RepoModel.GetById 1ms 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
 2use Irssi qw(settings_get_str signal_emit signal_add print);
 3#use warnings;
 4use strict;
 5use Data::Dumper;
 6use LWP::UserAgent;
 7use JSON;
 8use URI::Encode qw(uri_encode uri_decode);
 9
10signal_add("cuac cuac go","cuac_cuac");
11
12my $json = JSON->new();
13my $ua = LWP::UserAgent->new(timeout => '15');
14
15sub cuac_cuac {
16  my ($server, $chan, $searchme) = @_;
17  my $query = 'https://api.duckduckgo.com/?q='
18            . uri_encode($searchme)
19            . '&format=json';
20  $ua->agent(settings_get_str('myUserAgent'));
21
22  my $req = $ua->get($query);
23
24  unless ($req->is_success) {
25    print (CRAP "duckduckgo error code: $req->code - $req->message");
26    return;
27  }
28
29  my $parsed_json = eval { $json->allow_nonref->utf8->decode($req->decoded) };
30  return if $@;
31  print (CRAP Dumper($parsed_json));
32  sayit($server, $chan, '[Answer] '  . $parsed_json->{'Answer'})      if ($parsed_json->{'Answer'});
33  sayit($server, $chan, '[def] '     . $parsed_json->{'Definition'})  if ($parsed_json->{'Definition'});
34  sayit($server, $chan, '[Abstract]' . $parsed_json->{'Abstract'})    if ($parsed_json->{'Abstract'});
35  #return;
36}
37
38sub sayit { my $s = shift; $s->command("MSG @_"); }