Skip to content

Commit

Permalink
Improve provider example in README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ysavary committed Jul 7, 2024
1 parent 67ec690 commit 463b7cb
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 94 deletions.
87 changes: 1 addition & 86 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,92 +81,7 @@ Run the linter tools:
You know good quality weather stations that would be useful for many paraglider pilots or kitesurfers?

Awesome! Fork this repository and open a pull request with your new provider code. It's easy, look at the following
example:

providers/my_provider.py
```
import arrow
import requests
from winds_mobi_provider import Q_, Pressure, Provider, ProviderException, StationNames, StationStatus, ureg
class MyProvider(Provider):
provider_code = "my-provider"
provider_name = "my-provider.com"
def process_data(self):
self.log.info("Processing MyProvider data...")
try:
# data = requests.get(
# "https://api.my-provider.com/stations.json", timeout=(self.connect_timeout, self.read_timeout)
# ).json()
data = [
{
"id": "station-1",
"name": "Station 1",
"latitude": 46.713,
"longitude": 6.503,
"status": "ok",
"measures": [
{
"time": arrow.now().format("YYYY-MM-DD HH:mm:ssZZ"),
"windDirection": 180,
"windAverage": 10.5,
"windMaximum": 20.1,
"temperature": 25.7,
"pressure": 1013,
}
],
}
]
for station in data:
try:
winds_station = self.save_station(
provider_id=station["id"],
# Lets winds.mobi provide the full name (if found) with the help of Google Geocoding API
names=lambda names: StationNames(
short_name=station["name"], name=names.name or station["name"]
),
latitude=station["latitude"],
longitude=station["longitude"],
status=StationStatus.GREEN if station["status"] == "ok" else StationStatus.RED,
url=f"https://my-provider.com/stations/{station['id']}",
)
measure_key = arrow.get(station["measures"][0]["time"], "YYYY-MM-DD HH:mm:ssZZ").int_timestamp
measures_collection = self.measures_collection(winds_station["_id"])
if not self.has_measure(measures_collection, measure_key):
new_measure = self.create_measure(
for_station=winds_station,
_id=measure_key,
wind_direction=station["measures"][0]["windDirection"],
wind_average=Q_(station["measures"][0]["windAverage"], ureg.meter / ureg.second),
wind_maximum=Q_(station["measures"][0]["windMaximum"], ureg.meter / ureg.second),
temperature=Q_(station["measures"][0]["temperature"], ureg.degC),
pressure=Pressure(station["measures"][0]["pressure"], qnh=None, qff=None),
)
self.insert_new_measures(measures_collection, winds_station, [new_measure])
except ProviderException as e:
self.log.warning(f"Error while processing station '{station['id']}': {e}")
except Exception as e:
self.log.exception(f"Error while processing station '{station['id']}': {e}")
except Exception as e:
self.log.exception(f"Error while processing MyProvider: {e}")
self.log.info("...Done !")
def my_provider():
MyProvider().process_data()
if __name__ == "__main__":
my_provider()
```
example: [providers/example.py](providers/example.py)

## Licensing
winds.mobi is licensed under the AGPL License, Version 3.0. See [LICENSE.txt](LICENSE.txt)
99 changes: 99 additions & 0 deletions providers/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import arrow

from winds_mobi_provider import Q_, Pressure, Provider, ProviderException, StationNames, StationStatus, ureg


class MyProvider(Provider):
provider_code = "my-provider"
provider_name = "my-provider.com"
provider_url = "https://www.my-provider.com"

def process_data(self):
self.log.info("Processing MyProvider data...")
try:
# data = requests.get(
# "https://api.my-provider.com/stations.json", timeout=(self.connect_timeout, self.read_timeout)
# ).json()
# Result example:
data = [
{
"id": "station-1",
"name": "Station 1",
"latitude": 46.713,
"longitude": 6.503,
"status": "ok",
"measures": [
{
"time": arrow.now().format("YYYY-MM-DD HH:mm:ssZZ"),
"windDirection": 180,
"windAverage": 10.5,
"windMaximum": 20.1,
"temperature": 25.7,
"pressure": 1013,
}
],
}
]
for station in data:
try:
winds_station = self.save_station(
provider_id=station["id"],
# Let winds.mobi provide the full name (if found) with the help of Google Geocoding API
names=lambda names: StationNames(
short_name=station["name"], name=names.name or station["name"]
),
latitude=station["latitude"],
longitude=station["longitude"],
status=StationStatus.GREEN if station["status"] == "ok" else StationStatus.RED,
# If url is a dict, the keys must correspond to an ISO 639-1 language code. It also needs a
# "default" key, "english" if available. Here an example:
url={
"default": f"{self.provider_url}/en/stations/{station['id']}",
"en": f"{self.provider_url}/en/stations/{station['id']}",
"fr": f"{self.provider_url}/fr/stations/{station['id']}",
"de": f"{self.provider_url}/de/stations/{station['id']}",
},
)
station_id = winds_station["_id"]

measures_collection = self.measures_collection(station_id)
for measure in station["measures"]:
measure_key = arrow.get(measure["time"], "YYYY-MM-DD HH:mm:ssZZ").int_timestamp
if not self.has_measure(measures_collection, measure_key):
try:
new_measure = self.create_measure(
for_station=winds_station,
_id=measure_key,
wind_direction=measure["windDirection"],
wind_average=Q_(measure["windAverage"], ureg.meter / ureg.second),
wind_maximum=Q_(measure["windMaximum"], ureg.meter / ureg.second),
temperature=Q_(measure["temperature"], ureg.degC),
pressure=Pressure(measure["pressure"], qnh=None, qff=None),
)
self.insert_new_measures(measures_collection, winds_station, [new_measure])
except ProviderException as e:
self.log.warning(
f"Error while processing measure '{measure_key}' for station '{station_id}': {e}"
)
except Exception as e:
self.log.exception(
f"Error while processing measure '{measure_key}' for station '{station_id}': {e}"
)

except ProviderException as e:
self.log.warning(f"Error while processing station '{station['id']}': {e}")
except Exception as e:
self.log.exception(f"Error while processing station '{station['id']}': {e}")

except Exception as e:
self.log.exception(f"Error while processing MyProvider: {e}")

self.log.info("...Done !")


def my_provider():
MyProvider().process_data()


if __name__ == "__main__":
my_provider()
8 changes: 0 additions & 8 deletions winds_mobi_provider/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,6 @@ def save_station(
urls = {"default": self.provider_url}
elif isinstance(url, str):
urls = {"default": url}
# If url is a dict, the keys must correspond to an ISO 639-1 language code. It also needs a "default" key,
# "english" if available. Here an example:
# provider_urls = {
# "default": "https://my-provider.com/en/stations/{station["id"]}",
# "en": "https://my-provider.com/en/stations/{station["id"]}",
# "de": "https://my-provider.com/de/stations/{station["id"]}",
# "fr": "https://my-provider.com/fr/stations/{station["id"]}",
# }
elif isinstance(url, dict):
if "default" not in url:
raise ProviderException("No 'default' key in url")
Expand Down

0 comments on commit 463b7cb

Please sign in to comment.