Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Cryptocurrency: Fix Fiat Overtriggering (#3417)
Browse files Browse the repository at this point in the history
* Fix odd overtriggering instance.

* Rename cryptolist file.
  • Loading branch information
pjhampton authored and moollaza committed Aug 31, 2017
1 parent fdc253b commit d8c2a69
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
46 changes: 24 additions & 22 deletions lib/DDG/Spice/Cryptocurrency.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand Down Expand Up @@ -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) = @_;
Expand All @@ -139,12 +123,12 @@ sub checkCurrencyCode {
my $normalized_number = $styler->for_computation($amount);

# Handles queries of the form '1 <cryptocurrency>'
# 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)) {
Expand Down Expand Up @@ -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 = '';

Expand Down Expand Up @@ -231,14 +231,16 @@ 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)) {
return;
}
# 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.
Expand Down
7 changes: 7 additions & 0 deletions share/spice/cryptocurrency/FiatCurrencyList.txt
Original file line number Diff line number Diff line change
@@ -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,
7 changes: 0 additions & 7 deletions share/spice/cryptocurrency/cryptocurrencylist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
2 changes: 2 additions & 0 deletions t/Cryptocurrency.t
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d8c2a69

Please sign in to comment.