diff options
| author | root <root@turin.home> | 2022-01-15 14:46:26 +0000 |
|---|---|---|
| committer | root <root@turin.home> | 2022-01-15 14:46:26 +0000 |
| commit | f315adea703193deb7419355dc084e903dbfde1e (patch) | |
| tree | 64f0855bd91bf2c8b7cc4d9c9dc5a80a5c448015 /weather.py | |
| parent | cf70ae6ded332729f0dc2833d6680d7dac8072db (diff) | |
Development
Diffstat (limited to 'weather.py')
| -rw-r--r-- | weather.py | 40 |
1 files changed, 38 insertions, 2 deletions
@@ -1,9 +1,45 @@ -class weather: +from abc import ABC, abstractmethod +import requests + +# Abstract class for weather +class weather(ABC): def __init__(self): self.temp = 0 + + @abstractmethod def get_temp(self): - #update temp + # update temp pass + def temperature(self): self.get_temp() return self.temp + + +# inheriting weather class +class weather_met_no(weather): + def __init__(self): + # constant for location in (latitude, longitude form + self.LOCATION = (51.52866, -0.35508) + # constant for api request when getting data + # uses Norweigen Meterological Institute api + # -- todo : reference + self.CALL = "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat={}&lon={}".format( + self.LOCATION[0], self.LOCATION[1] + ) + # acceptable header for the API + # https://stackoverflow.com/questions/10606133/sending-user-agent-using-requests-library-in-python + # https://api.met.no/doc/FAQ + self.HEADERS = {"User-Agent": "TopologicalMap"} + super.init(self) + + def get_temperature(self): + # https://docs.python-requests.org/en/latest/ + try: + data = requests.get( + self.CALL, headers=self.HEADERS + ).json()['properties']['timeseries'][0]['data'] + return data['instant']['details']['air_temperature'] + # data['next_1_hours']['summary']['symbol_code'] + except: + return -1 |
