54 lines
1.3 KiB
Perl
Executable File
54 lines
1.3 KiB
Perl
Executable File
#!/usr/bin/env perl
|
|
|
|
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" if !$run_once;
|
|
eval {
|
|
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))
|
|
->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;
|
|
};
|
|
if ( !$run_once ) {
|
|
alarm 60;
|
|
my $line = <STDIN>;
|
|
alarm 0;
|
|
}
|
|
};
|
|
if ($run_once) {
|
|
exit(0);
|
|
}
|
|
if ($@) {
|
|
STDERR->say(qq($@)) unless $@ =~ /$ALARM_NO_ERROR/;
|
|
}
|
|
}
|
|
|