Adding euro conversion.

This commit is contained in:
sergiotarxz 2022-01-28 00:29:07 +01:00
parent d02af73836
commit 553245a703
1 changed files with 25 additions and 7 deletions

View File

@ -5,29 +5,47 @@ use v5.30.0;
use strict;
use warnings;
use Data::Dumper;
use Mojo::UserAgent;
use Mojo::DOM;
use Const::Fast;
use JSON;
const my $ALARM_NO_ERROR => q(Alarm defunction.);
my $ua = Mojo::UserAgent->new;
my $run_once = defined $ARGV[0];
while (1) {
say "Searching data";
say "Searching data" if !$run_once;
eval {
my $result = $ua->get(q(https://www.coingecko.com/en))->result->body;
my $result = $ua->get(q(https://www.coingecko.com/))->result->body;
my $eur_result =
$ua->get(q(https://duckduckgo.com/js/spice/currency/1/USD/EUR))
->result->body;
$eur_result = $eur_result =~ s/^ddg_spice_currency\(//r =~ s/\);$//r;
$eur_result = decode_json($eur_result);
my $dom = Mojo::DOM->new($result);
my $bitcoin = $dom->at(q(.coin-table))->find(q(tr))->[1];
say $bitcoin->all_text =~ s/(?:\s|\r|\n)+/ /gr;
my $bitcoin = $dom->at(q(.coin-table))
->at(q(span[data-coin-symbol="btc"][data-target="price.price"]));
my $bitcoin_price = $bitcoin->all_text =~ s/,//gr =~ s/\$//r;
printf "%.02f€ \$%0.2f\n", $bitcoin_price * 0.00257 *
$eur_result->{conversion}->{q/converted-amount/}, $bitcoin_price;
local $SIG{ALRM} = sub {
die $ALARM_NO_ERROR;
};
alarm 60;
my $line = <STDIN>;
alarm 0;
if ( !$run_once ) {
alarm 60;
my $line = <STDIN>;
alarm 0;
}
};
if ($run_once) {
exit(0);
}
if ($@) {
STDERR->say(qq($@)) unless $@ =~ /$ALARM_NO_ERROR/;
}