-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-loader.go
More file actions
56 lines (45 loc) · 1.3 KB
/
data-loader.go
File metadata and controls
56 lines (45 loc) · 1.3 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
package main
import (
// "fmt"
// "strconv"
// "strings"
"github.com/gocolly/colly"
)
func GetStocksWithUrls(market string, stockList *[]Stock) []Stock {
url := "https://www.jamstockex.com/trading/trade-quotes/?market=" + market + "-market"
// stockList := make([]Stock, 0, 120)
c := colly.NewCollector()
// search for table with main market stocks in html
c.OnHTML("table", func(e *colly.HTMLElement) {
if e.Index == 2 {
e.ForEach("tbody tr", func(_ int, el *colly.HTMLElement) {
var s Stock = Stock{
Ticker: el.ChildText("td:nth-child(2)"),
JSEUrl: el.ChildAttr("td:nth-child(2) a ", "href"),
}
*stockList = append(*stockList, s)
})
}
})
//attempt to visit url
c.Visit(url)
return *stockList
}
// load all stocks from JSE
func loadJSE(history bool, refresh_list bool) []Stock {
var length int
var stockList []Stock = make([]Stock, 0, 120)
// TODO: Keep history when list is refreshed
// TODO: account for new stocks when list is refreshed
if refresh_list {
stockList = GetStocksWithUrls(MARKET_COMBINED, &stockList)
} else {
read_stocks_from_json_file("stocklist.json", &stockList)
}
length = len(stockList)
for i := 0; i < length; i++ {
stockList[i].loadStock(history)
}
write_text_file(get_json_list(stockList), "stocklist.json")
return stockList
}