-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (102 loc) · 3.14 KB
/
main.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
package main
import (
// "github.com/dli-invest/finreddit/pkg/discord"
"github.com/piquette/finance-go/quote"
"encoding/json"
"fmt"
"net/http"
"log"
"os"
"bytes"
"strings"
"strconv"
"github.com/olekukonko/tablewriter"
)
func get_content() ([] string) {
url := "https://raw.githubusercontent.com/FriendlyUser/cad_tickers_list/main/static/latest/raw_tickers.json"
res, err := http.Get(url)
if err != nil {
}
// perror(err)
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var symbols []string
err = decoder.Decode(&symbols)
if err != nil {
}
// if err != nil {
// fmt.Printf("%T\n%s\n%#v\n",err, err, err)
// switch v := err.(type){
// case *json.SyntaxError:
// fmt.Println(string(body[v.Offset-40:v.Offset]))
// }
// }
return symbols
}
func main() {
var symbols []string = get_content()
// getJson(, symbols)
// data, _ := json.Marshal([]*Bird{pigeon, pigeon})
iter := quote.List(symbols)
var stock_data [][]string
var used_symbols []string
// columns := [8]string{"Last Price",
// "Change", "Volume", "Avg Vol (3 Month)", "Vol Ratio",
// "Dollar", "Market", "Exchange"}
// stock_data = append(stock_data, []string{"Symbol", "Last Price",
// "Change", "Vol Ratio"})
for iter.Next() {
q := iter.Quote()
volume_ratio := float64(q.RegularMarketVolume) / float64(q.AverageDailyVolume3Month)
used_symbols = append(used_symbols, q.Symbol)
stock_data = append(stock_data, []string{
q.Symbol,
fmt.Sprintf("%2.2f", q.RegularMarketPrice),
fmt.Sprintf("%2.2f", q.RegularMarketChangePercent),
fmt.Sprintf("%2.2f", volume_ratio),
})
// fmt.Println(stock_data)
}
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"Symbol", "Last Price",
"Change", "Vol Ratio"})
// send header
var send_str string = "```"
for i, s := range stock_data {
table.Append(s)
log.Printf( strconv.Itoa(i))
if (i % 10 == 0 && i != 0) || i == len(stock_data) - 1 || i == len(stock_data) {
table.Render()
string_value := tableString.String()
send_str = send_str + string_value + "```"
resp, err := SendWebhook(send_str)
fmt.Println(resp)
fmt.Println(err)
// send to discord
send_str = "```"
tableString = &strings.Builder{}
table = tablewriter.NewWriter(tableString)
table.SetHeader([]string{"Symbol", "Last Price",
"Change", "Vol Ratio"})
}
}
// result1 := strings.Join(stock_data, " ")
}
type DiscordPayload struct {
Content string `json:"content,omitempty" xml:"content,omitempty" form:"content,omitempty" query:"content,omitempty"`
// Embeds []DiscordEmbed `json:"embeds,omitempty" xml:"embeds,omitempty" form:"embeds,omitempty" query:"embeds,omitempty"`
}
func SendWebhook(stockData string) (*http.Response, error){
discordUrl := os.Getenv("DISCORD_WEBHOOK")
if discordUrl == "" {
log.Fatal("DISCORD_WEBHOOK not set")
}
payload := &DiscordPayload{Content: stockData}
webhookData, err := json.Marshal(payload)
if err != nil {
log.Fatal(err)
}
resp, err := http.Post(discordUrl, "application/json", bytes.NewBuffer(webhookData))
return resp, err
}