bcapi

package module
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 11 Imported by: 0

README

bestchange-api wrapper on golang

Installation

  1. Install the package
    go get -u github.com/bulatok/bestchange-api
  1. Import to your code
    import bcapi "github.com/bulatok/bestchange-api"

Usage

Simple example of usage:

$ touch test.go
package main

import (
	bcapi "github.com/bulatok/bestchange-api"
	"log"
	"fmt"
	"net/http"
)

func main() {
	myClient := &http.Client{
		Timeout: 30 * time.Second,
        }
		
	bc, err := bcapi.NewBestchange(myClient)
	if err != nil {
		log.Fatal(err)
	}

	rates, err := bc.GetRatesFromTo("Bitcoin (BTC)", "QIWI RUB") // BTC -> QIWI
	if err != nil {
		log.Fatal(err)
	}

	for _, v := range rates {
		fmt.Println(v.String())
	}
}
   $ go run test.go

Result -

BTC -> RUB QIWI
Market - ExLine
Price (BTC) - 1 (can buy from 0.006 - to 0.024594)
Recive (RUB QIWI) - 4066000.00000005
Rating - 0.738
Market link - https://www.bestchange.ru/click.php?id=952&from=93&to=63&city=0
Link to full list - https://www.bestchange.ru/bitcoin-to-qiwi.html

BTC -> RUB QIWI
Market - 1Обмен
Price (BTC) - 1 (can buy from 0.008 - to 0.188452)
Recive (RUB QIWI) - 3986705.50779997
Rating - 0.11174 # negative - 0, positive - 11174
Market link - https://www.bestchange.ru/click.php?id=666&from=93&to=63&city=0
Link to full list - https://www.bestchange.ru/bitcoin-to-qiwi.html

.........
......... # etc
or
package main

import (
    bcapi "github.com/bulatok/bestchange-api"
    "log"
    "fmt"
    "time"
    "net/http"
)

func main() {
	myClient := &http.Client{
		Timeout: 30 * time.Second,
	}
	bc, err := bcapi.NewBestchange(myClient)
	if err != nil {
		log.Fatal(err)
	}
	
	rates, err := bc.GetRatesFromTo("Bitcoin (BTC)", "QIWI RUB") // BTC -> QIWI
	if err != nil{
		log.Fatal(err)
	}
	
	rates = bcapi.SortRatesByReceive(rates) // sorting the rates by their receive prices

	for _, v := range rates{
		json, err := v.JSON()
		if err != nil{
			log.Fatal(err)
		}
		fmt.Println(json)
	}

	time.Sleep(time.Minute) // just for example to see the differences

	if err := bc.UpdateRates(myClient); err != nil{ // we can easily update the rates without creating new objects.
		log.Fatal(err)
	}

	for _, v := range rates{
		json, err := v.JSON()
		if err != nil{
			log.Fatal(err)
		}
		fmt.Println(json)
	}
}
   $ go run test.go

Result -

{
  "Rate": {
    "coin_from": {
      "coin_id": "93",
      "coin_full_name": "Bitcoin (BTC)",
      "coin_short_name": "BTC"
    },
    "coin_to": {
      "coin_id": "63",
      "coin_full_name": "QIWI RUB",
      "coin_short_name": "RUB QIWI"
    },
    "price": "1",
    "price_from": "0.01334854",
    "price_till": "0.22247561",
    "market": {
      "market_id": "577",
      "market_name": "Bit-Обменка"
    },
    "rating": "0.330",
    "receive": "2247437.4"
  },
  "MarketLink": "https://www.bestchange.ru/bitcoin-to-qiwi.html",
  "FullListLink": "https://www.bestchange.ru/bitcoin-to-qiwi.html"
}
.........
......... # etc

# after 1 minute
# it is another offer
{
  "Rate": {
    "coin_from": {
      "coin_id": "93",
      "coin_full_name": "Bitcoin (BTC)",
      "coin_short_name": "BTC"
    },
    "coin_to": {
      "coin_id": "63",
      "coin_full_name": "QIWI RUB",
      "coin_short_name": "RUB QIWI"
    },
    "price": "1",
    "price_from": "0.000185",
    "price_till": "15",
    "market": {
      "market_id": "992",
      "market_name": "OneMoment"
    },
    "rating": "0.4097",
    "receive": "2700143.55999998"
  },
  "MarketLink": "https://www.bestchange.ru/bitcoin-to-qiwi.html",
  "FullListLink": "https://www.bestchange.ru/bitcoin-to-qiwi.html"
}
.......
....... # etc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// CoinNames maps long name to short names of exchangers
	CoinNames = map[string]string{}/* 214 elements not displayed */

)
View Source
var (
	ErrBestchange = errors.New("bcapi")
)
View Source
var (
	ErrMarketsParsing = errors.New("markets parsing")
)
View Source
var (
	ErrRatesParsing = errors.New("rates parsing")
)

