Skip to content

Commit

Permalink
Weather API's initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Vijaya Prakash Masilamani authored and Vijaya Prakash Masilamani committed Oct 23, 2024
1 parent 4ab994c commit 94707ed
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/saanvijay/weathernotify

go 1.23.1
36 changes: 36 additions & 0 deletions weather-process/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"encoding/json"
"fmt"

weathersubs "github.com/saanvijay/weathernotify/weather-subs"
)

func main() {

location, err := weathersubs.GetCurrentLocation()
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}

fmt.Println(location)

grid, err := weathersubs.GetGridPoint(location)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
fmt.Println(string(grid))

forecast, err := weathersubs.GetForeCast(location)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
jsonData, _ := json.MarshalIndent(forecast.Properties.Periods, "", " ")

fmt.Println(string(jsonData))

}
41 changes: 41 additions & 0 deletions weather-subs/forcast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package weathersubs

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

type ForecastResponse struct {
Properties struct {
Periods []struct {
Name string `json:"name"`
Temperature int `json:"temperature"`
WindSpeed string `json:"windSpeed"`
ShortForecast string `json:"shortForecast"`
} `json:periods`
} `json:Properties`
}

func GetForeCast(location string) (*ForecastResponse, error) {

var forecastResponse ForecastResponse
forecastURL := fmt.Sprintf("https://api.weather.gov/gridpoints/LWX/%s/forecast", location)

response, err := http.Get(forecastURL)
if err != nil {
return nil, err
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &forecastResponse)
if err != nil {
return nil, err
}
return &forecastResponse, nil
}
35 changes: 35 additions & 0 deletions weather-subs/gridpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package weathersubs

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

type GridPointResponse struct {
Properties struct {
Forecast string `json:"json:"forecast"`
} `json:"Properties"`
}

func GetGridPoint(location string) (string, error) {
url := fmt.Sprintf("https://api.weather.gov/points/%s", location)

response, err := http.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", nil
}
var gridPointResponse GridPointResponse
err = json.Unmarshal(body, &gridPointResponse)
if err != nil {
return "", err
}
return gridPointResponse.Properties.Forecast, nil
}
64 changes: 64 additions & 0 deletions weather-subs/latlong.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package weathersubs

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)

type LocationResponse struct {
IP string `json:"ip"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
Loc string `json:"loc"`
Org string `json:"org"`
Timezone string `json:"timezone"`
}

func getproperLocationValues(location string) string {

location = strings.ReplaceAll(location, "-", " ")

parts := strings.Split(location, ", ")
var wholeNumbers []string

for _, part := range parts {
floatVal, err := strconv.ParseFloat(part, 64)
if err != nil {
fmt.Println("Error parsing float:", err)
continue
}

wholeNumber := int(floatVal)
wholeNumbers = append(wholeNumbers, strconv.Itoa(wholeNumber))
}

return strings.Join(wholeNumbers, ", ")

}

func GetCurrentLocation() (string, error) {
var location LocationResponse

locationURL := "https://ipinfo.io/json"
response, err := http.Get(locationURL)
if err != nil {
return "", err
}
defer response.Body.Close()

Body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
err = json.Unmarshal(Body, &location)
if err != nil {
return "", err
}

return getproperLocationValues(location.Loc), nil
}

0 comments on commit 94707ed

Please sign in to comment.