Created
February 20, 2014 12:58
-
-
Save bernatfp/9112972 to your computer and use it in GitHub Desktop.
This is a small command line utility that displays last trade price in USD for Litecoin and Bitcoin in BTC-E.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"encoding/json" | |
"time" | |
) | |
type Data struct { | |
Last float64 | |
} | |
type Ticker struct { | |
Ticker Data | |
} | |
type Prices struct { | |
Btc float64 | |
Ltc float64 | |
} | |
func getPrice(Pair string) float64 { | |
time.Sleep(300 * time.Millisecond) //without a wait the API blocks access | |
resp, err := http.Get("http://btc-e.com/api/2/" + Pair + "/ticker") | |
if err != nil { | |
return -2 | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
var apidata Ticker | |
err = json.Unmarshal(body, &apidata) | |
if err != nil { | |
return -1 | |
} | |
return apidata.Ticker.Last | |
} | |
func getLtcPrice(c chan float64){ | |
c <- getPrice("ltc_usd") | |
} | |
func getBtcPrice(c chan float64){ | |
c <- getPrice("btc_usd") | |
} | |
func main() { | |
var last Prices | |
btcChan := make(chan float64) | |
ltcChan := make(chan float64) | |
go getBtcPrice(btcChan) | |
go getLtcPrice(ltcChan) | |
last.Btc = <- btcChan | |
last.Ltc = <- ltcChan | |
fmt.Printf("BTC: %f\tLTC: %f\t", last.Btc, last.Ltc) | |
go getBtcPrice(btcChan) | |
go getLtcPrice(ltcChan) | |
for { | |
select { | |
case last.Btc = <- btcChan : | |
go getBtcPrice(btcChan) | |
case last.Ltc = <- ltcChan : | |
go getLtcPrice(ltcChan) | |
} | |
fmt.Print("\r ") | |
fmt.Print("\r") | |
fmt.Printf("BTC: %f\tLTC: %f\t", last.Btc, last.Ltc) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment