-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vijaya Prakash Masilamani
authored and
Vijaya Prakash Masilamani
committed
Oct 23, 2024
1 parent
4ab994c
commit 94707ed
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/saanvijay/weathernotify | ||
|
||
go 1.23.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |