-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweatherData.go
96 lines (69 loc) · 1.91 KB
/
weatherData.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
package main
import "fmt"
//https://openweathermap.org/current
//https://openweathermap.org/weather-conditions
const ZeroKelvin = -273.15
type WeatherMeta struct {
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
}
type Forecast struct {
Temp float32
Pressure float32
Humidity float32
TempMin float32 `json:"temp_min"`
TempMax float32 `json:"temp_max"`
}
type WindCast struct {
Speed float32 `json:"speed"` //meters per second
Deg float32 `json:"deg"` //meteorological degree
}
type CloudCast struct {
All int `json:"all"` // cloud percent 0..100
}
type RainCast struct {
OneHr float32 `json:"1h"` //volume in mm
ThreeHr float32 `json:"3h"`
}
type SnowCast struct {
OneHr float32 `json:"1h"` //volume in mm
ThreeHr float32 `json:"3h"`
}
type RegionCast struct {
Country string `json:"country"` //country code
Sunrise uint64 `json:"sunrise"`
Sunset uint64 `json:"sunset"`
}
type WeatherData struct {
Meta []WeatherMeta `json:"weather"`
Main Forecast `json:"main"`
Wind WindCast `json:"wind"`
Clouds CloudCast `json:"cloud"`
Rain RainCast `json:"rain"`
Snow RainCast `json:"snow"`
Sys RegionCast `json:"sys"`
Timestamp uint64 `json:"dt"`
Name string `json:"name"` //city name
}
//get icon path (must not be relative)
func (w WeatherData) icon() string {
icon_location := "icons/"
icon_label := w.Meta[0].Icon
icon_ext := ".png"
return fmt.Sprintf("%s%s%s", icon_location, icon_label, icon_ext)
}
//desc is the description of the weather
func (w WeatherData) desc() string {
return w.Meta[0].Description
}
//current temperature in Celsius
func (w WeatherData) tempC() string {
return fmt.Sprintf("%.2f °C", w.Main.Temp + ZeroKelvin)
}
func (w WeatherData) minTempC() string {
return fmt.Sprintf("%.1f °C", w.Main.TempMin + ZeroKelvin)
}
func (w WeatherData) maxTempC() string {
return fmt.Sprintf("%.1f °C", w.Main.TempMax + ZeroKelvin)
}