*alexandre* has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've a found a usefull way to ask the bitcoin from the commmand line
echo "1 BTC = $(curl -s https://api.coindesk.com/v1/bpi/currentprice/u +sd.json | grep -o 'rate":"[^"]*' | cut -d\" -f3) USD" 1 BTC
and I now want to integrate to my perl script
#!/usr/bin/perl -w use strict; use warnings; use CGI; my $query = CGI->new ; loadPage (); sub loadPage { my $page = ""; $page = $query->param("page"); if ($page eq 'search') { print "Content-Type: text/html\n\n"; print "1 BTC = $(curl -s https://api.coindesk.com/v1/bpi/curre +ntprice/usd.json | grep -o 'rate\":\"[^\"]*' | cut -d\" -f3) USD"; }else { print "Content-Type: text/html\n\n"; print "error"; } }
this code print to my browser
1 BTC = 33 33curl -s https://api.coindesk.com/v1/bpi/currentprice/usd. +json | grep -o 'rate":"[^"]*' | cut -d" -f3) USD
how guys to integrate properly the command line into a print command instead Thx in advance

2019-01-08 Athanasius fixed one code tag

Replies are listed 'Best First'.
Re: bitcoin price from command line
by Corion (Patriarch) on Jan 08, 2019 at 09:21 UTC

    Using my module HTTP::Request::FromCurl or it's associated online website, curl2lwp, you can conveniently convert any curl command to Perl.

    #!perl use strict; use warnings; use WWW::Mechanize; use HTTP::Request; my $ua = WWW::Mechanize->new(); my $r = HTTP::Request->new( 'GET' => 'https://api.coindesk.com/v1/bpi/currentprice/usd.json', [ 'Accept' => '*/*', 'Host' => 'api.coindesk.com:443', 'User-Agent' => 'curl/7.55.1', ], ); my $res = $ua->request( $r, ); __END__ Created from curl command line curl -s https://api.coindesk.com/v1/bpi/currentprice/usd.json

    Reading and extracting the rate from the JSON file is easiest done using the JSON module (or JSON::PP or JSON::XS or ...), which convert the JSON to a nice data structure which you can then traverse from Perl.

      Hi I follow your code and I'm getting the following error while executing my script.
      [Tue Jan 08 12:34:21 2019] [error] [client 127.0.0.1] Error GETing htt +ps://api.coindesk.com/v1/bpi/currentprice/usd.json: Can't connect to +api.coindesk.com:443 at /home/alexandre/apache/cgi-bin/helloWorld.cgi + line 20.

        Maybe you need to configure LWP::UserAgent with the HTTP proxy settings for your environment?

        $ua->env_proxy();
Re: bitcoin price from command line
by marto (Cardinal) on Jan 08, 2019 at 11:23 UTC

    Here's a basic example using Mojo::UserAgent:

    use feature 'say'; use Mojo::UserAgent; my $ua = Mojo::UserAgent->new; my $coinbaseURL = 'https://api.coindesk.com/v1/bpi/currentprice/usd.js +on'; my $value = $ua->get( $coinbaseURL )->result->json->{bpi}{USD}{rate}; say "Bitcoin Price (USD): $value";

    If I were doing such a thing I'd likely just use Mojolicious::Lite for the site/page, and create a little route based on the example above (throw in some error checking etc) to return this as json, and call that route on the page every few minutes via JavaScript, so that the Bitcoin price updated without having to refresh/reload the page, from expedience tends to be how users expect things to work these days. See also Mojolicious -> Tutorial and UserAgent.

      from expedience tends to be how users expect things to work these days.

      While I don't disagree, I would like to add a small request that anyone writing such pages also includes a simple Off Switch in order to cater for the expectations of the rest of us. Reading a page only to have it unexpectedly change on you is only slightly less annoying than those move-elements-around-while-the-page-builds renderers which invariably result in clicking on the wrong thing that's just magically moved under the pointer.

      I'll just crawl back to my web 1.0 now.

        Yes, much of the interwebs for me is just broken :)

      Hi first thanks for your time, I used the following code
      #!/usr/bin/perl -w use feature 'say'; use Mojo::UserAgent; my $ua = Mojo::UserAgent->new; my $coinbaseURL = 'https://api.coindesk.com/v1/bpi/currentprice/usd.js +on'; my $value = $ua->get( $coinbaseURL )->result->json->{bpi}{USD}{rate}; #say "Bitcoin Price (USD): $value"; print "Content-Type: text/html\n\n"; print "Bitcoin Price (USD): $value";
      But I got the following error :
      [error] [client 127.0.0.1] IO::Socket::SSL 2.009+ required for TLS sup +port at /home/alexandre/apache/cgi-bin/helloWorld.cgi line 10.
      EDIT : After upgrading IO::Socket::SSL it's work fine thanks you guys

        Mojo::UserAgent uses various modules for TLS and so on, if they're available (see Description). Either (ideally) install IO::Socket::SSL, or alternatively set the appropriate environment variable to disable the functionality, the first suggestion is better.