Ooops
September 24, 2021, 09:15:24

Author Topic: how to get datas from a webpage or using an API  (Read 445 times)

Offline RemiD

  • Hero Member
  • *****
  • Posts: 1282
how to get datas from a webpage or using an API
« on: May 03, 2021, 08:31:15 »
hi :)

i would like to get datas from a webpage ( coingecko.com )

i would like to get the top cryptos currencies names, their values (in USD), their marketcaps, their trading volumes.

i also see that there is an API :
coingecko.com/fr/api#explore-api

i have a good understanding on how to read / analyze / write binary files or lines and strings... but no experience on how to get such datas using 'GET'...

i see that there is a PHP code example, and i have some understanding of PHP, so i am going to study this.

but is there any way to get such datas using Blitz3d or Blitzmax ?

thanks,
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

Offline Derron

  • Hero Member
  • *****
  • Posts: 3651
Re: how to get datas from a webpage or using an API
« Reply #1 on: May 03, 2021, 09:42:10 »
libcurl should ease the pain (especially with https etc). For "curl" you then will surely find some examples on the net (so what parameters have to be set etc).

https://www.coingecko.com/fr/api#explore-api

-> go to "simple" -> "price"
-> click "try out"
-> fill in id "bitcoin"
-> fill in vs_currencies "usd"
-> scroll a bit down and click "execute"


To see what is the "id" of a coin: check the section "coin" - there is a list-sample
-> https://api.coingecko.com/api/v3/coins/list
-> btc has id "bitcoin"

To see what is a potential "vs_currencies" entry:
-> https://api.coingecko.com/api/v3/simple/supported_vs_currencies
-> dollar is "usd"


so to get the price as above
https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd
or for curl
curl -X GET "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd" -H "accept: application/json"

Response (json) is:
Code: [Select]
{
  "bitcoin": {
    "usd": 58935
  }
}

In BlitzMax NG there is text.mod/json.mod - and other stuff, easy to read then.


bye
Ron



Offline RemiD

  • Hero Member
  • *****
  • Posts: 1282
Re: how to get datas from a webpage or using an API
« Reply #2 on: May 03, 2021, 10:54:22 »
@Derron>>thanks for the infos.

i will have to re study / experiment with HTML, CSS, JS, PHP because i am a little rusty ::)

not urgent, on my todo list...
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

Offline Derron

  • Hero Member
  • *****
  • Posts: 3651
Re: how to get datas from a webpage or using an API
« Reply #3 on: May 03, 2021, 11:41:29 »
no need to do it with php - as stated it works "easily" in BlitzMax.


bye
Ron

Offline Kryzon

  • Sr. Member
  • ****
  • Posts: 276
Re: how to get datas from a webpage or using an API
« Reply #4 on: May 16, 2021, 06:17:03 »
In BlitzMax you'll have to use Brucey's libcurl module and some regex (regular expression) module to parse the response text -- unless the API gives you a simple enough data like Derron posted, that you can break it apart with only the built-in BlitzMax string functions (find, split, slice, join etc.).

You have that example I posted in the cryptocurrency thread, it uses libcurl and simple string manipulation to read the response: https://www.syntaxbomb.com/index.php/topic,3949.msg347049453.html#msg347049453

Brucey's modules are statically linked (as opposed to dynamically linked, at runtime), so it builds down to a single executable where there's no need to accompany it with any external DLLs.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal