diff --git a/lib/DDG/Spice/Cryptocurrency.pm b/lib/DDG/Spice/Cryptocurrency.pm index 706e7ca4c0..062265bde4 100644 --- a/lib/DDG/Spice/Cryptocurrency.pm +++ b/lib/DDG/Spice/Cryptocurrency.pm @@ -10,7 +10,9 @@ use YAML::XS qw(LoadFile); # Get all the valid currencies from a text file. my @currTriggers; my @cryptoTriggers; -my @currencies = share('cryptocurrencylist.txt')->slurp; +my @crypto_currencies = share('cryptocurrencylist.txt')->slurp; +my @fiat_currencies = share('FiatCurrencyList.txt')->slurp; +my @currencies = (@crypto_currencies, @fiat_currencies); my %currHash = (); my $currDisplayName = ''; @@ -105,24 +107,6 @@ spice alt_to => { } }; -# This function converts things like "us dollars" to the standard "usd". -sub getCode { - my $input = shift; - foreach my $key (keys %currHash) { - if(exists $currHash{$key}) { - my @currValues = @{$currHash{$key}}; - foreach my $value (@currValues) { - if($input eq $value) { - # Set the display name of the currency - $currDisplayName = $currValues[1]; - return $key; - } - } - } - } -} - - # This function is responsible for processing the input. sub checkCurrencyCode { my($amount, $from, $to, $generic) = @_; @@ -139,12 +123,12 @@ sub checkCurrencyCode { my $normalized_number = $styler->for_computation($amount); # Handles queries of the form '1 ' - # Avoids triggering on common queries like '1 gig' or '1 electron' # If the cryptocurrency is not in the top currencies list, the query does not include a 'to' currency, # and the query doesn't include 'coin' then don't trigger if ($normalized_number == 1 && $to eq '' && exists($availableLocalCurrencies{getCode($from)}) ) { return; } + # There are cases where people type in "2016 bitcoin", so we don't want to trigger on those queries. # The first cryptocoins appeared in 2008, so dates before that could be valid amounts. if($normalized_number >= 2008 && $normalized_number < 2100 && (length($from) == 0 || length($to) == 0)) { @@ -200,7 +184,23 @@ sub checkCurrencyCode { return $endpoint, $query, $query2; } -# get the local currency where the user is +# Reduces triggers to their respective symbol. "us dollars" --> "usd". +sub getCode { + my $input = shift; + + foreach my $key (keys %currHash) { + if(exists $currHash{$key}) { + my @currValues = @{$currHash{$key}}; + foreach my $value (@currValues) { + if($input eq $value) { + return $key; + } + } + } + } +} + +# retrieve the users local currency (if possible ~ default to USD) sub getLocalCurrency { my $local_currency = ''; @@ -231,6 +231,8 @@ handle query_lc => sub { # ie. 'ltc', 'feather coin calculator', 'eth to usd' if (/$guard/) { my ($amount, $from, $alt_amount, $to) = ($1, $2, $3, $4 || ''); + my $from_code = getCode($from); + my $to_code = getCode($to); # Exit early if two amounts are given if(length($amount) && length($alt_amount)) { @@ -238,7 +240,7 @@ handle query_lc => sub { } # ignore queries that don't involve a cryptocurrency # these are handled by the Currency Spice - elsif (defined $availableLocalCurrencies{$from} && defined $availableLocalCurrencies{$to}) { + elsif (defined $availableLocalCurrencies{$from_code} && defined $availableLocalCurrencies{$to_code}) { return; } # Case where the first amount is available. diff --git a/share/spice/cryptocurrency/FiatCurrencyList.txt b/share/spice/cryptocurrency/FiatCurrencyList.txt new file mode 100644 index 0000000000..933fc9f79b --- /dev/null +++ b/share/spice/cryptocurrency/FiatCurrencyList.txt @@ -0,0 +1,7 @@ +usd,us dollar,united states dollar,american dollar,america, us, +cad,canadian dollar,canada dollar, +gbp,british pound,united kingdom pound,uk pound,uk pounds,pound,sterling,pound sterling, +eur,euro,euros, +rub,rur,russia ruble,russia rouble,russian ruble,russian rouble,ruble,rouble, +uah,ukrainian hryvna,ukraine hryvna, +jpy,japanese yen,japan yen,yen, diff --git a/share/spice/cryptocurrency/cryptocurrencylist.txt b/share/spice/cryptocurrency/cryptocurrencylist.txt index fc24303f82..b2d780cc86 100644 --- a/share/spice/cryptocurrency/cryptocurrencylist.txt +++ b/share/spice/cryptocurrency/cryptocurrencylist.txt @@ -13,10 +13,3 @@ ppc,peercoin,peer coin, xpm,primecoin,prime coin, rdd,reddcoin,redd coin, zec,zcash,z cash, -usd,us dollar,united states dollar,american dollar,america, us, -cad,canadian dollar,canada dollar, -gbp,british pound,united kingdom pound,uk pound,uk pounds,pound,sterling,pound sterling, -eur,euro,euros, -rub,rur,russia ruble,russia rouble,russian ruble,russian rouble,ruble,rouble, -uah,ukrainian hryvna,ukraine hryvna, -jpy,japanese yen,japan yen,yen, diff --git a/t/Cryptocurrency.t b/t/Cryptocurrency.t index ff17d6c2e0..21bd39c732 100644 --- a/t/Cryptocurrency.t +++ b/t/Cryptocurrency.t @@ -444,6 +444,8 @@ ddg_spice_test( 'cad to php' => undef, 'eur to eur' => undef, 'cad to usd' => undef, + 'what is 79.95 cad in us dollars' => undef, + 'canadian dollars to us dollars' => undef, # crypto edge cases 'ftc' => undef,