-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (126 loc) · 2.73 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/enescakir/emoji"
"github.com/fatih/color"
)
type Weather struct {
Location struct {
Name string `json:"name"`
Country string `json:"country"`
}`json:"location"`
Current struct {
TempC float64 `json:"temp_c"`
Condition struct {
Text string `json:"text"`
Icon string `json:"icon"`
}`json:"condition"`
}`json:"current"`
Forecast struct {
Forecastday []struct {
Hour []struct {
TimeEpoch int64 `json:"time_epoch"`
TempC float64 `json:"temp_c"`
Condition struct {
Text string `json:"text"`
Icon string `json:"icon"`
}
ChanceOfRain float64 `json:"chance_of_rain"`
}`json:"hour"`
}`json:"forecastday"`
}`json:"forecast"`
}
var emojis = map[string]emoji.Emoji{
"Clear":emoji.Sun,
"Sunny":emoji.Sun,
"Patchy rain":emoji.CloudWithRain,
"Partly cloudy":emoji.SunBehindCloud,
"Cloudy":emoji.Cloud,
"Patchy rain nearby":emoji.CloudWithRain,
"Rainy":emoji.CloudWithRain,
}
func IsSimpleCondition(category string) bool {
switch category {
case
"Sunny",
"Patchy rain",
"Partly cloudy",
"Cloudy",
"Rainy",
"Patchy rain nearby",
"Clear":
return true
}
return false
}
func main() {
city := "Algiers"
if len(os.Args) >=2 {
city = os.Args[1]
}
res, err := http.Get("https://api.weatherapi.com/v1/forecast.json?key=94474d04349f43008d395834240102&q="+city+"&days=1&aqi=no&alerts=no")
if err != nil {
panic(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
panic("Sorry, couldn't get weather for"+city)
}
body, err := io.ReadAll(res.Body)
if err!= nil {
panic(err)
}
var weather Weather
err = json.Unmarshal(body,&weather)
if err != nil {
panic(err)
}
location, current, hours := weather.Location, weather.Current, weather.Forecast.Forecastday[0].Hour
var emj emoji.Emoji
currentCondition := strings.Trim(current.Condition.Text, " ")
if IsSimpleCondition(currentCondition) {
emj = emojis[currentCondition]
}else {
emj = emoji.Cloud
}
fmt.Printf(
"%s, %s: %.0f C, %s %s\n",
location.Name,location.Country,
current.TempC,
currentCondition,
emj,
)
for _,hour := range hours {
date := time.Unix(hour.TimeEpoch,0)
// skip hours that are before the current hour
if date.Before(time.Now()){
continue
}
var emjhr emoji.Emoji
condition := strings.Trim(hour.Condition.Text, " ")
if IsSimpleCondition(condition ) {
emjhr = emojis[condition]
}else {
emjhr = emoji.Cloud
}
message := fmt.Sprintf(
"%s - %.0f C°, %.0f%%, %s %s\n",
date.Local().Format("15:04"),
hour.TempC,
hour.ChanceOfRain,
condition,
emjhr,
)
if(hour.ChanceOfRain >= 50){
color.Red(message)
}else{
fmt.Print(message)
}
}
}