Created
January 19, 2018 02:53
-
-
Save takuo/05ad167b03326dc86c974079bf76ed11 to your computer and use it in GitHub Desktop.
coincheck_market_price.10s.go http://hitoriblog.com/?p=50182
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 ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"strconv" | |
) | |
// Rate json struct | |
type Rate struct { | |
Rate string `json:"rate"` | |
} | |
var jsonFile = os.Getenv("HOME") + "/coincheck.json" | |
func init() { | |
// os.Setenv("HTTP_PROXY", "http://192.168.0.2:3128") | |
} | |
func putsMenu() { | |
fmt.Println("---") | |
fmt.Println("ビットコイン(Bitcoin)価格チャート 5分足 | href=https://coincheck.com/ja/exchange/charts/coincheck/btc_jpy/300") | |
fmt.Println("ビットコイン(Bitcoin)価格チャート 15分足 | href=https://coincheck.com/ja/exchange/charts/coincheck/btc_jpy/900") | |
fmt.Println("ビットコイン(Bitcoin)価格チャート 1時間足 | href=https://coincheck.com/ja/exchange/charts/coincheck/btc_jpy/3600") | |
fmt.Println("ビットコイン(Bitcoin)価格チャート 4時間足 | href=https://coincheck.com/ja/exchange/charts/coincheck/btc_jpy/14400") | |
fmt.Println("ビットコイン(Bitcoin)価格チャート 24時間足 | href=https://coincheck.com/ja/exchange/charts/coincheck/btc_jpy/86400") | |
} | |
func main() { | |
color := "black" | |
diff := "0.0" | |
resp, err := http.Get("https://coincheck.com/api/rate/btc_jpy") | |
defer resp.Body.Close() | |
if err != nil { | |
panic(err) | |
} | |
jsonData, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
var rate Rate | |
if err := json.Unmarshal(jsonData, &rate); err != nil { | |
panic(err) | |
} | |
current, _ := strconv.ParseFloat(rate.Rate, 64) | |
if _, err := os.Stat(jsonFile); err == nil { | |
var previousRate Rate | |
j, err := ioutil.ReadFile(jsonFile) | |
if err != nil { | |
panic(err) | |
} | |
if err := json.Unmarshal(j, &previousRate); err != nil { | |
panic(err) | |
} | |
previous, _ := strconv.ParseFloat(previousRate.Rate, 64) | |
if d := current - previous; d > 0 { | |
color = "green" | |
diff = fmt.Sprintf("+%.01f", d) | |
} else if d < 0 { | |
color = "red" | |
diff = fmt.Sprintf("%.01f", d) | |
} | |
} | |
ioutil.WriteFile(jsonFile, jsonData, os.ModePerm) | |
fmt.Printf("%.01f (%s) | color=%s\n", current, diff, color) | |
putsMenu() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment