openweather

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2020 License: MIT Imports: 5 Imported by: 2

README

Go Report Card Go Go Doc Release Gitpod Ready-to-Code

About

This Repo contains golang library to query OpenWetherMaps (http://openweathermap.org/) for weather information.

Install

go get github.com/EricNeid/openweather

Documentation

Is available on godoc:

https://godoc.org/github.com/EricNeid/go-openweather

Examples

Consuming the library:

import "github.com/EricNeid/openweather"

// create a query
q := openweather.NewQueryForCity(readAPIKey(), "Berlin,de")

// obtain data
resp, err := q.Weather()

// enjoy
fmt.Println(resp.Name) // Berlin
fmt.Println(resp.Weather[0].Description) // misc
fmt.Println(resp.Main.Temp) // 1

See the test files for more example.

A simple client for testing is also included:

go build cmd/openweatherclient
openweatherclient -key <OpenWeather API Key> -city Berlin,de

Documentation

Overview

Package openweather contains helper functions to query OpenWeatherMaps (http://openweathermap.org/) for weather information. Currently the current weather API (http://openweathermap.org/current) and the 5 days forecast API (http://openweathermap.org/forecast5) are supported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DailyForecast16URL

func DailyForecast16URL(q Query) string

DailyForecast16URL returns a matching url for the given query which can be used to obtain the 16 days forecast from openweathermap.org.

func DailyForecast5URL

func DailyForecast5URL(q Query) string

DailyForecast5URL returns a matching url for the given query which can be used to obtain the 5 days forecast from openweathermap.org.

func WeatherIconURL

func WeatherIconURL(iconID string) (url string)

WeatherIconURL returns an url to download matching icon for given weather id.

func WeatherURL

func WeatherURL(q Query) string

WeatherURL returns a matching url for the given query which can be used to obtain the current weather information from openweathermap.org.

Types

type CurrentWeather

type CurrentWeather 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"`
		Pressure int     `json:"pressure"`
		Humidity int     `json:"humidity"`
		TempMin  float64 `json:"temp_min"`
		TempMax  float64 `json:"temp_max"`
	} `json:"main"`
	Wind struct {
		Speed float64 `json:"speed"`
		Deg   int     `json:"deg"`
	} `json:"wind"`
	Clouds struct {
		All int `json:"all"`
	} `json:"clouds"`
	Rain struct {
		ThreeH int `json:"3h"`
	} `json:"rain"`
	Dt  int `json:"dt"`
	Sys struct {
		Type    int     `json:"type"`
		ID      int     `json:"id"`
		Message float64 `json:"message"`
		Country string  `json:"country"`
		Sunrise int     `json:"sunrise"`
		Sunset  int     `json:"sunset"`
	} `json:"sys"`
	ID   int    `json:"id"`
	Name string `json:"name"`
	Cod  int    `json:"cod"`
}

