Пример #1
0
func (w *Huobi) AnalyzeKLinePeroid(symbol string, peroid int) (ret bool, records []Record) {
	ret = false
	var huobisymbol string
	if symbol == "btc_cny" {
		huobisymbol = "huobibtccny"
	} else {
		huobisymbol = "huobiltccny"
		logger.Fatal("huobi does not support LTC by now, wait for huobi provide it.", huobisymbol)
		return
	}

	req, err := http.NewRequest("GET", fmt.Sprintf(Config["hb_kline_url"], peroid), nil)
	if err != nil {
		logger.Fatal(err)
		return
	}

	req.Header.Set("Referer", Config["hb_base_url"])
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")

	logger.Traceln(req)

	c := util.NewTimeoutClient()

	logger.Tracef("HTTP req begin AnalyzeKLinePeroid")
	resp, err := c.Do(req)
	logger.Tracef("HTTP req end AnalyzeKLinePeroid")
	if err != nil {
		logger.Traceln(err)
		return
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		logger.Tracef("HTTP returned status %v", resp)
		return
	}
	var body string

	contentEncoding := resp.Header.Get("Content-Encoding")
	logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
	logger.Traceln(resp.Header.Get("Content-Type"))
	switch contentEncoding {
	case "gzip":
		body = util.DumpGZIP(resp.Body)
	default:
		bodyByte, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			logger.Errorln("read the http stream failed")
			return
		} else {
			body = string(bodyByte)
		}
	}

	ioutil.WriteFile(fmt.Sprintf("cache/hbKLine_%03d.data", peroid), []byte(body), 0644)

	return analyzePeroidLine(fmt.Sprintf("cache/hbKLine_%03d.data", peroid))
}
Пример #2
0
func (w *Huobi) TradeKLinePeroid(peroid int) (ret bool) {
	req, err := http.NewRequest("GET", fmt.Sprintf(Config["trade_kline_url"], peroid, rand.Float64()), nil)
	if err != nil {
		logger.Fatal(err)
		return false
	}

	req.Header.Set("Referer", Config["trade_flash_url"])
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")

	logger.Traceln(req)

	if w.client == nil {
		w.client = &http.Client{nil, nil, nil}
	}
	resp, err := w.client.Do(req)
	if err != nil {
		logger.Errorln(err)
		return false
	}
	defer resp.Body.Close()
	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		switch contentEncoding {
		case "gzip":
			body = DumpGZIP(resp.Body)

		default:
			bodyByte, err := ioutil.ReadAll(resp.Body)
			if err != nil {
				logger.Errorln("read the http stream failed")
				return false
			} else {
				body = string(bodyByte)

				ioutil.WriteFile(fmt.Sprintf("cache/TradeKLine_%03d.data", peroid), bodyByte, os.ModeAppend)
			}
		}

		logger.Traceln(resp.Header.Get("Content-Type"))

		ret := strings.Contains(body, "您需要登录才能继续")
		if ret {
			logger.Traceln("您需要登录才能继续")
			return false
		} else {
			return w.AnalyzePeroidLine(fmt.Sprintf("cache/TradeKLine_%03d.data", peroid), body)
		}

	} else {
		logger.Tracef("HTTP returned status %v", resp)
	}

	return false
}
Пример #3
0
func (w *HuobiTrade) httpRequest(pParams map[string]string) (string, error) {
	v := url.Values{}
	for key, val := range pParams {
		v.Add(key, val)
	}

	req, err := http.NewRequest("POST", Config["api_url"], strings.NewReader(v.Encode()))
	if err != nil {
		logger.Fatal(err)
		return "", err
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Referer", "https://www.huobi.com/")
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	logger.Traceln(req)

	w.client = new(http.Client)
	resp, err := w.client.Do(req)
	if err != nil {
		logger.Fatal(err)
		return "", err
	}
	defer resp.Body.Close()

	logger.Tracef("api_url resp StatusCode=%v", resp.StatusCode)
	logger.Tracef("api_url resp=%v", resp)
	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		logger.Traceln(resp.Header.Get("Content-Type"))

		switch contentEncoding {
		case "gzip":
			body = DumpGZIP(resp.Body)

		default:
			bodyByte, _ := ioutil.ReadAll(resp.Body)
			body = string(bodyByte)
			ioutil.WriteFile("cache/api_url.json", bodyByte, os.ModeAppend)
		}

		logger.Traceln(body)

		return body, nil

	} else {
		logger.Tracef("resp %v", resp)
	}

	return "", nil
}
Пример #4
0
func (w *Okcoin) getOrderBook(symbol string) (ret bool, orderBook OrderBook) {
	//init to false
	ret = false
	req, err := http.NewRequest("GET", fmt.Sprintf(Config["ok_depth_url"], symbol), nil)
	if err != nil {
		logger.Fatal(err)
		return
	}

	req.Header.Set("Referer", Config["ok_base_url"])
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")

	logger.Traceln(req)

	c := util.NewTimeoutClient()
	logger.Tracef("HTTP req begin getOrderBook")
	resp, err := c.Do(req)
	logger.Tracef("HTTP req end getOrderBook")
	if err != nil {
		logger.Traceln(err)
		return
	}

	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		logger.Errorf("HTTP returned status %v", resp)
		return
	}

	var body string
	contentEncoding := resp.Header.Get("Content-Encoding")
	logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
	switch contentEncoding {
	case "gzip":
		body = util.DumpGZIP(resp.Body)

	default:
		bodyByte, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			logger.Errorln("read the http stream failed")
			return
		} else {
			body = string(bodyByte)

			ioutil.WriteFile("cache/okdepth.json", bodyByte, 0644)
		}
	}

	return w.analyzeOrderBook(body)
}
Пример #5
0
func parse_trade(trades []interface{}, trades_data *[15]Trade) {
	for k, v := range trades {
		switch vt := v.(type) {
		case map[string]interface{}:
			logger.Traceln(k, " is a map:")
			logger.Tracef("trades[%d]\n", k)

			for ik, iv := range vt {
				switch ik {
				case "time":
					trades_data[k].time = iv.(string)
				case "price":
					trades_data[k].price = InterfaceToFloat64(iv)
				case "amount":
					trades_data[k].amount = InterfaceToFloat64(iv)
				case "type":
					trades_data[k].type_ = iv.(string)
				}
			}
		default:
			logger.Errorln(k, v)
			logger.Fatalln("don't know the type, crash!")
		}
	}
}
Пример #6
0
func parse_topsell(topsells map[string]interface{}, topsells_data *[5]Top_buy_sell) {
	index := 4
	for k, v := range topsells {
		switch vt := v.(type) {
		case map[string]interface{}:
			logger.Traceln(k, " is a map:")
			logger.Tracef("topsells[%s]\n", k)

			for ik, iv := range vt {
				logger.Traceln(ik, iv)
				switch ik {
				case "price":
					topsells_data[index].Price = InterfaceToFloat64(iv)
				case "amount":
					topsells_data[index].amount = InterfaceToFloat64(iv)
				case "level":
					topsells_data[index].level = InterfaceToFloat64(iv)
				case "accu":
					topsells_data[index].accu = InterfaceToFloat64(iv)
				}
			}

			index--

		default:
			logger.Errorln(k, v)
			logger.Fatalln("don't know the type, crash!")
		}
	}
}
Пример #7
0
func (w *Huobi) getOrderBook(symbol string) (ret bool, hbOrderBook OrderBook) {
	// init to false
	ret = false
	var huobisymbol string
	if symbol == "btc_cny" {
		huobisymbol = "huobibtccny"
	} else {
		huobisymbol = "huobiltccny"
		logger.Fatal("huobi does not support LTC by now, wait for huobi provide it.", huobisymbol)
		return
	}

	rnd := util.RandomString(20)

	now := time.Now().UnixNano() / 1000000

	req, err := http.NewRequest("GET", fmt.Sprintf(Config["hb_trade_detail"], rnd, now, now), nil)
	if err != nil {
		logger.Fatal(err)
		return
	}

	req.Header.Set("Referer", Config["base_url"])
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	req.Header.Add("Accept-Encoding", "identity")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		logger.Traceln(err)
		logger.Traceln(req)
		logger.Traceln(resp)
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		logger.Errorf("HTTP returned status %v", resp)
		return
	}

	var body string
	contentEncoding := resp.Header.Get("Content-Encoding")
	logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
	switch contentEncoding {
	case "gzip":
		body = util.DumpGZIP(resp.Body)
	default:
		bodyByte, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			logger.Errorln("read the http stream failed")
			return
		} else {
			body = string(bodyByte)
			ioutil.WriteFile("cache/OrderBook.json", bodyByte, 0644)
		}
	}

	return w.analyzeOrderBook(body)
}
Пример #8
0
func (w *OkcoinTrade) httpRequest(api_url string, pParams map[string]string) (string, error) {
	pParams["sign"] = w.createSign(pParams)

	v := url.Values{}
	for key, val := range pParams {
		v.Add(key, val)
	}

	req, err := http.NewRequest("POST", api_url, strings.NewReader(v.Encode()))
	if err != nil {
		logger.Fatal(err)
		return "", err
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Referer", "https://www.okcoin.cn/")
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	logger.Traceln(req)

	c := util.NewTimeoutClient()

	logger.Tracef("HTTP req begin OkcoinTrade")
	resp, err := c.Do(req)
	logger.Tracef("HTTP req end OkcoinTrade")
	if err != nil {
		logger.Fatal(err)
		return "", err
	}
	defer resp.Body.Close()

	logger.Tracef("api_url resp StatusCode=%v", resp.StatusCode)
	logger.Tracef("api_url resp=%v", resp)
	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		logger.Traceln(resp.Header.Get("Content-Type"))

		switch contentEncoding {
		case "gzip":
			body = util.DumpGZIP(resp.Body)

		default:
			bodyByte, _ := ioutil.ReadAll(resp.Body)
			body = string(bodyByte)
			ioutil.WriteFile("cache/okapi_url.json", bodyByte, 0644)
		}

		return body, nil

	} else {
		logger.Tracef("resp %v", resp)
	}

	return "", nil
}
Пример #9
0
func parse_buy_sell(sells_buys []interface{}, sells_buys_data *[10]SellBuy) {
	for k, v := range sells_buys {
		switch vt := v.(type) {
		case map[string]interface{}:
			logger.Traceln(k, " is a map:")
			logger.Tracef("sells/buys[%d]\n", k)

			for ik, iv := range vt {
				switch ik {
				case "price":
					sells_buys_data[k].price = InterfaceToFloat64(iv)
				case "level":
					sells_buys_data[k].level = InterfaceToFloat64(iv)
				case "amount":
					sells_buys_data[k].amount = InterfaceToFloat64(iv)
				}
			}
		default:
			logger.Errorln(k, v)
			logger.Fatalln("don't know the type, crash!")
		}
	}
}
Пример #10
0
func (w *Huobi) Login() bool {
	login_url := Config["login_url"]
	email := Config["email"]
	password := Config["password"]

	post_arg := url.Values{"email": {email}, "password": {password}}

	//logger.Traceln(strings.NewReader(post_arg.Encode()))
	req, err := http.NewRequest("POST", login_url, strings.NewReader(post_arg.Encode()))
	if err != nil {
		logger.Fatal(err)
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Referer", "https://www.huobi.com/")
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	logger.Traceln(req)

	//jar := NewJar()
	jar, _ := cookiejar.New(nil)
	w.client = &http.Client{nil, nil, jar}
	//w.client = new(http.Client)
	resp, err := w.client.Do(req)
	if err != nil {
		logger.Fatal(err)
	}
	defer resp.Body.Close()
	logger.Tracef("Login resp StatusCode=%v", resp.StatusCode)
	logger.Tracef("Login resp=%v", resp)
	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		switch contentEncoding {
		case "gzip":
			body = DumpGZIP(resp.Body)

		default:
			bodyByte, _ := ioutil.ReadAll(resp.Body)
			body = string(bodyByte)
			ioutil.WriteFile("cache/login.html", bodyByte, os.ModeAppend)
		}

		logger.Traceln(resp.Header.Get("Content-Type"))
		ret := strings.Contains(body, "用户名或者密码错误")
		if ret {
			logger.Traceln("用户名或者密码错误")
			return false
		}

		w.isLogin = true
		return true
	} else if resp.StatusCode == 500 {
		w.isLogin = true
		return true
	} else {
		logger.Tracef("resp %v", resp)
	}

	return false
}
Пример #11
0
func (w *Huobi) TradeDetail() (ret bool) {
	rnd := RandomString(20)

	now := time.Now().UnixNano() / 1000000

	req, err := http.NewRequest("GET", fmt.Sprintf(Config["trade_detail"], rnd, now, now), nil)
	if err != nil {
		logger.Fatal(err)
	}

	req.Header.Set("Referer", Config["base_url"])
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	req.Header.Add("Accept-Encoding", "identity")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		logger.Traceln(err)
		logger.Traceln(req)
		logger.Traceln(resp)
		return false
	}
	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		switch contentEncoding {
		case "gzip":
			body = DumpGZIP(resp.Body)

		default:
			bodyByte, err := ioutil.ReadAll(resp.Body)
			if err != nil {
				logger.Errorln("read the http stream failed")
				return false
			} else {
				body = string(bodyByte)

				ioutil.WriteFile("cache/TradeDetail.json", bodyByte, os.ModeAppend)
			}
		}

		logger.Traceln(resp.Header.Get("Content-Type"))

		if resp.Header.Get("Content-Type") == "application/json" {
			doc := json.NewDecoder(strings.NewReader(body))

			type Msg struct {
				Code int
				Msg  string
			}

			var m Msg
			if err := doc.Decode(&m); err == io.EOF {
				logger.Traceln(err)
			} else if err != nil {
				logger.Fatal(err)
			}
			logger.Traceln(m)

			if m.Code == 0 {
				return true
			} else {
				logger.Errorln(m)
				return false
			}
		} else {
			ret := strings.Contains(body, "您需要登录才能继续")
			if ret {
				logger.Errorln("您需要登录才能继续")
				logger.Errorln(body)
				return false
			} else {
				return w.TradeDetailAnalyze(body)
			}
		}
	} else {
		logger.Errorf("HTTP returned status %v", resp)
	}

	logger.Errorln("why in here?")
	return false
}
Пример #12
0
func (w *Huobi) TradeDelegation() bool {
	fmt.Println(w.isLogin)
	if w.isLogin == false {
		if w.Login() == false {
			return false
		}
	}

	req, err := http.NewRequest("GET", Config["trade_delegation"], nil)
	if err != nil {
		logger.Fatal(err)
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Referer", Config["trade_add_url"])
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	req.Header.Add("X-Requested-With", "XMLHttpRequest")

	logger.Traceln(req)

	resp, err := w.client.Do(req)
	if err != nil {
		logger.Fatal(err)
	}
	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		switch contentEncoding {
		case "gzip":
			body = DumpGZIP(resp.Body)

		default:
			bodyByte, _ := ioutil.ReadAll(resp.Body)
			body = string(bodyByte)
			ioutil.WriteFile("cache/TradeDelegation.html", bodyByte, os.ModeAppend)
		}

		logger.Traceln(resp.Header.Get("Content-Type"))

		if resp.Header.Get("Content-Type") == "application/json" {
			doc := json.NewDecoder(strings.NewReader(body))

			type Msg struct {
				Code int
				Msg  string
			}

			for {
				var m Msg
				if err := doc.Decode(&m); err == io.EOF {
					logger.Traceln(err)
					break
				} else if err != nil {
					logger.Fatal(err)
				}
				logger.Traceln(m)

				if m.Code == 0 {
					return true
				} else {
					return false
				}
			}

			return false
		} else {
			ret := strings.Contains(body, "您需要登录才能继续")
			if ret {
				logger.Traceln("您需要登录才能继续")
				w.isLogin = false
				return false
			} else {
				return w.TradeDelegationAnalyze(body)
			}
		}

	} else {
		logger.Tracef("HTTP returned status %v", resp)
	}

	return false
}
Пример #13
0
func (w *Huobi) QueryMyTradeInfo() bool {
	fmt.Println(w.isLogin)
	if w.isLogin == false {
		if w.Login() == false {
			return false
		}
	}

	req, err := http.NewRequest("GET", fmt.Sprintf(Config["my_trade_info"], rand.Float64()), nil)
	if err != nil {
		logger.Fatal(err)
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Referer", Config["trade_query_url"])
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	req.Header.Add("X-Requested-With", "XMLHttpRequest")

	logger.Traceln(req)

	resp, err := w.client.Do(req)
	if err != nil {
		logger.Fatal(err)
	}
	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		switch contentEncoding {
		case "gzip":
			body = DumpGZIP(resp.Body)

		default:
			bodyByte, _ := ioutil.ReadAll(resp.Body)
			body = string(bodyByte)
			ioutil.WriteFile("cache/MyTradeInfo.json", bodyByte, os.ModeAppend)
		}

		logger.Traceln(resp.Header.Get("Content-Type"))

		if resp.Header.Get("Content-Type") == "application/json" {
			w.TradeMyTradeInfoAnalyze(body)
			if w.MyTradeInfo.Code == 0 {
				return true
			} else {
				return false
			}
		} else {
			ret := strings.Contains(body, "您需要登录才能继续")
			if ret {
				logger.Traceln("您需要登录才能继续")
				w.isLogin = false
				return false
			} else {
				return true
			}
		}

	} else {
		logger.Tracef("HTTP returned status %v", resp)
	}

	return false
}
Пример #14
0
func (w *Huobi) TradeUpdate(id, price, amount string) bool {
	post_arg := url.Values{
		"id":     {id},
		"price":  {price},
		"amount": {amount},
	}

	req, err := http.NewRequest("POST", Config["trade_update_url"], strings.NewReader(post_arg.Encode()))
	if err != nil {
		logger.Fatal(err)
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Referer", "https://www.huobi.com/")
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	req.Header.Add("X-Requested-With", "XMLHttpRequest")

	logger.Traceln(req)

	resp, err := w.client.Do(req)
	if err != nil {
		logger.Fatal(err)
	}
	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		switch contentEncoding {
		case "gzip":
			body = DumpGZIP(resp.Body)

		default:
			bodyByte, _ := ioutil.ReadAll(resp.Body)
			body = string(bodyByte)
			ioutil.WriteFile("cache/TradeUpdate.json", bodyByte, os.ModeAppend)
		}

		logger.Traceln(resp.Header.Get("Content-Type"))

		if resp.Header.Get("Content-Type") == "application/json" {
			doc := json.NewDecoder(strings.NewReader(body))

			type Msg struct {
				Code int
				Msg  string
			}

			var m Msg
			if err := doc.Decode(&m); err == io.EOF {
				logger.Traceln(err)
			} else if err != nil {
				logger.Fatal(err)
			}

			logger.Traceln(m)

			if m.Code == 0 {
				return true
			} else {
				return false
			}
		} else {
			ret := strings.Contains(body, "您需要登录才能继续")
			if ret {
				logger.Traceln("您需要登录才能继续")
				w.isLogin = false
				return false
			} else {
				return true
			}
		}

	} else {
		logger.Tracef("HTTP returned status %v", resp)
	}

	return false
}
Пример #15
0
/*
	txcode := map[int]string{
		0:  `买单已委托,<a href="/trade/index.php?a=delegation">查看结果</a>`,
		2:  `没有足够的人民币`,
		10:	`没有足够的比特币`,
		16: `您需要登录才能继续`,
		17: `没有权限`,
		42:	`该委托已经取消,不能修改`,
		44:	`交易价钱太低`,
		56:`卖出价格不能低于限价的95%`}

	logger.Traceln(txcode[m.Code])
*/
func (w *BitvcTrade) Login() bool {
	login_url := Config["bitvc_login_url"]
	email := Config["bitvc_email"]
	clear_password := Config["bitvc_password"]
	password := util.Md5(clear_password + "hi,pwd")

	/*
			function calc_password_security_score(t) {
		    var e = 0;
		    return t.length < 4 ? e :
		     (t.length >= 8 && e++, t.length >= 10 && e++, /[a-z]/.test(t) && /[A-Z]/.test(t) && e++, /[0-9]/.test(t) && e++, /.[!,@,#,$,%,^,&,*,?,_,~, -,£,(,)]/.test(t) && e++, e)
			}*/

	var pwd_security_score int

	if len(clear_password) < 4 {
		pwd_security_score = 0
	} else if len(clear_password) >= 8 {
		pwd_security_score++
		if len(clear_password) >= 10 {
			pwd_security_score++
		}
	}
	//f**k正则,不玩了!
	reg := regexp.MustCompile(`[[:ascii:]]`)
	fmt.Printf("%q\n", reg.FindAllString(password, -1))
	// ["H" " 世界!123 G" "."]

	str_pwd_security_score := fmt.Sprintf("%d", pwd_security_score)
	post_arg := url.Values{"email": {email}, "password": {password}, "backurl": {"/index/index"}, "pwd_security_score": {str_pwd_security_score}}

	//logger.Traceln(strings.NewReader(post_arg.Encode()))
	req, err := http.NewRequest("POST", login_url, strings.NewReader(post_arg.Encode()))
	if err != nil {
		logger.Fatal(err)
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Referer", Config["bitvc_base_url"])
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	logger.Traceln(req)

	//jar := NewJar()
	jar, _ := cookiejar.New(nil)
	w.client = &http.Client{nil, nil, jar}
	//w.client = new(http.Client)
	resp, err := w.client.Do(req)
	if err != nil {
		logger.Fatal(err)
	}
	defer resp.Body.Close()
	logger.Tracef("Login resp StatusCode=%v", resp.StatusCode)
	logger.Tracef("Login resp=%v", resp)
	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		switch contentEncoding {
		case "gzip":
			body = DumpGZIP(resp.Body)

		default:
			bodyByte, _ := ioutil.ReadAll(resp.Body)
			body = string(bodyByte)
			ioutil.WriteFile("login.html", bodyByte, os.ModeAppend)
		}

		logger.Traceln(resp.Header.Get("Content-Type"))
		ret := strings.Contains(body, "用户名或者密码错误")
		if ret {
			logger.Traceln("用户名或者密码错误")
			return false
		}

		w.isLogin = true
		return true
	} else if resp.StatusCode == 500 {
		w.isLogin = true
		return true
	} else {
		logger.Tracef("resp %v", resp)
	}

	return false
}
Пример #16
0
func (w *Huobi) AnalyzeKLineMinute(symbol string) (ret bool) {
	var huobisymbol string
	if symbol == "btc_cny" {
		huobisymbol = "huobibtccny"
	} else {
		huobisymbol = "huobiltccny"
		logger.Fatal("huobi does not support LTC by now, wait for huobi provide it.", huobisymbol)
		return false
	}

	req, err := http.NewRequest("GET", Config["trade_fenshi"], nil)
	if err != nil {
		logger.Fatal(err)
	}

	req.Header.Set("Referer", Config["trade_flash_url"])
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")

	logger.Traceln(req)

	c := util.NewTimeoutClient()
	logger.Tracef("HTTP req begin AnalyzeKLineMinute")
	resp, err := c.Do(req)
	logger.Tracef("HTTP req end AnalyzeKLineMinute")
	if err != nil {
		logger.Traceln(err)
		return false
	}
	defer resp.Body.Close()
	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		switch contentEncoding {
		case "gzip":
			body = DumpGZIP(resp.Body)

		default:
			bodyByte, err := ioutil.ReadAll(resp.Body)
			if err != nil {
				logger.Errorln("read the http stream failed")
				return false
			} else {
				body = string(bodyByte)

				ioutil.WriteFile(fmt.Sprintf("cache/TradeKLine_minute.data"), bodyByte, 0644)
			}
		}

		logger.Traceln(resp.Header.Get("Content-Type"))

		ret := strings.Contains(body, "您需要登录才能继续")
		if ret {
			logger.Traceln("您需要登录才能继续")
			return false
		} else {
			return w.analyzeMinuteLine(fmt.Sprintf("cache/TradeKLine_minute.data"), body)
		}

	} else {
		logger.Tracef("HTTP returned status %v", resp)
	}

	return false
}
Пример #17
0
/*
	SEE DOC:
	TRADE API
	https://www.okcoin.com/t-1000097.html

	行情API
	https://www.okcoin.com/shequ/themeview.do?tid=1000052&currentPage=1

	//non-official API :P
	K线数据step单位为second
	https://www.okcoin.com/kline/period.do?step=60&symbol=okcoinbtccny&nonce=1394955131098

	https://www.okcoin.com/kline/trades.do?since=10625682&symbol=okcoinbtccny&nonce=1394955760557

	https://www.okcoin.com/kline/depth.do?symbol=okcoinbtccny&nonce=1394955767484

	https://www.okcoin.com/real/ticker.do?symbol=0&random=61

	//old kline for btc
	日数据
	https://www.okcoin.com/klineData.do?type=3&marketFrom=0
	5分钟数据
	https://www.okcoin.com/klineData.do?type=1&marketFrom=0

	/for ltc
	https://www.okcoin.com/klineData.do?type=3&marketFrom=3
*/
func (w *Okcoin) AnalyzeKLinePeroid(symbol string, peroid int) (ret bool, records []Record) {
	var oksymbol string
	if symbol == "btc_cny" {
		oksymbol = "okcoinbtccny"
	} else {
		oksymbol = "okcoinltccny"
	}

	ret = false
	now := time.Now().UnixNano() / 1000000

	req, err := http.NewRequest("GET", fmt.Sprintf(Config["ok_kline_url"], 60*peroid, oksymbol, now), nil)
	if err != nil {
		logger.Fatal(err)
		return
	}

	req.Header.Set("Referer", Config["ok_base_url"])
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")

	logger.Traceln(req)

	c := util.NewTimeoutClient()
	logger.Tracef("okHTTP req begin")
	resp, err := c.Do(req)
	logger.Tracef("okHTTP req end")
	if err != nil {
		logger.Traceln(err)
		return
	}

	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		logger.Tracef("HTTP returned status %v", resp)
		return
	}

	var body string
	contentEncoding := resp.Header.Get("Content-Encoding")

	logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
	logger.Traceln(resp.Header.Get("Content-Type"))

	switch contentEncoding {
	case "gzip":
		body = util.DumpGZIP(resp.Body)

	default:
		bodyByte, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			logger.Errorln("read the http stream failed")
			return
		} else {
			body = string(bodyByte)

			ioutil.WriteFile(fmt.Sprintf("cache/okTradeKLine_%03d.data", peroid), bodyByte, 0644)
		}
	}

	return analyzePeroidLine(body)
}
Пример #18
0
func (p *Request) DoJSON() (*Json, error) {
	j, _ := NewJson([]byte(""))
	var req *http.Request
	var err error
	if p.Method == "POST" {
		req, err = http.NewRequest(p.Method, p.Uri, strings.NewReader(p.EncodeParams))
	} else {
		req, err = http.NewRequest(p.Method, p.Uri+"?"+p.EncodeParams, nil)
	}

	if err != nil {
		logger.Fatal(err)
		return j, err
	}

	if p.Method == "POST" {
		req.Header.Add("Accept", "application/json")
		req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	}

	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
	logger.Traceln(req)

	c := util.NewTimeoutClient()

	logger.Tracef("HTTP req begin HuobiTrade")
	resp, err := c.Do(req)
	logger.Tracef("HTTP req end HuobiTrade")
	if err != nil {
		logger.Fatal(err)
		return j, err
	}
	defer resp.Body.Close()

	logger.Tracef("api_url resp StatusCode=%v", resp.StatusCode)
	logger.Tracef("api_url resp=%v", resp)
	if resp.StatusCode == 200 {
		var body string

		contentEncoding := resp.Header.Get("Content-Encoding")
		logger.Tracef("HTTP returned Content-Encoding %s", contentEncoding)
		logger.Traceln(resp.Header.Get("Content-Type"))

		switch contentEncoding {
		case "gzip":
			body = util.DumpGZIP(resp.Body)

		default:
			bodyByte, _ := ioutil.ReadAll(resp.Body)
			body = string(bodyByte)
			ioutil.WriteFile("cache/api_url.json", bodyByte, 0644)
		}

		logger.Traceln(body)

		return NewJson([]byte(body))

	} else {
		logger.Tracef("resp %v", resp)
	}

	return j, nil
}