53 lines
1.3 KiB
Perl
Executable File
53 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('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin')->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;
|
|
$result = $result =~ s/^ddg_spice_currency\(//r =~ s/\);$//r;
|
|
$eur_result = decode_json($eur_result);
|
|
$result = decode_json($result);
|
|
my $bitcoin_price = $result->[0]{current_price};
|
|
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/;
|
|
}
|
|
}
|
|
|