From 553245a7030e9ea0a2b0494bc7f725a39dc7cd76 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Fri, 28 Jan 2022 00:29:07 +0100 Subject: [PATCH] Adding euro conversion. --- bin/bitcoin_monitor.pl | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/bin/bitcoin_monitor.pl b/bin/bitcoin_monitor.pl index ec37c92..e3eb927 100755 --- a/bin/bitcoin_monitor.pl +++ b/bin/bitcoin_monitor.pl @@ -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 = ; - alarm 0; + if ( !$run_once ) { + alarm 60; + my $line = ; + alarm 0; + } }; + if ($run_once) { + exit(0); + } if ($@) { STDERR->say(qq($@)) unless $@ =~ /$ALARM_NO_ERROR/; }