-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
127 lines (109 loc) · 2.61 KB
/
fetch.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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"sort"
"strings"
)
const screenerURL = "https://api.stockbit.com/v2.4/screener"
const authorizationKey = "Authorization"
const totalPage = 31
func fetchTickerList() ([]string, error) {
var res []string
c := make(chan []string)
count := 0
for i := 1; i <= totalPage; i++ {
go func(i int) {
list, err := fetchTickerListPage(i)
if err != nil {
log.Println(err)
return
}
c <- list
count++
}(i)
}
for v := range c {
res = append(res, v...)
if count == totalPage {
close(c)
}
}
sort.Strings(res)
return res, nil
}
func fetchTickerListPage(page int) ([]string, error){
data := url.Values{}
data.Set("universe", "{\"scope\":\"IHSG\",\"scopeID\":\"\",\"name\":\"IHSG\"}")
data.Set("filters", "[{\"type\":\"basic\",\"item1\":2892,\"item2\":0,\"operator\":\">\",\"multiplier\":\"\"}]")
data.Set("page", fmt.Sprintf("%d", page))
req, err := http.NewRequest(http.MethodPost, screenerURL, strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Add(authorizationKey, authToken)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var model ScreenerResponse
if err := json.Unmarshal(b, &model); err != nil {
return nil, err
}
return model.TickerList(), nil
}
func fetchFinancial(ticker string) (string, error) {
req, err := http.NewRequest(http.MethodGet, financialURL(ticker), nil)
if err != nil {
return "", err
}
req.Header.Add(authorizationKey, authToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var model FinancialResponse
if err := json.Unmarshal(b, &model); err != nil {
return "", err
}
return model.Data.HTMLReport, nil
}
func financialURL(ticker string) string {
const url = "https://api.stockbit.com/v2.4/company/financial?symbol=%s&datatype=reported&reporttype=is&statement=ANNUALLY"
return fmt.Sprintf(url, ticker)
}
type FinancialResponse struct {
Message string `json:"message"`
Data struct {
HTMLReport string `json:"htmlReport"`
} `json:"data"`
}
type ScreenerResponse struct {
Data struct{
Calcs []struct{
Company struct{
Symbol string `json:"symbol"`
} `json:"company"`
} `json:"calcs"`
} `json:"data"`
}
func (s ScreenerResponse) TickerList() []string {
var res []string
for _, v := range s.Data.Calcs {
res = append(res, v.Company.Symbol)
}
return res
}