Created
December 15, 2013 03:45
-
-
Save clone1018/7968607 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// dogexrate project main.go | |
package main | |
import ( | |
"bytes" | |
"crypto/hmac" | |
"crypto/sha512" | |
"crypto/tls" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
_ "mime/multipart" | |
"net/http" | |
"net/url" | |
"os" | |
"strconv" | |
"strings" | |
"time" | |
) | |
const ( | |
CoinedUpUsername = `` | |
CoinedUpKey = `` | |
CoinedUpSecret = `` | |
) | |
func main() { | |
if len(os.Args) != 2 { | |
panic("Specify the file please.") | |
} | |
args := os.Args | |
response := Response{} | |
var mega float64 = 1000000 | |
oneDogeToBtc := getPrice("DOGE_BTC") | |
oneMegadogeToBtc := oneDogeToBtc * mega | |
response.Ticker.Doge.Btc = floatToString(oneDogeToBtc) | |
response.Ticker.Megadoge.Btc = floatToString(oneMegadogeToBtc) | |
response.Ticker.Megadoge.Usd = floatToString(getBTCPrice() * oneMegadogeToBtc) | |
b, err := json.Marshal(response) | |
if err != nil { | |
panic(err) | |
} | |
output := strings.ToLower(string(b[:])) | |
fErr := ioutil.WriteFile(args[1], []byte(output), 777) | |
if fErr != nil { | |
panic(fErr) | |
} | |
} | |
func getPrice(market string) float64 { | |
tr := &http.Transport{ | |
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | |
} | |
client := &http.Client{Transport: tr} | |
timestamp := strconv.FormatInt(time.Now().Unix(), 10) | |
apiUrl := "https://api.coinedup.com" | |
resource := "/orders" | |
data := url.Values{} | |
data.Add("requestKey", timestamp) | |
data.Add("market", market) | |
u, _ := url.ParseRequestURI(apiUrl) | |
u.Path = resource | |
urlStr := fmt.Sprintf("%v", u) | |
encoded := data.Encode() | |
sign := signRequest(encoded) | |
req, err := http.NewRequest("POST", urlStr, bytes.NewBufferString(encoded)) | |
if err != nil { | |
panic("Error accessing API endpoint") | |
} | |
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | |
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36") | |
req.Header.Add("Sign", sign) | |
req.Header.Add("Key", CoinedUpKey) | |
resp, resErr := client.Do(req) | |
if resErr != nil { | |
panic(resErr) | |
} | |
defer resp.Body.Close() | |
contents, pErr := ioutil.ReadAll(resp.Body) | |
if pErr != nil { | |
panic("Problem reading response") | |
} | |
var Orders CoinedSuccessResponse | |
decodeErr := json.Unmarshal(contents, &Orders) | |
if decodeErr != nil { | |
panic(decodeErr) | |
} | |
last := len(Orders.Value) - 1 | |
rate, _ := strconv.ParseFloat(Orders.Value[last].Rate, 64) | |
return rate | |
} | |
func getBTCPrice() float64 { | |
resp, err := http.Get("https://coinbase.com/api/v1/prices/spot_rate") | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
contents, pErr := ioutil.ReadAll(resp.Body) | |
if pErr != nil { | |
panic(pErr) | |
} | |
var Rate CoinbaseResponse | |
decodeErr := json.Unmarshal(contents, &Rate) | |
if decodeErr != nil { | |
panic(decodeErr) | |
} | |
rate, _ := strconv.ParseFloat(Rate.Amount, 64) | |
return rate | |
} | |
func signRequest(postData string) string { | |
mac := hmac.New(sha512.New, []byte(CoinedUpSecret)) | |
mac.Write([]byte(postData)) | |
return fmt.Sprintf("%x", mac.Sum(nil)) | |
} | |
func floatToString(input_num float64) string { | |
// to convert a float number to a string | |
return strconv.FormatFloat(input_num, 'f', -1, 64) | |
} | |
type Response struct { | |
Ticker struct { | |
Doge struct { | |
Btc string | |
} | |
Megadoge struct { | |
Btc string | |
Usd string | |
} | |
} | |
} | |
type CoinedSuccessResponse struct { | |
Version string | |
RequestKey string | |
Value []Order | |
} | |
type Order struct { | |
Time string | |
Type string | |
Rate string | |
Volume string | |
} | |
type CoinbaseResponse struct { | |
Amount string | |
Currency string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment