-
Notifications
You must be signed in to change notification settings - Fork 10
/
goweather.go
95 lines (74 loc) · 1.77 KB
/
goweather.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
package main
import (
"flag"
"fmt"
"os"
"time"
f "github.com/kenhkelly/GoWeather/src/forecast"
i "github.com/kenhkelly/GoWeather/src/ipinfo"
"github.com/kenhkelly/GoWeather/src/objects"
)
var (
version string = "3.2"
forecast bool
)
func help() {
fmt.Printf("Usage: goweather [flags]\n")
flag.PrintDefaults()
}
func exitHelp() {
help()
os.Exit(3)
}
func init() {
flag.Usage = help
forecastPtr := flag.Bool("forecast", false, "Show 8 day forecast")
flag.Parse()
forecast = *forecastPtr
}
func main() {
fmt.Printf("\nGoWeather %s - @kenhkelly\n", version)
key := objects.GetApiKey()
location, err := i.GetLocation()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("\n Found you in: %s, %s\n", location.City, location.Region)
fc, err := f.GetForecast(key, location.Lat, location.Lng)
if err != nil {
fmt.Println("An error occured:", err)
return
}
cur := fc.Currently
dail := fc.Daily
if !forecast {
cur_time := time.Unix(cur.Time, 0).Format(time.RFC822)
cur_str := `
Current Weather: %s
Summary %s
Temperature %f
Humidity %f
WindSpeed %f
WindBearing %f
`
fmt.Printf(cur_str, cur_time, cur.Summary, cur.Temperature, cur.Humidity, cur.WindSpeed, cur.WindBearing)
} else {
var dail_str string
for _, v := range dail.Data {
d_time := time.Unix(v.Time, 0).Format(time.RFC822)
dail_for_str := `
Weather for %s
Summary %s
Temperature Min %f
Temperature Max %f
Humidity %f
WindSpeed %f
WindBearing %f
`
dail_str += fmt.Sprintf(dail_for_str, d_time, v.Summary, v.TemperatureMin, v.TemperatureMax, v.Humidity, v.WindSpeed, v.WindBearing)
}
fmt.Println(dail_str)
}
fmt.Println("")
}