-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorca.go
285 lines (235 loc) · 6.93 KB
/
orca.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Stocks daily price update
// Usage: go run orca.go [csv_file]
// [update_time]
//
// update_time := format HH:mm UTC time
// csv_file := [symbol,price,amount,isThaiStock]
// see sample_input.csv for more info
// TODO: Support ErrorMessage handling from API call
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/jasonlvhit/gocron"
"github.com/leekchan/accounting"
"github.com/mailgun/mailgun-go"
)
const MAILGUN_URL = "___URL___"
const MAILGUN_PRIVATE_KEY = "___API_PRIVATE_KEY___"
const MAILGUN_PUBLIC_KEY = "___API_PUBLIC_KEY___"
const USER_EMAIL = "___EMAIL___"
const QUOTE_BASE_URL = "https://finance.yahoo.com/webservice/v1/symbols/"
const QUOTE_TH_STOCK = ".BK"
const QUOTE_URL_SUFFIX = "/quote?format=json"
const QUOTE_USER_AGENT = "Mozilla/5.0 (Linux; Android 6.0.1; MotoG3 Build/MPI24.107-55) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.81 Mobile Safari/537.36"
const DEBUG = true
var user_stocks map[string]*UserStock
var update_time string
type UserStock struct {
Shares []*Share
IsThai bool
}
type Share struct {
Price float64
Amount int
}
type Response struct {
Quotes []*Quote
}
type Quote struct {
Symbol string
LastPrice float64
}
// DEBUG: Print message to console.
func debugMessage(message ...interface{}) {
if DEBUG {
fmt.Println(message)
}
}
// Check if an error has occured.
func checkError(e error) {
if e != nil {
fmt.Printf("Error: %s\n", e)
os.Exit(1)
}
}
// Override: flatten structure of response into Quote.
func (response *Response) UnmarshalJSON(b []byte) error {
var f interface{}
json.Unmarshal(b, &f)
m := f.(map[string]interface{})
n := m["list"].(map[string]interface{})
v := n["resources"]
resources := v.([]interface{})
for _, resource := range resources {
r := resource.(map[string]interface{})["resource"].(map[string]interface{})
stock := r["fields"].(map[string]interface{})
symbol := strings.ToUpper(stock["symbol"].(string))
price, _ := strconv.ParseFloat(stock["price"].(string), 64)
response.Quotes = append(response.Quotes, &Quote{symbol, price})
}
return nil
}
// Parse input data into user_stocks.
func populateUserStocks(csv_file string) {
debugMessage("populateUserStocks csv: ", csv_file)
file, err := os.Open(csv_file)
checkError(err)
r := csv.NewReader(file)
records, err := r.ReadAll()
checkError(err)
debugMessage("records:", records)
for i := 1; i < len(records); i++ {
record := records[i]
symbol := strings.ToUpper(record[0])
price, err := strconv.ParseFloat(record[1], 64)
checkError(err)
amount, err := strconv.Atoi(record[2])
checkError(err)
isBKKStock, err := strconv.ParseBool(record[3])
checkError(err)
stock := user_stocks[symbol]
share := &Share{price, amount}
if stock == nil {
stock = &UserStock{IsThai: isBKKStock, Shares: []*Share{share}}
user_stocks[symbol] = stock
} else {
stock.Shares = append(stock.Shares, share)
}
}
debugMessage("user_stocks:", user_stocks)
}
// Fetch updated stocks and send email.
func fetchAndSendUpdate() {
update := getUserStockPriceResult()
debugMessage("\n\nOutput result:\n", update)
if DEBUG {
return
}
mg := mailgun.NewMailgun(
MAILGUN_URL,
MAILGUN_PRIVATE_KEY,
MAILGUN_PUBLIC_KEY,
)
email := mg.NewMessage(
"ORCA PROJECT <orca@"+MAILGUN_URL+">",
"ORCA Daily Update",
update,
USER_EMAIL,
)
_, _, err := mg.Send(email)
checkError(err)
fmt.Println("[", update_time, "] Sent an update to", USER_EMAIL)
}
// Return a well formatted result of current market prices.
func getUserStockPriceResult() string {
investmentUSD := 0.0
investmentTHB := 0.0
returnInvestmentUSD := 0.0
returnInvestmentTHB := 0.0
result := "Stocks Update - " + time.Now().Format("Mon Jan 2") + "\n"
result += "-----------------------------\n"
result += fmt.Sprintf("%-*s %-*s\n", 10, "Symbol", 15, "M.Price")
quotes := fetchStocksUpdate()
for symbol, stock := range user_stocks {
price := quotes[symbol].LastPrice
result += fmt.Sprintf("%-*s %-*.2f\n", 10, symbol, 15, price)
for _, share := range stock.Shares {
if stock.IsThai {
investmentTHB += share.Price * float64(share.Amount)
returnInvestmentTHB += price * float64(share.Amount)
} else {
investmentUSD += share.Price * float64(share.Amount)
returnInvestmentUSD += price * float64(share.Amount)
}
}
}
if investmentUSD > 0.0 {
ac := accounting.Accounting{Symbol: "$", Precision: 2}
result += "-----------------------------\n"
result += "USD Summary\n"
result += "Total amount invested: " + ac.FormatMoney(investmentUSD) + "\n"
result += "Current investments: " + ac.FormatMoney(returnInvestmentUSD) + "\n"
result += "Gain/Loss = " + ac.FormatMoney(returnInvestmentUSD-investmentUSD) + "\n"
}
if investmentTHB > 0.0 {
ac := accounting.Accounting{Symbol: "฿", Precision: 2}
result += "-----------------------------\n"
result += "THB Summary\n"
result += "Total amount invested: " + ac.FormatMoney(investmentTHB) + "\n"
result += "Current investments: " + ac.FormatMoney(returnInvestmentTHB) + "\n"
result += "Gain/Loss = " + ac.FormatMoney(returnInvestmentTHB-investmentTHB) + "\n"
}
result += "-----------------------------"
return result
}
// Return market prices of user's stocks.
func fetchStocksUpdate() map[string]*Quote {
var symbols []string
for k, v := range user_stocks {
s := k
if v.IsThai {
s += QUOTE_TH_STOCK
}
symbols = append(symbols, s)
}
debugMessage("stocks to fetch:", symbols)
return fetchQuoteForStocks(strings.Join(symbols, ","))
}
// Return market price of stocks.
// Args: codes - list of comma separated symbols (eg. AAPL,MSFT)
func fetchQuoteForStocks(codes string) map[string]*Quote {
url := QUOTE_BASE_URL + codes + QUOTE_URL_SUFFIX
debugMessage("url:", url)
httpResponse := getHttpResponse(url)
debugMessage("http response:", string(httpResponse))
var r Response
err := json.Unmarshal(httpResponse, &r)
checkError(err)
quotes := make(map[string]*Quote)
for _, q := range r.Quotes {
// Remove country specific annotation.
symbol := strings.Split(q.Symbol, ".")[0]
quotes[symbol] = q
}
return quotes
}
// Return byte array response of an API call.
func getHttpResponse(url string) []byte {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
checkError(err)
req.Header.Set("User-Agent", QUOTE_USER_AGENT)
resp, err := client.Do(req)
checkError(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
checkError(err)
return body
}
func main() {
if len(os.Args) < 3 {
fmt.Printf("Usage: ./orca [csv_file] [update_time]\n")
os.Exit(1)
}
fmt.Println("[", time.Now().UTC().Format("15:04"), "] Started running..")
user_stocks = make(map[string]*UserStock)
populateUserStocks(os.Args[1])
update_time = os.Args[2]
gocron.Remove(fetchAndSendUpdate)
gocron.Clear()
if DEBUG {
fetchAndSendUpdate()
} else {
gocron.ChangeLoc(time.UTC)
gocron.Every(1).Day().At(update_time).Do(fetchAndSendUpdate)
<-gocron.Start()
}
}