Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 23 additions & 32 deletions tap_openweathermap/streams.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Stream type classes for tap-openweathermap."""

from datetime import datetime
from typing import Any, Dict, Optional
from typing import Optional
from urllib.parse import parse_qsl, urlsplit

from singer_sdk.typing import (
Expand Down Expand Up @@ -45,35 +45,7 @@ def post_process(self, row: dict, context: Optional[dict] = None) -> dict:
return row


class _CurrentWeatherStream(_SyncedAtStream):
"""Define user top items stream."""

def get_url_params(
self, context: Optional[dict], next_page_token: Optional[Any]
) -> Dict[str, Any]:
params = super().get_url_params(context, next_page_token)
params["q"] = self.config["current_weather_city_name"]
params["appid"] = self.config["api_key"]

return params


class _ForcastWeatherStream(_SyncedAtStream):
"""Define user top items stream."""

def get_url_params(
self, context: Optional[dict], next_page_token: Optional[Any]
) -> Dict[str, Any]:
params = super().get_url_params(context, next_page_token)
params["lat"] = self.config["forecast_weather_lattitude"]
params["lon"] = self.config["forecast_weather_longitude"]
params["appid"] = self.config["api_key"]

return params



class CurrentWeatherStream(_CurrentWeatherStream):
class CurrentWeatherStream(_SyncedAtStream):
"""Define custom stream."""
url_base = "https://api.openweathermap.org/data/2.5"
name = "current_weather_stream"
Expand All @@ -96,10 +68,21 @@ class CurrentWeatherStream(_CurrentWeatherStream):
Property("name", StringType),
Property("cod", NumberType),
).to_dict()


def get_url_params(self, context, next_page_token):
params = super().get_url_params(context, next_page_token)
params["appid"] = self.config["api_key"]

if city_name := self.config.get("current_weather_city_name"):
params["q"] = city_name
else:
params["lat"] = self.config["forecast_weather_lattitude"]
params["lon"] = self.config["forecast_weather_longitude"]

return params


class ForecastWeatherStream(_ForcastWeatherStream):
class ForecastWeatherStream(_SyncedAtStream):
"""Define custom stream."""
url_base = "https://api.openweathermap.org/data/3.0"
name = "forecast_stream"
Expand All @@ -117,6 +100,14 @@ class ForecastWeatherStream(_ForcastWeatherStream):
Property("daily", ForecastDailyObject)
).to_dict()

def get_url_params(self, context, next_page_token):
params = super().get_url_params(context, next_page_token)
params["appid"] = self.config["api_key"]
params["lat"] = self.config["forecast_weather_lattitude"]
params["lon"] = self.config["forecast_weather_longitude"]

return params

def response_error_message(self, response):
return "\n".join(
(
Expand Down
6 changes: 5 additions & 1 deletion tap_openweathermap/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TapOpenWeatherMap(Tap):
th.Property(
"current_weather_city_name",
th.StringType,
required=True,
required=False,
description="City name that you want to get weather for"
),
th.Property(
Expand All @@ -49,3 +49,7 @@ class TapOpenWeatherMap(Tap):
def discover_streams(self) -> List[Stream]:
"""Return a list of discovered streams."""
return [stream_class(tap=self) for stream_class in STREAM_TYPES]


if __name__ == "__main__":
TapOpenWeatherMap.cli()