-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (66 loc) · 1.27 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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/aimof/jaason"
)
func main() {
var cityID = os.Getenv("owmCityID")
var appID = os.Getenv("owmAppID")
if cityID == "" || appID == "" {
os.Exit(3)
}
resp, err := http.Get("https://api.openweathermap.org/data/2.5/weather?id=" + cityID + "&APPID=" + appID)
if err != nil {
log.Println(err)
os.Exit(4)
}
cp, err := current(resp.Body)
if err != nil {
log.Println(err)
os.Exit(5)
}
resp, err = http.Get("https://api.openweathermap.org/data/2.5/forecast?id=" + cityID + "&APPID=" + appID)
if err != nil {
log.Println(err)
os.Exit(6)
}
fp, err := forecast(resp.Body)
if err != nil {
log.Println(err)
os.Exit(7)
}
if fp+1 < cp {
fmt.Println("🤢👎")
} else {
fmt.Println("😀👍")
}
}
func current(r io.Reader) (int, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return 0, err
}
v, err := jaason.Parse(b)
if err != nil {
return 0, err
}
p, err := v.Get("main").Get("pressure").Int()
return p, err
}
func forecast(r io.Reader) (int, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return 0, err
}
v, err := jaason.Parse(b)
if err != nil {
return 0, err
}
p, err := v.Get("list").Get(1).Get("main").Get("pressure").Int()
return p, err
}