forked from genuinetools/weather
/
utilities.go
119 lines (98 loc) · 2.68 KB
/
utilities.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/http"
"strings"
"time"
"github.com/mitchellh/colorstring"
)
func createRequest(uri, method string, data interface{}) (req *http.Request, err error) {
// create json data
jsonByte, err := json.Marshal(data)
if err != nil {
return req, fmt.Errorf("Marshaling JSON for %s to %s failed: %s", method, uri, err.Error())
}
// send the request
req, err = http.NewRequest("POST", uri, bytes.NewReader(jsonByte))
if err != nil {
return req, fmt.Errorf("Creating the %s request to %s failed: %s", method, uri, err.Error())
}
req.Header.Set("Content-Type", "application/json")
return req, nil
}
func epochFormat(seconds int64) string {
epochTime := time.Unix(0, seconds*int64(time.Second))
return epochTime.Format("January 2 at 3:04pm MST")
}
func epochFormatDate(seconds int64) string {
epochTime := time.Unix(0, seconds*int64(time.Second))
return epochTime.Format("January 2")
}
func epochFormatTime(seconds int64) string {
epochTime := time.Unix(0, seconds*int64(time.Second))
return epochTime.Format("3:04pm MST")
}
func getIcon(icon string) (iconTxt string, err error) {
color := "blue"
switch icon {
case "clear-day":
color = "yellow"
case "clear-night":
color = "light_yellow"
case "snow":
color = "white"
case "wind":
color = "black"
case "partly-cloudy-day":
color = "yellow"
case "partly-cloudy-night":
color = "light_yellow"
case "thunderstorm":
color = "black"
case "tornado":
color = "black"
}
uri := "https://s3.j3ss.co/weather/icons/" + icon + ".txt"
resp, err := http.Get(uri)
if err != nil {
return iconTxt, fmt.Errorf("Requesting icon (%s) failed: %s", icon, err)
}
defer resp.Body.Close()
// decode the body
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return iconTxt, fmt.Errorf("Reading response body for icon (%s) failed: %s", icon, err)
}
iconTxt = string(out)
if strings.Contains(iconTxt, "<?xml") {
return "", fmt.Errorf("No icon found for %s.", icon)
}
return colorstring.Color("[" + color + "]" + iconTxt), nil
}
func getBearingDetails(degrees float64) (direction string) {
windDeg := (degrees + 11.25) / 22.5
directionInt := int(math.Abs(math.Remainder(windDeg, 16)))
if len(Directions) > directionInt && directionInt >= 0 {
direction = Directions[directionInt]
}
return direction
}
func printError(err error) {
fmt.Println(colorstring.Color("[red]" + err.Error()))
}
func Round(x float64, prec int) float64 {
var rounder float64
pow := math.Pow(10, float64(prec))
intermed := x * pow
_, frac := math.Modf(intermed)
if frac >= 0.5 {
rounder = math.Ceil(intermed)
} else {
rounder = math.Floor(intermed)
}
return rounder / pow
}