-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstockchart.go
More file actions
205 lines (189 loc) · 4.14 KB
/
Copy pathstockchart.go
File metadata and controls
205 lines (189 loc) · 4.14 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// package main
//
// import (
// "fmt"
// "github.com/pdevty/stockchart"
// "io/ioutil"
// "os"
// )
//
// func main() {
// // params json format
// // brand id,name from yahoo finance japan
// params := `[
// {"id":"7618","name":"ピーシーデポ"},
// {"id":"6157","name":"日進工具"},
// {"id":"7821","name":"前田工繊"},
// {"id":"7917","name":"藤森工業"},
// {"id":"4681","name":"リゾートトラスト"},
// {"id":"4301","name":"アミューズ"},
// {"id":"4290","name":"プレステージ"},
// {"id":"2780","name":"コメ兵"},
// {"id":"2695","name":"くらコーポレーション"},
// {"id":"2695","name":"内外トランスライン"}
// ]`
// // new
// sc, err := stockchart.New(params)
// if err != nil {
// panic(err)
// }
// // return chart html
// fmt.Println(sc.Html())
// // return chart csv
// fmt.Println(sc.Csv())
// // create chart html file
// ioutil.WriteFile("index.html",
// []byte(sc.Html()), os.ModePerm)
// }
package stockchart
import (
"encoding/json"
"github.com/PuerkitoBio/goquery"
"runtime"
"sort"
"strconv"
"strings"
"time"
)
type Brand struct {
Id string `json:"id"`
Name string `json:"name"`
}
type Brands []Brand
type Map struct {
Key string
Value string
}
type Maps []Map
type Client struct {
Header string
Body map[string]string
}
type Data struct {
Id string
Name string
Data [][]string
}
func (m Maps) Len() int {
return len(m)
}
func (m Maps) Less(i, j int) bool {
return m[i].Key < m[j].Key
}
func (m Maps) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func getHtml() string {
return `<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dygraph/1.2/dygraph-combined.min.js"></script>
</head>
<body>
<div id="graphdiv"></div>
<script>
var csv = "";
new Dygraph(document.getElementById("graphdiv"),csv);
</script>
</body>
</html>
`
}
func scrape(url string) ([][]string, error) {
doc, err := goquery.NewDocument(url)
if err != nil {
return nil, err
}
dat := [][]string{}
doc.Find("div.padT12 table tbody tr").Each(func(i int, s *goquery.Selection) {
if i != 0 {
t, _ := time.Parse("2006年1月2日", s.Find("td").First().Text())
dat = append(dat, []string{
t.Format("2006/01/02"),
strings.Replace(s.Find("td").Last().Text(), ",", "", 1),
})
}
})
return dat, nil
}
func getData(b Brand) (*Data, error) {
datCh := make(chan [][]string)
errCh := make(chan error)
data := [][]string{}
for i := 1; i <= 4; i++ {
go func(i int) {
url := "http://info.finance.yahoo.co.jp/history/?code=" +
b.Id + "&p=" + strconv.Itoa(i)
dat, err := scrape(url)
if err != nil {
errCh <- err
} else {
datCh <- dat
}
}(i)
}
for i := 1; i <= 4; i++ {
select {
case err := <-errCh:
return nil, err
case dat := <-datCh:
data = append(data, dat...)
}
}
return &Data{
Id: b.Id,
Name: b.Name,
Data: data,
}, nil
}
func New(params string) (*Client, error) {
runtime.GOMAXPROCS(runtime.NumCPU())
p := Brands{}
json.Unmarshal([]byte(params), &p)
header := ""
body := map[string]string{}
datCh := make(chan *Data)
errCh := make(chan error)
for _, b := range p {
go func(b Brand) {
dat, err := getData(b)
if err != nil {
errCh <- err
} else {
datCh <- dat
}
}(b)
}
for i := 0; i < len(p); i++ {
select {
case err := <-errCh:
return nil, err
case dat := <-datCh:
header += "," + dat.Name
for _, d := range dat.Data {
body[d[0]] += "," + d[1]
}
}
}
return &Client{
Header: header,
Body: body,
}, nil
}
func (c *Client) Csv() string {
r := "Date" + c.Header + "\\n"
ms := Maps{}
for k, v := range c.Body {
ms = append(ms, Map{Key: k, Value: v})
}
sort.Sort(ms)
for _, v := range ms {
r += v.Key + v.Value + "\\n"
}
return r
}
func (c *Client) Html() string {
r := strings.Replace(getHtml(),
"csv = \"\"",
"csv = \""+c.Csv()+"\"", 1)
return r
}