python-pirateweather 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ """Initializes the Pirate Weather library."""
2
+
3
+ # ruff: noqa: F401
4
+
5
+ from pirateweather.api import load_forecast, manual
pirateweather/api.py ADDED
@@ -0,0 +1,78 @@
1
+ """Fetches data from the Pirate Weather API."""
2
+
3
+ import threading
4
+
5
+ import requests
6
+
7
+ from pirateweather.models import Forecast
8
+
9
+
10
+ def load_forecast(
11
+ key, lat, lng, time=None, units="us", lang="en", lazy=False, callback=None
12
+ ):
13
+ """Build the request url and loads some or all of the needed json depending on lazy is True.
14
+
15
+ inLat: The latitude of the forecast
16
+ inLong: The longitude of the forecast
17
+ time: A datetime.datetime object representing the desired time of
18
+ the forecast. If no timezone is present, the API assumes local
19
+ time at the provided latitude and longitude.
20
+ units: A string of the preferred units of measurement, "us" id
21
+ default. also ca,uk,si is available
22
+ lang: Return summary properties in the desired language
23
+ lazy: Defaults to false. The function will only request the json
24
+ data as it is needed. Results in more requests, but
25
+ probably a faster response time (I haven't checked)
26
+ """
27
+
28
+ if time is None:
29
+ url = (
30
+ f"https://api.pirateweather.net/forecast/{key}/{lat},{lng}"
31
+ f"?units={units}&lang={lang}"
32
+ )
33
+ else:
34
+ url_time = time.replace(
35
+ microsecond=0
36
+ ).isoformat() # API returns 400 for microseconds
37
+ url = (
38
+ f"https://timemachine.pirateweather.net/forecast/{key}/{lat},{lng},{url_time}"
39
+ f"?units={units}&lang={lang}"
40
+ )
41
+
42
+ if lazy is True:
43
+ baseURL = "{}&exclude={}".format(
44
+ url,
45
+ "minutely,currently,hourly," "daily,alerts,flags",
46
+ )
47
+ else:
48
+ baseURL = url
49
+
50
+ return manual(baseURL, callback=callback)
51
+
52
+
53
+ def manual(requestURL, callback=None):
54
+ """Manually construct the URL for an API call used by load_forecast OR by users."""
55
+
56
+ if callback is None:
57
+ return get_forecast(requestURL)
58
+ thread = threading.Thread(target=load_async, args=(requestURL, callback))
59
+ thread.start()
60
+ return None
61
+
62
+
63
+ def get_forecast(requestURL):
64
+ """Get the forecast from the Pirate Weather API."""
65
+
66
+ pirateweather_reponse = requests.get(requestURL, timeout=60)
67
+ pirateweather_reponse.raise_for_status()
68
+
69
+ json = pirateweather_reponse.json()
70
+ headers = pirateweather_reponse.headers
71
+
72
+ return Forecast(json, pirateweather_reponse, headers)
73
+
74
+
75
+ def load_async(url, callback):
76
+ """Get the forecast from the Pirate Weather API asynchronously."""
77
+
78
+ callback(get_forecast(url))
@@ -0,0 +1,159 @@
1
+ """Models used in the Pirate Weather library."""
2
+
3
+ import datetime
4
+
5
+ import requests
6
+
7
+ from pirateweather.utils import PropertyUnavailable, UnicodeMixin
8
+
9
+
10
+ class Forecast(UnicodeMixin):
11
+ """Represent the forecast data and provide methods to access weather blocks."""
12
+
13
+ def __init__(self, data, response, headers):
14
+ """Initialize the Forecast with data, HTTP response, and headers."""
15
+ self.response = response
16
+ self.http_headers = headers
17
+ self.json = data
18
+
19
+ self._alerts = []
20
+ for alertJSON in self.json.get("alerts", []):
21
+ self._alerts.append(Alert(alertJSON))
22
+
23
+ def update(self):
24
+ """Update the forecast data by making a new request to the same URL."""
25
+ r = requests.get(self.response.url)
26
+ self.json = r.json()
27
+ self.response = r
28
+
29
+ def currently(self):
30
+ """Return the current weather data block."""
31
+ return self._pirateweather_data("currently")
32
+
33
+ def minutely(self):
34
+ """Return the minutely weather data block."""
35
+ return self._pirateweather_data("minutely")
36
+
37
+ def hourly(self):
38
+ """Return the hourly weather data block."""
39
+ return self._pirateweather_data("hourly")
40
+
41
+ def daily(self):
42
+ """Return the daily weather data block."""
43
+ return self._pirateweather_data("daily")
44
+
45
+ def offset(self):
46
+ """Return the time zone offset for the forecast location."""
47
+ return self.json["offset"]
48
+
49
+ def alerts(self):
50
+ """Return the list of alerts issued for this forecast."""
51
+ return self._alerts
52
+
53
+ def _pirateweather_data(self, key):
54
+ """Fetch and return specific weather data (currently, minutely, hourly, daily)."""
55
+ keys = ["minutely", "currently", "hourly", "daily"]
56
+ try:
57
+ if key not in self.json:
58
+ keys.remove(key)
59
+ url = "{}&exclude={}{}".format(
60
+ self.response.url.split("&")[0],
61
+ ",".join(keys),
62
+ ",alerts,flags",
63
+ )
64
+
65
+ response = requests.get(url).json()
66
+ self.json[key] = response[key]
67
+
68
+ if key == "currently":
69
+ return PirateWeatherDataPoint(self.json[key])
70
+ return PirateWeatherDataBlock(self.json[key])
71
+ except KeyError:
72
+ if key == "currently":
73
+ return PirateWeatherDataPoint()
74
+ return PirateWeatherDataBlock()
75
+
76
+
77
+ class PirateWeatherDataBlock(UnicodeMixin):
78
+ """Represent a block of weather data such as minutely, hourly, or daily summaries."""
79
+
80
+ def __init__(self, d=None):
81
+ """Initialize the data block with summary and icon information."""
82
+ d = d or {}
83
+ self.summary = d.get("summary")
84
+ self.icon = d.get("icon")
85
+ self.data = [
86
+ PirateWeatherDataPoint(datapoint) for datapoint in d.get("data", [])
87
+ ]
88
+
89
+ def __unicode__(self):
90
+ """Return a string representation of the data block."""
91
+ return (
92
+ "<PirateWeatherDataBlock instance: "
93
+ "%s with %d PirateWeatherDataPoints>"
94
+ % (
95
+ self.summary,
96
+ len(self.data),
97
+ )
98
+ )
99
+
100
+
101
+ class PirateWeatherDataPoint(UnicodeMixin):
102
+ """Represent a single data point in a weather forecast, such as an hourly or daily data point."""
103
+
104
+ def __init__(self, d={}):
105
+ """Initialize the data point with timestamp and weather information."""
106
+ self.d = d
107
+
108
+ try:
109
+ self.time = datetime.datetime.fromtimestamp(int(d["time"]))
110
+ self.utime = d["time"]
111
+ except KeyError:
112
+ pass
113
+
114
+ try:
115
+ sr_time = int(d["sunriseTime"])
116
+ self.sunriseTime = datetime.datetime.fromtimestamp(sr_time)
117
+ except KeyError:
118
+ self.sunriseTime = None
119
+
120
+ try:
121
+ ss_time = int(d["sunsetTime"])
122
+ self.sunsetTime = datetime.datetime.fromtimestamp(ss_time)
123
+ except KeyError:
124
+ self.sunsetTime = None
125
+
126
+ def __getattr__(self, name):
127
+ """Return the weather property dynamically or raise PropertyUnavailable if missing."""
128
+ try:
129
+ return self.d[name]
130
+ except KeyError as err:
131
+ raise PropertyUnavailable(
132
+ f"Property '{name}' is not valid"
133
+ " or is not available for this forecast"
134
+ ) from err
135
+
136
+ def __unicode__(self):
137
+ """Return a string representation of the data point."""
138
+ return "<PirateWeatherDataPoint instance: " f"{self.summary} at {self.time}>"
139
+
140
+
141
+ class Alert(UnicodeMixin):
142
+ """Represent a weather alert, such as a storm warning or flood alert."""
143
+
144
+ def __init__(self, json):
145
+ """Initialize the alert with the raw JSON data."""
146
+ self.json = json
147
+
148
+ def __getattr__(self, name):
149
+ """Return the alert property dynamically or raise PropertyUnavailable if missing."""
150
+ try:
151
+ return self.json[name]
152
+ except KeyError as err:
153
+ raise PropertyUnavailable(
154
+ f"Property '{name}' is not valid" " or is not available for this alert"
155
+ ) from err
156
+
157
+ def __unicode__(self):
158
+ """Return a string representation of the alert."""
159
+ return f"<Alert instance: {self.title} at {self.time}>"
pirateweather/utils.py ADDED
@@ -0,0 +1,13 @@
1
+ """Utilities for the Pirate Weather library."""
2
+
3
+
4
+ class UnicodeMixin:
5
+ """Mixin class to handle defining the proper __str__/__unicode__ methods in Python 3."""
6
+
7
+ def __str__(self):
8
+ """Return the unicode representation of the object for Python 3 compatibility."""
9
+ return self.__unicode__()
10
+
11
+
12
+ class PropertyUnavailable(AttributeError):
13
+ """Raise when a requested property is unavailable in the forecast data."""
@@ -0,0 +1,13 @@
1
+ ## License (BSD 2-clause)
2
+
3
+ Copyright (c) 2013, Ze'ev Gilovitz and contributors
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11
+
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,200 @@
1
+ Metadata-Version: 2.1
2
+ Name: python-pirateweather
3
+ Version: 1.0.0
4
+ Summary: A thin Python Wrapper for the Pirate Weather API
5
+ Home-page: https://github.com/cloneofghosts/python-pirate-weather
6
+ Author: cloneofghosts
7
+ License: BSD 2-clause
8
+ Keywords: weather API wrapper pirateweather location
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE.txt
11
+ Requires-Dist: requests ==2.32.3
12
+
13
+ # Pirate Weather Wrapper
14
+
15
+
16
+ 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.
17
+
18
+ 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).
19
+
20
+
21
+ ## Installation
22
+
23
+ You should use pip to install python-pirateweather.
24
+
25
+ * To install pip install python-pirateweather
26
+ * To remove pip uninstall python-pirateweather
27
+
28
+ Simple!
29
+
30
+ ## Requirements
31
+
32
+ - You need an API key to use it (https://pirateweather.net/en/latest/). Don't worry a key is free.
33
+
34
+
35
+ ## Basic Use
36
+
37
+ 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/.
38
+
39
+ To use the wrapper:
40
+
41
+ ```python
42
+ import pirateweather
43
+
44
+ api_key = "YOUR API KEY"
45
+ lat = -31.967819
46
+ lng = 115.87718
47
+
48
+ forecast = pirateweather.load_forecast(api_key, lat, lng)
49
+ ```
50
+
51
+ The ``load_forecast()`` method has a few optional parameters. Providing your API key, a latitude and longitude are the only required parameters.
52
+
53
+ Use the ``forecast.DataBlockType()`` eg. ``currently()``, ``daily()``, ``hourly()``, ``minutely()`` methods to load the data you are after.
54
+
55
+ These methods return a DataBlock. Except ``currently()`` which returns a DataPoint.
56
+
57
+ ```python
58
+ byHour = forecast.hourly()
59
+ print(byHour.summary)
60
+ print(byHour.icon)
61
+ ```
62
+
63
+ The .data attributes for each DataBlock is a list of DataPoint objects. This is where all the good data is :)
64
+
65
+ ```python
66
+ for hourlyData in byHour.data:
67
+ print(hourlyData.temperature)
68
+ ```
69
+
70
+
71
+ ## Advanced
72
+
73
+ *function* pirateweather.load_forecast(key, latitude, longitude)
74
+ ---------------------------------------------------
75
+
76
+ This makes an API request and returns a **Forecast** object (see below).
77
+
78
+ Parameters:
79
+ - **key** - Your API key from https://pirateweather.net/en/latest/.
80
+ - **latitude** - The latitude of the location for the forecast
81
+ - **longitude** - The longitude of the location for the forecast
82
+ - **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.
83
+ - **lang** - (optional) A string of the desired language. See https://pirateweather.net/en/latest/API/#time-machine-request for supported languages.
84
+ - **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.
85
+ - **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.
86
+ - **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.
87
+
88
+ ----------------------------------------------------
89
+
90
+
91
+ *function* pirateweather.manual(url)
92
+ ----------------------------------------------------
93
+ 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).
94
+
95
+ Parameters:
96
+ - **url** - The URL which the wrapper will attempt build a forecast from.
97
+ - **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.
98
+
99
+ ----------------------------------------------------
100
+
101
+
102
+ *class* pirateweather.models.Forecast
103
+ ------------------------------------
104
+
105
+ The **Forecast** object, it contains both weather data and the HTTP response from Pirate Weather
106
+
107
+ **Attributes**
108
+ - **response**
109
+ - The Response object returned from requests request.get() method. See https://requests.readthedocs.org/en/latest/api/#requests.Response
110
+ - **http_headers**
111
+ - 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.
112
+ - **json**
113
+ - A dictionary containing the json data returned from the API call.
114
+
115
+ **Methods**
116
+ - **currently()**
117
+ - Returns a PirateWeatherDataPoint object
118
+ - **minutely()**
119
+ - Returns a PirateWeatherDataBlock object
120
+ - **hourly()**
121
+ - Returns a PirateWeatherDataBlock object
122
+ - **daily()**
123
+ - Returns a PirateWeatherDataBlock object
124
+ - **update()**
125
+ - Refreshes the forecast data by making a new request.
126
+
127
+ ----------------------------------------------------
128
+
129
+
130
+ *class* pirateweather.models.PirateWeatherDataBlock
131
+ ---------------------------------------------
132
+
133
+ Contains data about a forecast over time.
134
+
135
+ **Attributes** *(descriptions taken from the pirateweather.net website)*
136
+ - **summary**
137
+ - A human-readable text summary of this data block.
138
+ - **icon**
139
+ - A machine-readable text summary of this data block.
140
+ - **data**
141
+ - An array of **PirateWeatherDataPoint** objects (see below), ordered by time, which together describe the weather conditions at the requested location over time.
142
+
143
+ ----------------------------------------------------
144
+
145
+
146
+ *class* pirateweather.models.PirateWeatherDataPoint
147
+ ---------------------------------------------
148
+
149
+ Contains data about a forecast at a particular time.
150
+
151
+ Data points have many attributes, but **not all of them are always available**. Some commonly used ones are:
152
+
153
+ **Attributes** *(descriptions taken from the pirateweather.net website)*
154
+ - **summary**
155
+ - A human-readable text summary of this data block.
156
+ - **icon**
157
+ - A machine-readable text summary of this data block.
158
+ - **time**
159
+ - The time at which this data point occurs.
160
+ - **temperature**
161
+ - (not defined on daily data points): A numerical value representing the temperature at the given time.
162
+ - **precipProbability**
163
+ - A numerical value between 0 and 1 (inclusive) representing the probability of precipitation occurring at the given time.
164
+
165
+ 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)
166
+
167
+ ----------------------------------------------------
168
+
169
+
170
+ How Timezones Work
171
+ ------------------
172
+ 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.
173
+
174
+ 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.
175
+
176
+ Typically, would would want to do something like this:
177
+
178
+ ```python
179
+ # Amsterdam
180
+ lat = 52.370235
181
+ lng = 4.903549
182
+ current_time = datetime(2015, 2, 27, 6, 0, 0)
183
+ forecast = pirateweather.load_forecast(api_key, lat, lng, time=current_time)
184
+ ```
185
+
186
+ 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).
187
+
188
+ ```python
189
+ # Amsterdam
190
+ lat = 52.370235
191
+ lng = 4.903549
192
+
193
+ current_time = datetime.datetime.now()
194
+
195
+ forecast = pirateweather.load_forecast(api_key, lat, lng, time=current_time)
196
+ ```
197
+
198
+ 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.
199
+
200
+ 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/>`_
@@ -0,0 +1,9 @@
1
+ pirateweather/__init__.py,sha256=raRpS-b_ltdjbUjuLU1UEFP9xAWXHKv82cw9BTf6tBo,119
2
+ pirateweather/api.py,sha256=vqGwiquAcUVrMNCqD7018-4b53ZktwXI-bAFlbkx0H8,2467
3
+ pirateweather/models.py,sha256=RsSySdcTzoXoJhzxt5WP1JeeuMgqbzV3kYdx5yWg1Wo,5312
4
+ pirateweather/utils.py,sha256=qiCWsEU0GFEpkZBxaiSGWXn4j6nNMIZpA9ik3sNXWs4,435
5
+ python_pirateweather-1.0.0.dist-info/LICENSE.txt,sha256=oHMuaCnD9D2jpuUXmJRy6U0xSp62bBXcikUxHSbumQU,1336
6
+ python_pirateweather-1.0.0.dist-info/METADATA,sha256=qyfT8qONh0e_OeZT2J-9k39xOtpqRZUeDU9r-gujuDI,8533
7
+ python_pirateweather-1.0.0.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
8
+ python_pirateweather-1.0.0.dist-info/top_level.txt,sha256=EtbXy9nj80X06RItu2tVG0VaMXMeqF0-yns1eRBNLyM,14
9
+ python_pirateweather-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.1.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ pirateweather