zeroclickinfo-goodies/lib/DDG/Goodie/DuckDuckGo.pm

79 lines
2.7 KiB
Perl
Raw Normal View History

package DDG::Goodie::DuckDuckGo;
# ABSTRACT: Return hard-coded descriptions for DuckDuckGo terms
use strict;
use DDG::Goodie;
2015-05-29 15:18:03 -07:00
use YAML::XS 'LoadFile';
primary_example_queries 'duckduckgo help';
secondary_example_queries 'ddg tor', 'short URL for duck duck go';
2014-03-17 14:01:17 -07:00
description 'DuckDuckGo help and quick links';
name 'DuckDuckGo';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/DuckDuckGo.pm';
category 'cheat_sheets';
topics 'everyday';
2015-01-07 10:37:42 -08:00
attribution twitter => ['crazedpsyc','crazedpsyc'],
cpan => ['CRZEDPSYC','crazedpsyc'];
2014-03-17 14:01:17 -07:00
my @ddg_aliases = map { ($_, $_ . "'s", $_ . "s") } ('duck duck go', 'duckduck go', 'duck duckgo', 'duckduckgo', 'ddg');
2015-01-08 19:19:26 -08:00
triggers any => @ddg_aliases, "zeroclickinfo", "private search";
zci is_cached => 1;
2015-05-29 15:18:03 -07:00
my $responses = LoadFile(share('responses.yml'));
# The YAML is intended to be human-friendly.
# Now we make something computer-friendly.
foreach my $keyword (keys %$responses) {
my $response = $responses->{$keyword};
if (my $base_format = $response->{base_format}) {
# We need to produce the output for each version.
if (my $info_url = $response->{info_url}) {
$response->{text} = $base_format;
$response->{text} =~ s/[\[\]]//g; # No internal linking.
$response->{text} .= ': ' . $response->{info_url}; # Stick the link on the end.
$response->{html} = $base_format;
$response->{html} =~ s#\[#<a href='$info_url'>#; # Insert link.
$response->{html} =~ s#\]#</a>#;
$response->{html} .= '.';
} else {
# No link to insert, so it must be ready for both.
$response->{text} = $response->{html} = $response->{base_format};
}
}
if (ref($response->{aliases}) eq 'ARRAY') {
foreach my $alias (@{$response->{aliases}}) {
# Assume we didn't add an alias for an existing keyword.
$responses->{$alias} = $response;
}
}
foreach my $key (keys %$response) {
# No matter what they added, we only use the following keys for the actual response.
delete $response->{$key} unless (grep { $key eq $_ } (qw(text html)));
}
}
my $skip_words_re = qr/\b(?:what|where|is|of|for|the|in|on)\b/;
handle remainder => sub {
2014-05-01 14:30:26 -07:00
my $key = lc;
2014-04-04 02:39:55 -07:00
$key =~ s/\?//g; # Allow for questions, but don't pollute skip words.
$key =~ s/$skip_words_re//g;
$key =~ s/\W+//g;
my $response = $responses->{$key};
2014-04-04 02:39:55 -07:00
return unless ($response);
2014-10-06 04:37:53 -07:00
return $response->{text},
structured_answer => {
input => [],
operation => 'DuckDuckGo info',
result => $response->{html}};
};
1;