-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock.go
67 lines (58 loc) · 1.49 KB
/
stock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package models
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
type Stock struct {
Id string `json:"id"`
Name string `json:"name"`
Start float32 `json:"start"`
Current float32 `json:"current"`
Top float32 `json:"top"`
Down float32 `json:"down"`
Time string `json:"time"`
}
func GetStockMsg(stockid string) (stock *Stock,err error){
stock = new(Stock)
httpclient := &http.Client{}
url := "http://hq.sinajs.cn/list=" + stockid
resp, _ := httpclient.Get(url)
fmt.Println("response Status:", resp.Status)
body, _ := ioutil.ReadAll(resp.Body)
b, _ := GbkToUtf8(body)
split := bytes.Split(b, []byte(","))
stock.Id = stockid
stock.Name = string(bytes.Split(split[0],[]byte("\""))[1])
if stock.Name == ""{
return nil, errors.New("No such a stock!")
}
stock.Start = strToFloat(string(split[1]))
stock.Current = strToFloat(string(split[2]))
stock.Top = strToFloat(string(split[3]))
stock.Down = strToFloat(string(split[4]))
stock.Time = string(split[30]) + " " + string(split[31])
return stock,nil
}
func ConverToStock(list []string)(stock *Stock,err error){
stock,err = mapToStock(listToMap(list))
return
}
func mapToStock(m map[string]interface{}) (stock *Stock,err error){
bytes, err := json.Marshal(m)
if err != nil {
return nil,err
}
json.Unmarshal(bytes,&stock)
return stock,nil
}
func listToMap(l []string) (map[string]interface{}){
m := make(map[string]interface{})
m["id"] = l[0]
m["name"] = l[1]
m["start"] = l[2]
return m
}