meteo

package module
v0.0.0-...-246a0e6 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2023 License: MIT Imports: 14 Imported by: 0

README

Go Reference Go Go Report Card GitHub GitHub go.mod Go version

meteo

Meteo is a Go client library for the weather and meteorological forecast from Yr.

Weather forecast from Yr, delivered by the Norwegian Meteorological Institute and NRK.

Usage

You must register your user agent string in the YR.NO service and your user name at the GeoNames.org.

package main

import (
	"fmt"

	"github.com/qba73/meteo"
)

func main() {
	// Export GEO_USERNAME Env Var (you registered at geonames.org)

	// Get current weather for given location.
	weather, err := meteo.GetWeather("Vilnius,LT")
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(weather)
	// Cloudy 4.2°C

	fmt.Println(weather.Summary)
	// cloudy

	fmt.Println(weather.Temp)
	// 4.2
}

The code sample below shows a basic example of how the meteo package can fetch weather statuses concurrently.

package main

import (
	"fmt"
	"time"

	"github.com/qba73/meteo"
)

func main() {
	start := time.Now()
	ch := make(chan string)

	locations := []string{
		"Vilnius,LT", "Dublin,IE", "London,UK", "Berlin,DE",
		"Belfast,UK", "Castlebar,IE", "Killarney,IE",
		"Warsaw,PL", "Lodz,PL", "Vienna,AT"}

	for _, loc := range locations {
		go getWeather(loc, ch)
	}

	for range locations {
		fmt.Println(<-ch)
	}

	fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
}

func getWeather(location string, ch chan<- string) {
	start := time.Now()

	weather, err := meteo.GetWeather(location)
	if err != nil {
		ch <- fmt.Sprint(err)
		return
	}
	sec := time.Since(start).Seconds()
	ch <- fmt.Sprintf("%.2fs Location: %s, Weather: %s", sec, location, weather)
}

Installation

$ go install github.com/qba73/meteo/cmd/meteo@latest

Documentation

Overview

Package meteo is a client library for the weather APIs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunForecastCLI

func RunForecastCLI() int

func RunWeatherCLI

func RunWeatherCLI() int

RunCLI is a main function that runs the cli machinery.

func WithBaseURL

func WithBaseURL(u string) option

func WithHTTPClient

func WithHTTPClient(hc *http.Client) option

func WithResolver

func WithResolver(rs func(context.Context, string) (Location, error)) option

func WithUserAgent

func WithUserAgent(ua string) option

Types

type Client

type Client struct {
	BaseURL    string
	HTTPClient *http.Client
	Resolve    func(context.Context, string) (Location, error)
	Debug      io.Writer
	// contains filtered or unexported fields
}

Client represents a weather client for the Norwegian Meteorological Institute.

func NewClient

func NewClient(opts ...option) (*Client, error)

NewClient knows how to construct a new default client.

func (*Client) GetWeather

func (c *Client) GetWeather(ctx context.Context, place string) (Weather, error)

GetWeather returns current weather for given place.

Place string should have format: "<place-name>,<country-code>", for example: "London,UK", "Dublin,IE", "Paris,FR", "Warsaw,PL".

func (*Client) GetWeatherForCoordinates

func (c *Client) GetWeatherForCoordinates(ctx context.Context, lat, long float64) (Weather, error)

GetWeatherForCoordinates returns current weather for a place with given coordinates (lat, long)

type CurrentWeather

type CurrentWeather struct {
	UpdatedAt          time.Time
	Time               time.Time
	PressureAtSeaLevel float64
	Temperature        float64
	Precipitation      float64
}

type Forecast

type Forecast struct {
	UpdatedAt time.Time
	Hourly    []HourlyForecast
}

type HourlyForecast

type HourlyForecast struct {
	Time                time.Time
	AirPressure         float64 // in hPa
	AirTemperature      float64 // in Celsius
	CloudAreaFraction   float64 // in "%""
	RelativeHumidity    float64 // in "%"
	WindFromDirection   float64 // in "degrees"
	WindSpeed           float64 // in "m/s"
	PrecipitationAmount float64 // in "mm"
	Summary             string
}

type Location

type Location struct {
	Lat  float64
	Long float64
}

Location represents geo coordinates.

type Weather

type Weather struct {
	Summary string
	Temp    float64
}

Weather represents weather conditions in a geographical region.

func GetWeather

func GetWeather(location string) (Weather, error)

GetWeather returns current weather for given place and country using default client.

func (Weather) String

func (w Weather) String() string

String implements stringer interface.

Directories

Path Synopsis
cmd
examples
concurrent
Example of running the program below
Example of running the program below

Jump to

Keyboard shortcuts

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