-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha4_APIweatherApp.py
150 lines (123 loc) · 4.45 KB
/
a4_APIweatherApp.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'''
This program is inteded to use the Open Meteo API
and output an API and Mock plot of temperatures in a location of your choice
'''
from abc import ABC, abstractmethod
import requests
import matplotlib.pyplot as plt
from enum import Enum
class Environment(Enum):
TEST = 0
PROD = 1
class Service(ABC):
@abstractmethod
def get_weather_data(self):
pass
class MockService(Service):
def get_weather_data(self):
'''
This method generates mock time and temperature data
that resembles the same output from the Open Meteo API.
It then puts the data into a 2D array with a marker to signify
that it is a mock data set.
'''
dates = ["2024-08-02T02:00",
"2024-08-02T03:00",
"2024-08-02T04:00",
"2024-08-02T05:00",
"2024-08-02T06:00",
"2024-08-02T07:00",
"2024-08-02T08:00",
"2024-08-02T09:00",
"2024-08-02T10:00",
"2024-08-02T11:00",
"2024-08-02T12:00",
"2024-08-02T13:00",
"2024-08-02T14:00",
"2024-08-02T15:00",
"2024-08-02T16:00",
"2024-08-02T17:00",
"2024-08-02T18:00",
"2024-08-02T19:00",
"2024-08-02T20:00",
"2024-08-02T21:00",
"2024-08-02T22:00",
"2024-08-02T23:00",
"2024-08-03T00:00",
"2024-08-03T01:00"]
temps = [19.4, 18.8, 18.2, 17.8, 17.7, 17.7, 17.9, 18, 18.1, 18.6, 18.8, 18.8, 18.9, 19.2, 19.4, 19.6, 19.4, 19.5, 19.4, 19, 18.5, 18, 17.6, 17.3]
mockArray2d = [dates, temps, 0]
return mockArray2d
class APIService(Service):
def get_weather_data(self):
'''
This method asks for user input of the location of where they would
like to get temperature data from and then makes an API request call for weather and time in that location.
It puts the data into their respective arrays.
Finally, puts the data into a 2D array with a marker to signify
that it is an API data set.
'''
latitude = input("Please write your latitude: ")
longitude = input("Please write your longitude: ")
daily_weather_variable = "temperature_2m"
base_url = "https://api.open-meteo.com/v1/forecast"
params = {}
params['latitude'] = latitude # float
params['longitude'] = longitude # float
params['hourly'] = daily_weather_variable # string with "temperature_2m"
response = requests.get(base_url, params=params)
print(response.json())
'''
latitude = input("Please write your latitude: ")
longitude = input("Please write your longitude: ")
response = requests.get(f'https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&hourly=temperature_2m')
'''
if response.status_code == 200:
dates = response.json()['hourly']['time'][0:24]
temps = response.json()['hourly']['temperature_2m'][0:24]
apiArray2d = [dates, temps, 1]
return apiArray2d
class ServiceFactory(ABC):
def create_service(self, num):
'''
This method generates either an API or Mock Service instance based on
the input value.
'''
if num == Environment.PROD:
return APIService()
elif num == Environment.TEST:
return MockService()
class Handler:
def __init__(self, service): #the service will be injected
self.sv = service
def plot(self):
'''
This method takes the data of the given Service and generates a plot
of the temperature over time.
'''
values = []
values = self.sv.get_weather_data()
#returns the 2d array
plt.scatter(values[0], values[1])
if values[2] == 1:
plt.title("API Service")
elif values[2] == 0:
plt.title("Mock Service")
plt.show()
def main():
'''
This main function calls both the Mock and API Services and makes sure
that it outputs a plot of the temperature over time for both.
'''
#API
api_factory = ServiceFactory()
api = api_factory.create_service(Environment.PROD) #api is the instance of the API Service
ans = Handler(api)
ans.plot() #returns the data
#MOCK
mock_factory = ServiceFactory()
mock = mock_factory.create_service(Environment.TEST)
mo = Handler(mock)
mo.plot() #returns the data
if __name__ == "__main__":
main()