Functions

func GenerateLinkCustom added in v0.2.5

func GenerateLinkCustom(alias1, alias2 string) string

GenerateLinkCustom generates link to bestchange site custom

Types

type Bestchange

type Bestchange struct {
	Coins   Coins
	Markets Markets
	Rates   []Rate
}

Bestchange is a main object of api that provides the way to get the best rates for coins

func NewBestchange

func NewBestchange(client *http.Client) (*Bestchange, error)

NewBestchange returns the Bestchange object with which we can find the best price for coins

As argument you should pass your specified http.Client or just pass http.DefaultClient

func (*Bestchange) GetRatesFromTo

func (b *Bestchange) GetRatesFromTo(from, to string) ([]Rate, error)

GetRatesFromTo return rates

You can pass to this function either the ID, full-name, short-name.

But ID is preferable, because it will 100% work

func (*Bestchange) UpdateCoins

func (b *Bestchange) UpdateCoins(client *http.Client) error

UpdateCoins again download the zip file and updates exactly Coins

func (*Bestchange) UpdateMarktes

func (b *Bestchange) UpdateMarktes(client *http.Client) error

UpdateMarktes again download the zip file and updates exactly Markets

func (*Bestchange) UpdateRates

func (b *Bestchange) UpdateRates(client *http.Client) error

UpdateRates again download the zip file and updates exactly Rates

type Coin

type Coin struct {
	ID        string `json:"coin_id"`
	FullName  string `json:"coin_full_name"`
	ShortName string `json:"coin_short_name"`
}

Coin has id, FullName and ShortName

type Coins

type Coins map[string]Coin

Coins is map[string]Coin, which key is the IDCoin and value is a Coin

type Market

type Market struct {
	ID   string `json:"market_id"`
	Name string `json:"market_name"`
}

Market holds the ID and its Name

type Markets

type Markets map[string]Market

Markets is alias for map[string]string

type Rate

type Rate struct {
	// CoinFrom is coin from where to where we want
	// to convert the coin
	CoinFrom Coin `json:"coin_from"`

	// CoinTo this is the coin we want
	// to convert to
	CoinTo Coin `json:"coin_to"`

	// Price is a cost per 1 coinTo in coinTo currency
	//
	// for example we want to convert QIWI_RUB -> BTC
	// we will got price 4 448 698 QIWI_RUB per 1 BTC
	Price string `json:"price"`

	// PriceFrom is a price starting from how many we can purchase
	PriceFrom string `json:"price_from"`

	// PriceTill is a price till to we can purchase
	PriceTill string `json:"price_till"`

	// Market is a market
	Market Market `json:"market"`

	// Rating is a rating of market in format negative/positive
	//
	// for example "WW-Pay" has 0/8467 rating,
	// 0 - negative, 8467 postive
	Rating string `json:"rating"`

	// Receive is number of coins that customer will get for the price
	Receive string `json:"receive"`
}

Rate contains information for coinFrom -> coinTo transaction

func SortRatesByPrice

func SortRatesByPrice(rates []Rate) []Rate

SortRatesByPrice sorts the rates ([]Rate) by their Price value

This function does not modify the data, it returns the sorted one

func SortRatesByReceive

func SortRatesByReceive(rates []Rate) []Rate

SortRatesByReceive sorts the rates ([]Rate) by their Recieve value

This function does not modify the data, it returns the sorted one

func (r *Rate) GenerateLink() string

GenerateLink generates link to bestchange site

Unfortunately, works incorrectly sometimes

func (r *Rate) GenerateMarketLink() string

GenerateMarketLink generates the link to the market of this offer

func (*Rate) JSON

func (r *Rate) JSON() (string, error)

JSON return the human-readable JSON format string

func (*Rate) String

func (r *Rate) String() string

String return the human-readable string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL