Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Merge branch 'reteps-master'
Browse files Browse the repository at this point in the history
* reteps-master:
  coin markets
  Update README.md
  save
  • Loading branch information
miguelmota committed Jan 18, 2018
2 parents 981920a + 4d1cb0f commit 934d608
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 139 deletions.
145 changes: 11 additions & 134 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,146 +1,23 @@
# go-coinmarketcap

# coinmarketcap
--
import "github.com/miguelmota/go-coinmarketcap"
> [CoinMarketCap](https://coinmarketcap.com/) client for Golang.
Coin Market Cap API in golang
[![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/miguelmota/go-coinmarketcap/master/LICENSE.md) [![Go Report Card](https://goreportcard.com/badge/github.com/miguelmota/go-coinmarketcap?)](https://goreportcard.com/report/github.com/miguelmota/go-coinmarketcap) [![GoDoc](https://godoc.org/github.com/miguelmota/go-coinmarketcap?status.svg)](https://godoc.org/github.com/miguelmota/go-coinmarketcap)

## Usage
## Documentation

#### func GetAllCoinData
[https://godoc.org/github.com/miguelmota/go-coinmarketcap](https://godoc.org/github.com/miguelmota/go-coinmarketcap)

```go
func GetAllCoinData(limit int) (map[string]Coin, error)
```
Get information about all coins listed in Coin Market Cap.

#### type Coin

```go
type Coin struct {
ID string `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Rank int `json:"rank,string"`
PriceUsd float64 `json:"price_usd,string"`
PriceBtc float64 `json:"price_btc,string"`
Usd24hVolume float64 `json:"24h_volume_usd,string"`
MarketCapUsd float64 `json:"market_cap_usd,string"`
AvailableSupply float64 `json:"available_supply,string"`
TotalSupply float64 `json:"total_supply,string"`
PercentChange1h float64 `json:"percent_change_1h,string"`
PercentChange24h float64 `json:"percent_change_24h,string"`
PercentChange7d float64 `json:"percent_change_7d,string"`
LastUpdated string `json:"last_updated"`
}
```
## Install

Coin struct

#### func GetCoinData

```go
func GetCoinData(coin string) (Coin, error)
```bash
go get -u github.com/miguelmota/go-coinmarketcap
```
Get information about a crypto currency.

#### type CoinGraph

```go
type CoinGraph struct {
MarketCapByAvailableAupply [][]float64 `json:"market_cap_by_available_supply"`
PriceBtc [][]float64 `json:"price_btc"`
PriceUsd [][]float64 `json:"price_usd"`
VolumeUsd [][]float64 `json:"volume_usd"`
}
```

CoinGraph struct

#### func GetCoinGraphData

```go
func GetCoinGraphData(coin string, start int64, end int64) (CoinGraph, error)
```
Get graph data points for a crypto currency.

#### type GlobalMarketData

```go
type GlobalMarketData struct {
TotalMarketCapUsd float64 `json:"total_market_cap_usd"`
Total24hVolumeUsd float64 `json:"total_24h_volume_usd"`
BitcoinPercentageOfMarketCap float64 `json:"bitcoin_percentage_of_market_cap"`
ActiveCurrencies int `json:"active_currencies"`
ActiveAssets int `json:"active_assets"`
ActiveMarkets int `json:"active_markets"`
}
```
## Examples

GlobalMarketData struct

#### func GetMarketData

```go
func GetMarketData() (GlobalMarketData, error)
```

# Examples

```go
package main

import (
"fmt"
"log"
"time"

coinApi "github.com/miguelmota/go-coinmarketcap"
)

func main() {
// Get global market data
marketInfo, err := coinApi.GetMarketData()
if err != nil {
log.Println(err)
} else {
fmt.Println(marketInfo)
}

// Get info about coin
coinInfo, err := coinApi.GetCoinData("ethereum")
if err != nil {
log.Println(err)
} else {
fmt.Println(coinInfo)

}
// Get top 10 coins
top10, err := coinApi.GetAllCoinData(10)
if err != nil {
log.Println(err)
} else {
fmt.Println(top10)
}

var threeMonths int64 = (60 * 60 * 24 * 90)
now := time.Now()
secs := now.Unix()
start := secs - threeMonths
end := secs

// Get graph data for coin
coinGraphData, err := coinApi.GetCoinGraphData("ethereum", start, end)
if err != nil {
log.Println(err)
} else {
fmt.Println(coinGraphData)
}

}
```
Check out the [`./example`](./example) folder.

# License
## License

MIT
MIT
53 changes: 48 additions & 5 deletions coinmarketcap.go
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Coin Market Cap API in golang
// Coin Market Cap API fo golang
package coinmarketcap

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"

"github.com/anaskhan96/soup"
)

var (
Expand All @@ -14,7 +18,7 @@ var (
url string
)

// Get information about the global market data of the cryptocurrencies.
// GetMarketData get information about the global market data of the cryptocurrencies
func GetMarketData() (GlobalMarketData, error) {
url = fmt.Sprintf(baseUrl + "/global/")

Expand All @@ -29,7 +33,7 @@ func GetMarketData() (GlobalMarketData, error) {
return data, nil
}

// Get information about a crypto currency.
// GetCoinData get information about a crypto currency
func GetCoinData(coin string) (Coin, error) {
url = fmt.Sprintf("%s/ticker/%s", baseUrl, coin)
resp, err := makeReq(url)
Expand All @@ -45,7 +49,7 @@ func GetCoinData(coin string) (Coin, error) {
return data[0], nil
}

// Get information about all coins listed in Coin Market Cap.
// GetAllCoinData get information about all coins listed in Coin Market Cap
func GetAllCoinData(limit int) (map[string]Coin, error) {
var l string
if limit >= 0 {
Expand All @@ -69,7 +73,7 @@ func GetAllCoinData(limit int) (map[string]Coin, error) {
return allCoins, nil
}

// Get graph data points for a crypto currency.
// GetCoinGraphData get graph data points for a crypto currency
func GetCoinGraphData(coin string, start int64, end int64) (CoinGraph, error) {
url = fmt.Sprintf("%s/%s/%d/%d", graphUrl, coin, start*1000, end*1000)
resp, err := makeReq(url)
Expand All @@ -85,6 +89,33 @@ func GetCoinGraphData(coin string, start int64, end int64) (CoinGraph, error) {
return data, nil
}

// CoinMarkets get market data for a coin name.
func CoinMarkets(coin string) ([]Market, error) {
url := fmt.Sprintf("https://coinmarketcap.com/currencies/%s/#markets", coin)
var markets []Market
response, err := soup.Get(url)
if err != nil {
return nil, err
}
rows := soup.HTMLParse(response).Find("table", "id", "markets-table").Find("tbody").FindAll("tr")
for _, row := range rows {
var data []string
for colNum, column := range row.FindAll("td") {
for _, link := range column.FindAll("a") {
data = append(data, strings.TrimSpace(link.Text()))
}
if colNum == 0 || colNum == 5 || colNum == 6 {
data = append(data, column.Text())
}
for _, span := range column.FindAll("span") {
data = append(data, strings.TrimSpace(span.Text()))
}
}
markets = append(markets, Market{Rank: toInt(data[0]), Exchange: data[1], Pair: data[2], Volume: toInt(data[3]), Price: toFloat(data[4]), PercentVolume: toFloat(data[5]), Updated: (data[6] == "Recently")})
}
return markets, nil
}

// HTTP Client
func doReq(req *http.Request) ([]byte, error) {
client := &http.Client{}
Expand Down Expand Up @@ -117,3 +148,15 @@ func makeReq(url string) ([]byte, error) {

return resp, err
}

// helper Function for CoinMarkets
func toInt(raw_int string) int {
parsed, _ := strconv.Atoi(strings.Replace(strings.Replace(raw_int, "$", "", -1), ",", "", -1))
return parsed
}

// helper Function for CoinMarkets
func toFloat(raw_float string) float64 {
parsed, _ := strconv.ParseFloat(strings.Replace(strings.Replace(strings.Replace(raw_float, "$", "", -1), ",", "", -1), "%", "", -1), 64)
return parsed
}
39 changes: 39 additions & 0 deletions coinmarketcap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package coinmarketcap

import "testing"

func TestGetMarketData(t *testing.T) {

}

func TestGetCoinData(t *testing.T) {

}

func TestGetAllCoinData(t *testing.T) {

}

func TestGetCoinGraphData(t *testing.T) {

}

func TestCoinMarkets(t *testing.T) {

}

func TestDoReq(t *testing.T) {

}

func TestMakeReq(t *testing.T) {

}

func TestToInt(t *testing.T) {

}

func TestToFloat(t *testing.T) {

}
11 changes: 11 additions & 0 deletions types.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ type CoinGraph struct {
PriceUsd [][]float64 `json:"price_usd"`
VolumeUsd [][]float64 `json:"volume_usd"`
}

//CoinMarkets struct
type Market struct {
Rank int
Exchange string
Pair string
Volume int
Price float64
PercentVolume float64
Updated bool
}

0 comments on commit 934d608

Please sign in to comment.