- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
Latest commit
68 lines (55 loc) · 2.32 KB
/
Copy pathscript.py
File metadata and controls
68 lines (55 loc) · 2.32 KB
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
importrequests
classIPMAService:
base_url="https://api.ipma.pt/open-data/"
def__init__(self):
self.session=requests.Session()
def_request(self, endpoint):
default_url=f"{self.base_url}{endpoint}"
try:
response=self.session.get(default_url)
response.raise_for_status()
returnresponse.json()
exceptrequests.exceptions.HTTPErrorase:
print(f"Error HTTP {response.status_code}: {e}")
returnNone
exceptrequests.exceptions.RequestExceptionase:
print(f"Connection errorr: {e}")
returnNone
#Public function to return all districts for Portugal and other information
defget_district(self):
#api https://api.ipma.pt/open-data/distrits-islands.json
endpoint="distrits-islands.json"
data=self._request(endpoint)
ifdataand'data'indata:
returndata['data']
returnNone
#Public function to return Weather Forecast daily up to 5 days for specific location
defget_forecast_by_id(self, globalIdLocal):
endpoint=f"forecast/meteorology/cities/daily/{globalIdLocal}.json"
returnself._request(endpoint)
#Public function to return Weather Forecast daily up to 5 days aggregate for all local
defget_five_days_forecast_by_City(self):
#https://api.ipma.pt/open-data/forecast/meteorology/cities/daily/{globalIdLocal}.json
district=self.get_district()
ifnotdistrict:
print("The list of districts could not be obtained.")
returnNone
all_data= []
fordistindistrict:
data=self.get_forecast_by_id(dist['globalIdLocal'])
ifdataisnotNoneand'data'indata:
dist_name=dist['local']
forforecastindata['data']:
forecast['district'] =dist_name
#Append each forecast to the list (all_data)
all_data.append(forecast)
returnall_data
ipma_service=IPMAService()
#district = ipma_district.get_district()
#for d in district:
# print(f"{d['local']}")
#forecast = ipma_service.get_five_days_forecast_by_City()
#if forecast:
# print(forecast)
forecas_by_city=ipma_service.get_forecast_by_id(1050200)
print(forecas_by_city)