-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
112 lines (89 loc) · 2.78 KB
/
functions.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 (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/mail"
"net/smtp"
"os"
"github.com/gincorp/gin/taskmanager"
)
type financeitem struct {
Name, Price string
}
func apiCall(url string) (d map[string]interface{}, err error) {
var resp *http.Response
if resp, err = http.Get(url); err != nil {
return
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
defer resp.Body.Close()
d = make(map[string]interface{})
json.Unmarshal(buf.Bytes(), &d)
return
}
func getCurrencyPrices(jn taskmanager.JobNotification) (output map[string]interface{}, err error) {
respData, err := apiCall("http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json")
if err != nil {
return
}
output = make(map[string]interface{})
prices := []financeitem{}
// Somewhere, and I suspect in java world, there's a fucking awful library turning xml into json
// by trying to map xml classes/types to a json object. It means stupid shit like this format.
//
// Seriously: fuck anybody who does this.
data := respData["list"].(map[string]interface{}) // ITS THE ONLY FUCKING KEY IN THE FUCKING OBJECT
for _, resource := range data["resources"].([]interface{}) {
actualResource := resource.(map[string]interface{})["resource"].(map[string]interface{}) // WANKERS
fields := actualResource["fields"].(map[string]interface{})
switch fields["name"].(string) {
case "USD/GBP":
prices = append(prices, financeitem{"$/£", fields["price"].(string)})
case "USD/EUR":
prices = append(prices, financeitem{"$/€", fields["price"].(string)})
case "GOLD 1 OZ":
prices = append(prices, financeitem{"Gold per ounce", fields["price"].(string)})
}
}
output["prices"] = prices
return
}
func sendEmail(jn taskmanager.JobNotification) (output map[string]interface{}, err error) {
// Hat tip: https://gist.github.com/andelf/5004821
username := os.Getenv("MAIL_USERNAME")
password := os.Getenv("MAIL_PASSWORD")
mailhost := jn.Context["host"]
mailport := jn.Context["port"]
auth := smtp.PlainAuth("",
username,
password,
mailhost,
)
from := mail.Address{Name: "gin", Address: jn.Context["from"]}
to := mail.Address{Address: jn.Context["to"]}
header := make(map[string]string)
header["From"] = from.String()
header["To"] = to.String()
header["Subject"] = jn.Context["subject"]
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/plain; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(jn.Context["body"]))
err = smtp.SendMail(
mailhost+":"+mailport,
auth,
from.Address,
[]string{to.Address},
[]byte(message),
)
output = make(map[string]interface{})
return
}