summaryrefslogtreecommitdiff
path: root/weather.py
blob: cc210229ea17fabf37871ebf86d6727009769708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
        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_temp(self):
        # https://docs.python-requests.org/en/latest/
        try:
            data = requests.get(
                self.CALL, headers=self.HEADERS
            ).json()["properties"]["timeseries"][0]["data"]
            self.temp = data["instant"]["details"]["air_temperature"]
            # data['next_1_hours']['summary']['symbol_code']
            return 0
        except:
            return -1