added some github imports and updated rdm

master
Simon Kellet 2 years ago
parent 0b6472fb4f
commit aeb64a0579
  1. 35
      README.md
  2. 52
      main.go

@ -0,0 +1,35 @@
# Go OpenWeather
This is a simple (but messy) Go project I am working on in my spare time!
It pulls JSON data from [here](https://openweathermap.org/) and uses 'encoding/json' to sort the data!
## Usage
You will need a OpenWeather API-Key. This can be done by opening a free account and creating a key!
```
./go-weather -h
```
Will give:
```
Usage of go-weather:
-api string
Enter Code e.g. uk, us etc... Default(none) (default "none")
-city string
Enter city e.g. London, Glasgow etc... (Default: London) (default "London")
-code string
Enter Code e.g. uk, us etc... (Default: uk) (default "uk")
```
```
./go-weather -api=API-KEY -city=Glasgow -code=uk
```
Will give:
```
02/17/2022 10:17
This will print text in bold white.
Location: Glasgow, GB
Weather: Rain (light rain)
Temp: 5.64°C (Feels like: 0.78°C)
(Min: 3.17°C, Max: 6.84°C)
Sunrise: 07:37 Sunset: 17:24
```

@ -9,6 +9,9 @@ import (
"net/http"
"os"
"time"
"github.com/fatih/color"
"github.com/sami2020pro/gmoji"
)
type WeatherAPI struct {
@ -82,19 +85,20 @@ func main() {
full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&appid=" + api_key + ""
//read webpage
html, _ := LoadWebPage(full)
html, _ := loadWebPage(full)
//Create file
CreateFile(string(html), false)
data, _ := LoadJson("weather.json")
createFile(string(html), false)
data, _ := loadJson("weather.json")
//print the weather!
PrintWeather(data)
printWeather(data)
color.Cyan("Prints text in cyan.")
color.Red("Hello!")
fmt.Printf("Hello %v\n", gmoji.Fire)
}
func LoadJson(file string) (WeatherAPI, error) {
func loadJson(file string) (WeatherAPI, error) {
var config WeatherAPI
configFile, err := os.Open(file)
if err != nil {
@ -105,7 +109,7 @@ func LoadJson(file string) (WeatherAPI, error) {
return config, err
}
func CreateFile(fileContents string, debugFlag bool) {
func createFile(fileContents string, debugFlag bool) {
file, err := os.Create("weather.json")
if err != nil {
log.Fatalf("failed creating file: %s", err)
@ -125,7 +129,7 @@ func CreateFile(fileContents string, debugFlag bool) {
}
}
func LoadWebPage(web_url string) ([]byte, error) {
func loadWebPage(web_url string) ([]byte, error) {
url := web_url
resp, err := http.Get(url)
//reads html as a slice of bytes
@ -137,11 +141,20 @@ func LoadWebPage(web_url string) ([]byte, error) {
}
func PrintWeather(data WeatherAPI) {
/*
func iconToEmoji(icon string) (emoji string) {
}
*/
func printWeather(data WeatherAPI) {
weatherData := data
whilte := color.New(color.FgWhite)
boldWhite := whilte.Add(color.Bold)
dt := time.Now()
fmt.Println(dt.Format("01/02/2006\t15:04"))
fmt.Println(dt.Format("01/02/2006 15:04"))
str_loc := weatherData.Name
str_code := weatherData.Sys.Country
@ -149,7 +162,18 @@ func PrintWeather(data WeatherAPI) {
str_weather_detail := weatherData.Weather[0].Description
str_temp := weatherData.Main.Temp
str_temp_fl := weatherData.Main.Feels_like
str_hum := weatherData.Main.Humidity
fmt.Printf("Location: %v, %v\nWeather: %v (%v)\nTemp: %v°C (Feels like: %v°C)\nHumidity: %v", str_loc, str_code, str_weather, str_weather_detail, str_temp, str_temp_fl, str_hum)
str_temp_min := weatherData.Main.Temp_min
str_temp_max := weatherData.Main.Temp_max
str_sunset := time.Unix(weatherData.Sys.Sunset, 0)
str_sunrise := time.Unix(weatherData.Sys.Sunrise, 0)
boldWhite.Printf("Location: %v, %v\n"+
"Weather: %v (%v)\n"+
"Temp: %v°C (Feels like: %v°C)\n(Min: %v°C, Max: %v°C)\n"+
"Sunrise: %v\t"+
"Sunset: %v\n",
str_loc, str_code,
str_weather, str_weather_detail,
str_temp, str_temp_fl, str_temp_min, str_temp_max,
str_sunrise.Format("15:04"), str_sunset.Format("15:04"))
}

Loading…
Cancel
Save