odin-weather/main.odin
2025-02-27 19:00:11 +00:00

86 lines
2.3 KiB
Odin

package main
import curl "./libs/odin-curl/"
import "core:encoding/json"
import "core:flags"
import "core:fmt"
import "core:mem"
import vmem "core:mem/virtual"
import "core:os"
import "core:strings"
import "core:time"
HOST :: "https://api.openweathermap.org/data/2.5/weather?q="
main :: proc() {
arena: vmem.Arena
arena_err := vmem.arena_init_growing(&arena)
ensure(arena_err == nil) //make sure it doesnt fuck up
arena_alloc := vmem.arena_allocator(&arena)
defer vmem.arena_destroy(&arena) //clean up at end
city := "" //city name e.g. London
code := "" //code e.g. UK, JP, GE
api_key := "" //openweather api key
sb := strings.builder_make(allocator = arena_alloc)
buf1: [64]u8
buf2: [64]u8
// parse os.args in this order
// city -> code -> api_key
program_name := os.args[0]
if len(os.args) < 3 {
print_usage()
} else if len(os.args) > 4 {
print_usage()
}
city = os.args[1]
code = os.args[2]
api_key = os.args[3]
strings.write_string(&sb, HOST)
strings.write_string(&sb, city)
strings.write_string(&sb, ",")
strings.write_string(&sb, code)
strings.write_string(&sb, "&units=metric")
strings.write_string(&sb, "&appid=")
strings.write_string(&sb, api_key)
full_url := strings.to_string(sb)
data := curl_web(full_url)
weather: WeatherResponse
unmarshal_err := json.unmarshal_any(data, &weather)
if unmarshal_err != nil {
fmt.eprintln("Failed to unmarshal the file!")
return
}
weather_location := weather.name
weather_code := weather.country
weather_resp := weather.weather[0].main
weather_resp_detail := weather.weather[0].description
temperature := weather.temp
feels_like_temp := weather.feels_like
temp_min := weather.temp_min
temp_max := weather.temp_max
sunset_time := time.to_string_hms_12(time.unix(weather.sunset, 0), buf1[:])
sunrise_time := time.to_string_hms_12(time.unix(weather.sunrise, 0), buf2[:])
fmt.println("Location:", weather_location)
fmt.println("Country Code:", weather_code)
fmt.println("Weather:", weather_resp)
fmt.println("Weather Detail:", weather_resp_detail)
//fmt.println("Weather Icon:", weather_icon)
fmt.println("Temperature:", temperature)
fmt.println("Feels Like Temperature:", feels_like_temp)
fmt.println("Min Temperature:", temp_min)
fmt.println("Max Temperature:", temp_max)
fmt.println("Sunset Time:", sunset_time)
fmt.println("Sunrise Time:", sunrise_time)
}