-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (85 loc) · 1.87 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"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"`
} `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"`
} `json:"condition"`
ChanceOfRain float64 `json:"chance_of_rain"`
} `json:"hour"`
} `json:"forecastday"`
} `json:"forecast"`
}
func main() {
fmt.Println("Starting")
q := "Lucknow"
if len(os.Args) >= 2 {
q = os.Args[1]
}
// Replace "your_api_key" with your actual Weather API key
res, err := http.Get("http://api.weatherapi.com/v1/forecast.json?key=6e368f8a457647baab612910240410&q=" + q + "&days=1&aqi=no&alerts=no")
if err != nil {
panic(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
panic("Weather API not available")
}
body, err := ioutil.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
fmt.Printf(
"%s, %s, %.0fC, %s\n",
location.Name,
location.Country,
current.TempC,
current.Condition.Text,
)
for _, hour := range hours {
date := time.Unix(hour.TimeEpoch, 0)
if date.Before(time.Now()) {
continue
}
message := fmt.Sprintf(
"%s - %.0fC, %.0f%%, %s\n",
date.Format("03:04 PM"),
hour.TempC,
hour.ChanceOfRain,
hour.Condition.Text,
)
if hour.ChanceOfRain < 40 {
fmt.Print(message)
} else {
color.Red(message)
}
}
}