CurrentWeather represents unmarshalled data from openweathermap for the current weather API (http://openweathermap.org/current).

type DailyForecast16

type DailyForecast16 struct {
	Cod     string  `json:"cod"`
	Message float64 `json:"message"`
	City    struct {
		ID    int    `json:"id"`
		Name  string `json:"name"`
		Coord struct {
			Lon float64 `json:"lon"`
			Lat float64 `json:"lat"`
		} `json:"coord"`
		Country string `json:"country"`
	} `json:"city"`
	Cnt  int `json:"cnt"`
	List []struct {
		Dt   int `json:"dt"`
		Temp struct {
			Day   float64 `json:"day"`
			Min   float64 `json:"min"`
			Max   float64 `json:"max"`
			Night float64 `json:"night"`
			Eve   float64 `json:"eve"`
			Morn  float64 `json:"morn"`
		} `json:"temp"`
		Pressure float64 `json:"pressure"`
		Humidity int     `json:"humidity"`
		Weather  []struct {
			ID          int    `json:"id"`
			Main        string `json:"main"`
			Description string `json:"description"`
			Icon        string `json:"icon"`
		} `json:"weather"`
	} `json:"list"`
}

DailyForecast16 represents unmarshalled data from openweathermap for the 16 days forecast weather API (http://openweathermap.org/forecast16).

type DailyForecast5

type DailyForecast5 struct {
	Cod     string  `json:"cod"`
	Message float64 `json:"message"`
	City    struct {
		GeonameID  int     `json:"geoname_id"`
		Name       string  `json:"name"`
		Lat        float64 `json:"lat"`
		Lon        float64 `json:"lon"`
		Country    string  `json:"country"`
		Iso2       string  `json:"iso2"`
		Type       string  `json:"type"`
		Population int     `json:"population"`
	} `json:"city"`
	Cnt  int `json:"cnt"`
	List []struct {
		Dt   int `json:"dt"`
		Temp struct {
			Day   float64 `json:"day"`
			Min   float64 `json:"min"`
			Max   float64 `json:"max"`
			Night float64 `json:"night"`
			Eve   float64 `json:"eve"`
			Morn  float64 `json:"morn"`
		} `json:"temp"`
		Pressure float64 `json:"pressure"`
		Humidity int     `json:"humidity"`
		Weather  []struct {
			ID          int    `json:"id"`
			Main        string `json:"main"`
			Description string `json:"description"`
			Icon        string `json:"icon"`
		} `json:"weather"`
		Speed  float64 `json:"speed"`
		Deg    int     `json:"deg"`
		Clouds int     `json:"clouds"`
		Snow   float64 `json:"snow,omitempty"`
	} `json:"list"`
}

DailyForecast5 represents unmarshalled data from openweathermap for the 5 days forecast weather API (http://openweathermap.org/forecast5).

type Query

type Query struct {
	APIKey string
	Unit   string
	Query  string
	// contains filtered or unexported fields
}

Query represents a pending request to openweathermap.

func NewQueryForCity

func NewQueryForCity(apiKey string, city string, unit ...string) Query

NewQueryForCity creates a query for openweathermap from city name. The unit is optional and defaults to metric.

func NewQueryForID

func NewQueryForID(apiKey string, id string, unit ...string) Query

NewQueryForID creates a query for openweathermap from city id. The unit is optional and defaults to metric.

func NewQueryForLocation

func NewQueryForLocation(apiKey string, lat string, lon string, unit ...string) Query

NewQueryForLocation creates a query for openweathermap from latitude and longitude. The unit is optional and defaults to metric.

func NewQueryForZip

func NewQueryForZip(apiKey string, zip string, unit ...string) Query

NewQueryForZip creates a query for openweathermap from zip code. The unit is optional and defaults to metric.

func (Query) DailyForecast16

func (query Query) DailyForecast16() (*DailyForecast16, error)

DailyForecast16 downloads 16 days forecast data from openweathermap and return them as DailyForecast16. Warning: the 16 days forecast requires a paid account.

func (Query) DailyForecast16Raw

func (query Query) DailyForecast16Raw() (json string, err error)

DailyForecast16Raw downloads 16 days forecast data from openweathermap and return them as string. Warning: the 16 days forecast requires a paid account.

func (Query) DailyForecast5

func (query Query) DailyForecast5() (*DailyForecast5, error)

DailyForecast5 downloads 5 days forecast data from openweathermap and return them as DailyForecast5.

func (Query) DailyForecast5Raw

func (query Query) DailyForecast5Raw() (json string, err error)

DailyForecast5Raw downloads 5 days forecast data from openweathermap and return them as string.

func (Query) Weather

func (query Query) Weather() (*CurrentWeather, error)

Weather downloads current weather data from openweathermap and return them as WeatherData.

func (Query) WeatherRaw

func (query Query) WeatherRaw() (json string, err error)

WeatherRaw downloads current weather data from openweathermap and return them as string.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL