python-google-weather-api 0.0.7__py3-none-any.whl → 0.0.9__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.
@@ -21,9 +21,11 @@ from .model import (
21
21
  IceThickness,
22
22
  Interval,
23
23
  LocalizedText,
24
+ MinuteForecastResponse,
24
25
  MoonEvents,
25
26
  Precipitation,
26
27
  PrecipitationProbability,
28
+ PrecipitationSegment,
27
29
  QuantitativePrecipitationForecast,
28
30
  SunEvents,
29
31
  Temperature,
@@ -54,9 +56,11 @@ __all__ = [
54
56
  "IceThickness",
55
57
  "Interval",
56
58
  "LocalizedText",
59
+ "MinuteForecastResponse",
57
60
  "MoonEvents",
58
61
  "Precipitation",
59
62
  "PrecipitationProbability",
63
+ "PrecipitationSegment",
60
64
  "QuantitativePrecipitationForecast",
61
65
  "SunEvents",
62
66
  "Temperature",
google_weather_api/api.py CHANGED
@@ -17,11 +17,14 @@ from .model import (
17
17
  CurrentConditionsResponse,
18
18
  DailyForecastResponse,
19
19
  HourlyForecastResponse,
20
+ MinuteForecastResponse,
20
21
  )
21
22
 
22
23
  _LOGGER = logging.getLogger(__name__)
23
24
 
24
25
  _BASE_URL = "https://weather.googleapis.com/v1"
26
+ # See https://docs.cloud.google.com/docs/authentication/api-keys-use
27
+ _API_KEY_HEADER = "X-Goog-Api-Key"
25
28
  _USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"
26
29
 
27
30
 
@@ -45,20 +48,22 @@ class GoogleWeatherApi:
45
48
  self.referrer = referrer
46
49
  self.timeout = aiohttp.ClientTimeout(total=timeout)
47
50
 
48
- async def _async_get(self, endpoint: str, params: dict[str, Any]) -> dict[str, Any]:
51
+ async def _async_get(self, endpoint: str, params: dict[str, Any], *, include_language_code: bool = True) -> dict[str, Any]:
49
52
  """Perform a GET request."""
50
53
  url = f"{_BASE_URL}/{endpoint}"
51
- headers = {aiohttp.hdrs.USER_AGENT: _USER_AGENT}
54
+ # Send the API key in a header rather than the "key" query parameter so it
55
+ # never ends up in a URL, which aiohttp includes in exceptions such as
56
+ # ContentTypeError and which callers commonly log.
57
+ headers = {aiohttp.hdrs.USER_AGENT: _USER_AGENT, _API_KEY_HEADER: self.api_key}
52
58
  if self.referrer:
53
59
  headers[aiohttp.hdrs.REFERER] = self.referrer
54
60
  params = {
55
61
  **params,
56
- "language_code": self.language_code,
57
62
  "units_system": self.units_system,
58
63
  }
59
- # Log before adding the API key so it doesn't end up in debug logs.
64
+ if include_language_code:
65
+ params["language_code"] = self.language_code
60
66
  _LOGGER.debug("GET %s with params: %s", url, params)
61
- params["key"] = self.api_key
62
67
  try:
63
68
  async with self.session.get(
64
69
  url,
@@ -89,18 +94,27 @@ class GoogleWeatherApi:
89
94
  raise GoogleWeatherApiConnectionError(err) from err
90
95
 
91
96
  async def _async_get_all_pages(
92
- self, endpoint: str, params: dict[str, Any], records_key: str, limit: int
97
+ self,
98
+ endpoint: str,
99
+ params: dict[str, Any],
100
+ records_key: str,
101
+ limit: int,
102
+ *,
103
+ include_language_code: bool = True,
93
104
  ) -> dict[str, Any]:
94
105
  """Perform a GET request, following pagination until `limit` records are collected.
95
106
 
96
- The API caps the number of records it returns per page (24 for hourly forecasts),
97
- regardless of the requested page size, so a single request is not enough.
107
+ Some endpoints cap the number of records they return per page regardless of the
108
+ requested page size, for example 24 for hourly forecasts, so a single request is
109
+ not always enough.
98
110
  """
99
111
  params = {**params, "page_size": limit}
100
- data = await self._async_get(endpoint, params)
112
+ data = await self._async_get(endpoint, params, include_language_code=include_language_code)
101
113
  records: list[Any] = data.get(records_key, [])
102
114
  while len(records) < limit and (page_token := data.get("nextPageToken")):
103
- data = await self._async_get(endpoint, {**params, "page_token": page_token})
115
+ data = await self._async_get(
116
+ endpoint, {**params, "page_token": page_token}, include_language_code=include_language_code
117
+ )
104
118
  page_records: list[Any] = data.get(records_key, [])
105
119
  if not page_records:
106
120
  break
@@ -155,3 +169,26 @@ class GoogleWeatherApi:
155
169
  days,
156
170
  )
157
171
  return DailyForecastResponse.from_dict(data)
172
+
173
+ async def async_get_minute_forecast(
174
+ self, latitude: float, longitude: float, page_size: int = 360
175
+ ) -> MinuteForecastResponse:
176
+ """Fetch the precipitation nowcast for up to 6 hours.
177
+
178
+ Segments cover 2- or 15-minute intervals depending on the location. The endpoint
179
+ does not accept language_code (a reserved field in google.maps.weather.v1), so it
180
+ is omitted from these requests.
181
+
182
+ See https://developers.google.com/maps/documentation/weather/minute-forecast
183
+ """
184
+ data = await self._async_get_all_pages(
185
+ "forecast/minutes:lookup",
186
+ {
187
+ "location.latitude": latitude,
188
+ "location.longitude": longitude,
189
+ },
190
+ "segments",
191
+ page_size,
192
+ include_language_code=False,
193
+ )
194
+ return MinuteForecastResponse.from_dict(data)
@@ -639,3 +639,64 @@ class HourlyForecastResponse(BaseModel):
639
639
 
640
640
  next_page_token: str | None = field(default=None, metadata={"alias": "nextPageToken"})
641
641
  """The token to retrieve the next page."""
642
+
643
+
644
+ @dataclass
645
+ class PrecipitationSegment(BaseModel):
646
+ """Represents a segment of precipitation forecast data."""
647
+
648
+ class DominantPrecipitationType(ApiStrEnum):
649
+ """Represents the type of precipitation of a segment."""
650
+
651
+ DOMINANT_PRECIPITATION_TYPE_UNSPECIFIED = "DOMINANT_PRECIPITATION_TYPE_UNSPECIFIED"
652
+ NONE = "NONE"
653
+ RAIN = "RAIN"
654
+ SNOW = "SNOW"
655
+ HAIL = "HAIL"
656
+
657
+ class PrecipitationIntensity(ApiStrEnum):
658
+ """Represents the precipitation intensity of a segment."""
659
+
660
+ PRECIPITATION_INTENSITY_UNSPECIFIED = "PRECIPITATION_INTENSITY_UNSPECIFIED"
661
+ NO_INTENSITY = "NO_INTENSITY"
662
+ LIGHT = "LIGHT"
663
+ MID_LIGHT = "MID_LIGHT"
664
+ MODERATE = "MODERATE"
665
+ MID_MODERATE = "MID_MODERATE"
666
+ HEAVY = "HEAVY"
667
+ MID_HEAVY = "MID_HEAVY"
668
+
669
+ time_frame: Interval = field(metadata={"alias": "timeFrame"})
670
+ """The timeframe relevant to the segment."""
671
+
672
+ type: DominantPrecipitationType = DominantPrecipitationType.DOMINANT_PRECIPITATION_TYPE_UNSPECIFIED
673
+ """A code that indicates the type of precipitation."""
674
+
675
+ probability: int = 0
676
+ """The chance of precipitation (values from 0 to 100)."""
677
+
678
+ qpf: QuantitativePrecipitationForecast | None = None
679
+ """The amount of precipitation, measured as liquid water equivalent."""
680
+
681
+ snowfall_amount: QuantitativePrecipitationForecast | None = field(default=None, metadata={"alias": "snowfallAmount"})
682
+ """The amount of snow accumulation, measured as liquid water equivalent."""
683
+
684
+ intensity: PrecipitationIntensity = PrecipitationIntensity.PRECIPITATION_INTENSITY_UNSPECIFIED
685
+ """A code that indicates the precipitation intensity of the segment."""
686
+
687
+
688
+ @dataclass
689
+ class MinuteForecastResponse(BaseModel):
690
+ """Response model for the forecast.minutes.lookup method."""
691
+
692
+ overall_prediction_timeframe: Interval = field(metadata={"alias": "overallPredictionTimeframe"})
693
+ """The overall timeframe for the predictions."""
694
+
695
+ time_zone: TimeZone = field(metadata={"alias": "timeZone"})
696
+ """The time zone at the requested location."""
697
+
698
+ segments: list[PrecipitationSegment] = field(default_factory=list)
699
+ """The minute-level prediction segments."""
700
+
701
+ next_page_token: str | None = field(default=None, metadata={"alias": "nextPageToken"})
702
+ """The token to retrieve the next page."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-google-weather-api
3
- Version: 0.0.7
3
+ Version: 0.0.9
4
4
  Summary: A python client library for Google Weather API
5
5
  Author-email: tronikos <tronikos@gmail.com>
6
6
  License-Expression: Apache-2.0
@@ -0,0 +1,10 @@
1
+ google_weather_api/__init__.py,sha256=Su4PjNcbIJFYoFCEH4wwdtT-M2HSMtLNLvF-TWGHiNo,1601
2
+ google_weather_api/api.py,sha256=4_Y8TN6Mtrf4JDGoBOVm6W0uedxe5sPxMVx-ktD2kCI,7478
3
+ google_weather_api/exceptions.py,sha256=AHHzb-cbE0bgzp4PH2IC5s3jHAJHONbL9iw5b-_MvoU,534
4
+ google_weather_api/model.py,sha256=2Dx6VGh5Uaotb3McZSNs0BT6iJTrr5spYEUUC-IlXiA,24858
5
+ google_weather_api/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ python_google_weather_api-0.0.9.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
7
+ python_google_weather_api-0.0.9.dist-info/METADATA,sha256=SvCCnXAfToJfdwSvgBJ1D41y3lR3IsJGxgBwl_WJY5E,1412
8
+ python_google_weather_api-0.0.9.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
9
+ python_google_weather_api-0.0.9.dist-info/top_level.txt,sha256=ZkFbtKDHz3vUJhH6DGEPVLj3PPeNpR9-Wg0f4HqGD78,19
10
+ python_google_weather_api-0.0.9.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (84.0.0)
2
+ Generator: setuptools (84.2.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,10 +0,0 @@
1
- google_weather_api/__init__.py,sha256=mIsd7IvUJhucWozTmTKqDHT2wtGEPMG34Ux2Ve9q8QM,1489
2
- google_weather_api/api.py,sha256=7RzupmA5E0ft-Yn_ygGCIDxIz1bcB1iXuo0cbcU0vsY,5997
3
- google_weather_api/exceptions.py,sha256=AHHzb-cbE0bgzp4PH2IC5s3jHAJHONbL9iw5b-_MvoU,534
4
- google_weather_api/model.py,sha256=9dwgYTRLi37XPNDL0X5OZrBut6qBjxqO8BL4kFl-iL0,22500
5
- google_weather_api/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- python_google_weather_api-0.0.7.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
7
- python_google_weather_api-0.0.7.dist-info/METADATA,sha256=KJCoHyP64kq1mZdcH3r4_zUn955g3bbfwy3_b4MBwTw,1412
8
- python_google_weather_api-0.0.7.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
9
- python_google_weather_api-0.0.7.dist-info/top_level.txt,sha256=ZkFbtKDHz3vUJhH6DGEPVLj3PPeNpR9-Wg0f4HqGD78,19
10
- python_google_weather_api-0.0.7.dist-info/RECORD,,