Skip to content

Commit

Permalink
save get_price script
Browse files Browse the repository at this point in the history
  • Loading branch information
tksarah committed Mar 9, 2024
1 parent 7245255 commit a9919c1
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions get_price.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use LWP::Simple;
use Data::Dumper;

my $url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest';
my $api_key = 'API_KEY';
my $headers = '-H "X-CMC_PRO_API_KEY: '.$api_key.'" -H "Accept: application/json"';

# for USD
my $params_usd = '-d "symbol=ETH,ASTR&convert=USD"';
my $curl_command_usd = "curl -s $headers $params_usd -G \"$url\"";

my $json_usd = `$curl_command_usd`;
die "Could not get $url" unless defined $json_usd;

my $decoded_json_usd = decode_json($json_usd);

# TEST
#print Dumper($decoded_json);

my $eth_price_usd = $decoded_json_usd->{'data'}{'ETH'}{'quote'}{'USD'}{'price'};
my $astr_price_usd = $decoded_json_usd->{'data'}{'ASTR'}{'quote'}{'USD'}{'price'};

# for JPY
my $params_jpy = '-d "symbol=ETH,ASTR&convert=JPY"';
my $curl_command_jpy = "curl -s $headers $params_jpy -G \"$url\"";

my $json_jpy = `$curl_command_jpy`;
die "Could not get $url" unless defined $json_jpy;

my $decoded_json_jpy = decode_json($json_jpy);

my $eth_price_jpy = $decoded_json_jpy->{'data'}{'ETH'}{'quote'}{'JPY'}{'price'};
my $astr_price_jpy = $decoded_json_jpy->{'data'}{'ASTR'}{'quote'}{'JPY'}{'price'};

# Output
print "\n--- Price ---\n";
print "\$/Yen:\n";
printf(" %.2f\n\n", $eth_price_jpy/$eth_price_usd);
print "USD:\n";
printf(" ETH : %.2f\n", $eth_price_usd);
printf(" ASTR : %.4f\n", $astr_price_usd);
print "\n";
print "JPY:\n";
printf(" ETH : %.2f\n", $eth_price_jpy);
printf(" ASTR : %.2f\n", $astr_price_jpy);
print "-------\n\n";

0 comments on commit a9919c1

Please sign in to comment.