App-CryptoCurrencyUtils

 view release on metacpan or  search on metacpan

lib/App/CryptoCurrencyUtils.pm  view on Meta::CPAN

    require CryptoCurrency::Catalog;

    my %args = @_;

    if ($args{codes}) {
        [200, "OK", [map {$_->{code}} CryptoCurrency::Catalog->new->all_data]];
    } elsif ($args{safenames}) {
        [200, "OK", [map {$_->{safename}} CryptoCurrency::Catalog->new->all_data]];
    } elsif ($args{names}) {
        [200, "OK", [map {$_->{name}} CryptoCurrency::Catalog->new->all_data]];
    } else {
        [200, "OK", [CryptoCurrency::Catalog->new->all_data]];
    }
}


$SPEC{list_exchanges} = {
    v => 1.1,
    summary => "List cryptocurrency exchanges",
    description => <<'_',

This utility lists cryptocurrency exchanges from <pm:CryptoExchange::Catalog>,
which in turn gets its list from <https://coinmarketcap.com/>.

_
    args => {
    },
};
sub list_exchanges {
    require CryptoExchange::Catalog;

    [200, "OK", [CryptoExchange::Catalog->new->all_data]];
}

$SPEC{list_cmc_coins} = {
    v => 1.1,
    summary => "List of all coins listed on coinmarketcap.com (CMC) ".
        "along with their marketcaps, ranks, etc",
    description => <<'_',

This utility basically parses <https://coinmarketcap.com/all/views/all/> into
table data.

_
    args => {
    },
};
sub list_cmc_coins {
    require HTTP::Tiny;

    my $res = HTTP::Tiny->new->get("https://coinmarketcap.com/all/views/all/");
    return [$res->{status}, $res->{reason}] unless $res->{success};

    my @coins;

    # we capture the records first to speed up otherwise-glacial matching
    my @trs;
    while ($res->{content} =~ m!(<tr \s id="id-[\w-]+".+?</tr>)!gsx) {
        push @trs, $1;
    }
    #say "D:found ", scalar(@trs), " coins";

    my $i = 0;
    for my $tr (@trs) {
        $i++;
        $tr =~
            m!<tr \s id="id-(?<safename>[\w-]+)"[^>]*>.+?
              <td \s class="text-center">\s*(?<rank>\d+)\s*</td>.+?
              <td \s class="[^"]*?col-symbol">(?<symbol>[^<]+)<.+?
              <td \s class="[^"]*?market-cap[^"]*" \s data-usd="(?<mktcap_usd>[^"]+)" \s data-btc="(?<mktcap_btc>[^"]+)".+?
              <a \s href="[^"]+" \s class="price" \s data-usd="(?<price_usd>[^"]+)" \s data-btc="(?<price_btc>[^"]+)".+?
              \s data-supply="(?<supply>[^"]+)".+?
              <a \s href="[^"]+" \s class="volume" \s data-usd="(?<volume_usd>[^"]+)" \s data-btc="(?<volume_btc>[^"]+)".+?
             !sx
                 or die "Can't parse row #$i";
        push @coins, {%+};
    }

    my $resmeta = {
        'table.fields'       => [qw/rank safename symbol mktcap_usd mktcap_btc price_usd price_btc supply volume_usd volume_btc/],
        #'table.field_aligns' => [qw/left left     left   right      right     right     right     right  right      right/], # ugh, makes rendering so slow
    };
    [200, "OK", \@coins, $resmeta];
}

1;
# ABSTRACT: CLI utilities related to cryptocurrencies

__END__

=pod

=encoding UTF-8

=head1 NAME

App::CryptoCurrencyUtils - CLI utilities related to cryptocurrencies

=head1 VERSION

This document describes version 0.012 of App::CryptoCurrencyUtils (from Perl distribution App-CryptoCurrencyUtils), released on 2018-04-06.

=head1 DESCRIPTION

This distribution contains the following CLI utilities:

=over

=item * L<coin-cmc-summary>

=item * L<global-cmc-summary>

=item * L<grep-coin>

=item * L<grep-exchange>

=item * L<list-cmc-coins>

=item * L<list-coins>

=item * L<list-exchanges>



( run in 2.301 seconds using v1.01-cache-2.11-cpan-d7a12ab2c7f )