Skip to main content

A thin Python Wrapper for the Pirate Weather API

Project description

Pirate Weather Wrapper

This is a wrapper for the Pirate Weather API. It allows you to get the weather for any location, now, in the past, or future.

The Basic Use section covers enough to get you going. I suggest also reading the source if you want to know more about how to use the wrapper or what its doing (it's very simple).

Installation

You should use pip to install python-pirateweather.

  • To install pip install python-pirateweather
  • To remove pip uninstall python-pirateweather

Simple!

Requirements

Basic Use

Although you don't need to know anything about the Pirate Weather API to use this module, their docs are available at https://pirateweather.net/en/latest/.

To use the wrapper:

	import pirateweather

	api_key = "YOUR API KEY"
	lat = -31.967819
	lng = 115.87718

	forecast = pirateweather.load_forecast(api_key, lat, lng)

The load_forecast() method has a few optional parameters. Providing your API key, a latitude and longitude are the only required parameters.

Use the forecast.DataBlockType() eg. currently(), daily(), hourly(), minutely() methods to load the data you are after.

These methods return a DataBlock. Except currently() which returns a DataPoint.

	byHour = forecast.hourly()
	print(byHour.summary)
	print(byHour.icon)

The .data attributes for each DataBlock is a list of DataPoint objects. This is where all the good data is :)

	for hourlyData in byHour.data:
		print(hourlyData.temperature)

Advanced

function pirateweather.load_forecast(key, latitude, longitude)

This makes an API request and returns a Forecast object (see below).

Parameters:

  • key - Your API key from https://pirateweather.net/en/latest/.
  • latitude - The latitude of the location for the forecast
  • longitude - The longitude of the location for the forecast
  • time - (optional) A datetime object for the forecast either in the past or future - see How Timezones Work below for the details on how timezones are handled in this library.
  • lang - (optional) A string of the desired language. See https://pirateweather.net/en/latest/API/#time-machine-request for supported languages.
  • units - (optional) A string of the preferred units of measurement, "us" is the default. "us","ca","uk","si" are also available. See the API Docs (https://pirateweather.net/en/latest/API/#units) for exactly what each unit means.
  • lazy - (optional) Defaults to false. If true the function will request the json data as it is needed. Results in more requests, but maybe a faster response time.
  • callback - (optional) Pass a function to be used as a callback. If used, load_forecast() will use an asynchronous HTTP call and will not return the forecast object directly, instead it will be passed to the callback function. Make sure it can accept it.

function pirateweather.manual(url)

This function allows manual creation of the URL for the Pirate Weather API request. This method won't be required often but can be used to take advantage of new or beta features of the API which this wrapper does not support yet. Returns a Forecast object (see below).

Parameters:

  • url - The URL which the wrapper will attempt build a forecast from.
  • callback - (optional) Pass a function to be used as a callback. If used, an asynchronous HTTP call will be used and pirateweather.manual will not return the forecast object directly, instead it will be passed to the callback function. Make sure it can accept it.

class pirateweather.models.Forecast

The Forecast object, it contains both weather data and the HTTP response from Pirate Weather

Attributes

  • response
  • http_headers
    • A dictionary of response headers. 'X-Forecast-API-Calls' might be of interest, it contains the number of API calls made by the given API key for the month.
  • json
    • A dictionary containing the json data returned from the API call.

Methods

  • currently()
    • Returns a PirateWeatherDataPoint object
  • minutely()
    • Returns a PirateWeatherDataBlock object
  • hourly()
    • Returns a PirateWeatherDataBlock object
  • daily()
    • Returns a PirateWeatherDataBlock object
  • update()
    • Refreshes the forecast data by making a new request.

class pirateweather.models.PirateWeatherDataBlock

Contains data about a forecast over time.

Attributes (descriptions taken from the pirateweather.net website)

  • summary
    • A human-readable text summary of this data block.
  • icon
    • A machine-readable text summary of this data block.
  • data
    • An array of PirateWeatherDataPoint objects (see below), ordered by time, which together describe the weather conditions at the requested location over time.

class pirateweather.models.PirateWeatherDataPoint

Contains data about a forecast at a particular time.

Data points have many attributes, but not all of them are always available. Some commonly used ones are:

Attributes (descriptions taken from the pirateweather.net website)

  • summary
    • A human-readable text summary of this data block.
  • icon
    • A machine-readable text summary of this data block.
  • time
    • The time at which this data point occurs.
  • temperature
    • (not defined on daily data points): A numerical value representing the temperature at the given time.
  • precipProbability
    • A numerical value between 0 and 1 (inclusive) representing the probability of precipitation occurring at the given time.

For a full list of PirateWeatherDataPoint attributes and attribute descriptions, take a look at the Pirate Weather data point documentation (https://pirateweather.net/en/latest/API/#data-point)


How Timezones Work

Requests with a naive datetime (no time zone specified) will correspond to the supplied time in the requesting location. If a timezone aware datetime object is supplied, the supplied time will be in the associated timezone.

Returned times eg the time parameter on the currently DataPoint are always in UTC time even if making a request with a timezone. If you want to manually convert to the locations local time, you can use the offset and timezone attributes of the forecast object.

Typically, would would want to do something like this:

  # Amsterdam
  lat  = 52.370235
  lng  = 4.903549
  current_time = datetime(2015, 2, 27, 6, 0, 0)
  forecast = pirateweather.load_forecast(api_key, lat, lng, time=current_time)

Be caerful, things can get confusing when doing something like the below. Given that I'm looking up the weather in Amsterdam (+2) while I'm in Perth, Australia (+8).

  # Amsterdam
  lat  = 52.370235
  lng  = 4.903549

  current_time = datetime.datetime.now()

  forecast = pirateweather.load_forecast(api_key, lat, lng, time=current_time)

The result is actually a request for the weather in the future in Amsterdam (by 6 hours). In addition, since all returned times are in UTC, it will report a time two hours behind the local time in Amsterdam.

If you're doing lots of queries in the past/future in different locations, the best approach is to consistently use UTC time. Keep in mind datetime.datetime.utcnow() is still a naive datetime. To use proper timezone aware datetime objects you will need to use a library like pytz <http://pytz.sourceforge.net/>_

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_pirateweather-1.0.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

python_pirateweather-1.0.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file python_pirateweather-1.0.1.tar.gz.

File metadata

  • Download URL: python_pirateweather-1.0.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for python_pirateweather-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8945cc6cb9122d4c29523ff0547fe300ced6a5e9f32838856b2b2e5e8c40e134
MD5 1f40082f5562ad099bf5dfead9f7ad80
BLAKE2b-256 5d807faa1f05255be4e04dfb72d0b128f995c2e93d93428a3b412dffd3ee7bcf

See more details on using hashes here.

File details

Details for the file python_pirateweather-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_pirateweather-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e3bb24f763a092b39568068653da36112e75f9d17db541c2b5f87daf4be5c648
MD5 0cb52b136eb49850d36063d9dc2abde5
BLAKE2b-256 0c66fa5b760104fc612df7ccf887de7e675652853171314b45b00eac5bd47250

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page