-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (155 loc) · 4.47 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/joho/godotenv"
)
type Config struct {
ApiKey string `json:"apiKey"`
City string `json:"city"`
Units string `json:"units"`
}
type WeatherData struct {
Coord struct {
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
} `json:"coord"`
Weather []struct {
ID int `json:"id"`
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
} `json:"weather"`
Base string `json:"base"`
Main struct {
Temp float64 `json:"temp"`
FeelsLike float64 `json:"feels_like"`
TempMin float64 `json:"temp_min"`
TempMax float64 `json:"temp_max"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
} `json:"main"`
Visibility int `json:"visibility"`
Wind struct {
Speed float64 `json:"speed"`
Deg int `json:"deg"`
} `json:"wind"`
Clouds struct {
All int `json:"all"`
} `json:"clouds"`
Dt int `json:"dt"`
Sys struct {
Type int `json:"type"`
ID int `json:"id"`
Country string `json:"country"`
Sunrise int64 `json:"sunrise"`
Sunset int64 `json:"sunset"`
} `json:"sys"`
Timezone int `json:"timezone"`
ID int `json:"id"`
Name string `json:"name"`
Cod int `json:"cod"`
}
func main() {
const YELLOW = "\033[1;33m"
const GREY = "\033[1;37m"
const WHITE = "\033[1;37m"
const BLUE = "\033[1;34m"
const RESET = "\033[0m"
executablePath, err := os.Executable()
if err != nil {
log.Fatal(err)
}
executableDir := filepath.Dir(executablePath)
envFilePath := filepath.Join(executableDir, "..", ".env")
err = godotenv.Load(envFilePath)
if err != nil {
log.Fatal("Error loading .env file")
}
apiKey := os.Getenv("API_KEY")
city := os.Getenv("CITY")
units := os.Getenv("UNITS")
if apiKey == "" || city == "" || units == "" {
log.Fatal("Please set the API_KEY, CITY, and UNITS environment variables.")
}
var windSpeedUnits string
if units == "metric" {
windSpeedUnits = "m/s"
} else if units == "imperial" {
windSpeedUnits = "mph"
} else {
windSpeedUnits = "m/s"
}
resp, err := http.Get(fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&APPID=%s", city, units, apiKey))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var weatherData WeatherData
err = json.Unmarshal(body, &weatherData)
if err != nil {
log.Fatal(err)
}
if weatherData.Cod != 200 {
switch weatherData.Cod {
case 404:
fmt.Println("City not found")
os.Exit(1)
case 401:
fmt.Println("Invalid API key")
os.Exit(1)
default:
fmt.Println("ERROR")
os.Exit(1)
}
}
temp := strconv.FormatFloat(weatherData.Main.Temp, 'f', 0, 64)
tempMin := strconv.FormatFloat(weatherData.Main.TempMin, 'f', 0, 64)
tempMax := strconv.FormatFloat(weatherData.Main.TempMax, 'f', 0, 64)
windSpeed := strconv.FormatFloat(weatherData.Wind.Speed, 'f', 0, 64)
humidity := strconv.FormatFloat(float64(weatherData.Main.Humidity), 'f', 0, 64)
println()
switch weatherData.Weather[0].Main {
case "Clear":
fmt.Printf(" %s\\ /%s", YELLOW, RESET)
fmt.Printf(" %s.-.%s\n", YELLOW, RESET)
fmt.Printf(" %s‒ ( ) ‒%s", YELLOW, RESET)
fmt.Printf(" %s`-᾿%s\n", YELLOW, RESET)
fmt.Printf(" %s/ \\%s\n", YELLOW, RESET)
case "Clouds":
fmt.Printf(" %s.--.%s\n", GREY, RESET)
fmt.Printf(" %s.-( ).%s\n", GREY, RESET)
fmt.Printf(" %s(___.__)__)%s\n", GREY, RESET)
case "Rain":
fmt.Printf(" %s.--.%s\n", BLUE, RESET)
fmt.Printf(" %s.-( ).%s\n", BLUE, RESET)
fmt.Printf(" %s(___.__)__)%s\n", BLUE, RESET)
fmt.Printf(" %sʻ‚ʻ‚ʻ‚ʻ‚ʻ%s\n", BLUE, RESET)
case "Snow":
fmt.Printf(" %s.--.%s\n", WHITE, RESET)
fmt.Printf(" %s.-( ).%s\n", WHITE, RESET)
fmt.Printf(" %s(___.__)__)%s\n", WHITE, RESET)
fmt.Printf(" %s* * * * *%s\n", WHITE, RESET)
case "Thunderstorm":
fmt.Printf(" .--. Weather: storm\n")
fmt.Printf(" %s.-( ).%s Temperature: %s\n", BLUE, RESET, temp)
fmt.Printf(" %s(___.__)__)%s Min/Max: %s/%s\n", BLUE, RESET, tempMin, tempMax)
}
fmt.Printf("\n")
fmt.Printf(" Weather: clear\n")
fmt.Printf(" Temperature: %s\n", temp)
fmt.Printf(" Min/Max: %s/%s\n", tempMin, tempMax)
fmt.Printf(" Wind speed: %s %s\n", windSpeed, windSpeedUnits)
fmt.Printf(" Humidity: %s%%\n", humidity)
println()
}