-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI_request.py
More file actions
43 lines (40 loc) · 1.78 KB
/
API_request.py
File metadata and controls
43 lines (40 loc) · 1.78 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
from requests.exceptions import HTTPError
import requests
import json
import pandas as pd
class Request:
def __init__(self, city, days):
self.city = city
self.days = days
def forcast(self):
"""
returns required forcast data for days in the city:
In hour element:
time => time
wind_speed => wind_kph
cloud_coverage => cloud
temperature => temp_c
:return: dataframe
"""
url = 'https://api.weatherapi.com/v1/forecast.json?key=bea853d469e0444dbe7152331220507&q={}&days={}'.format(
self.city, self.days)
try:
response = requests.get(url)
response.raise_for_status()
json_data = json.loads(response.text)
# Collect required data as one hour element for the 3 days
list_hour_elements = list()
for i in range(3):
list_hour_elements.extend(json_data['forecast']['forecastday'][i]['hour'])
dataFrame = pd.DataFrame(list_hour_elements)
df_required = dataFrame[['time', 'wind_kph', 'temp_c', 'cloud']]
df_required = df_required.rename(columns={'time': 'Datum/Uhr', 'wind_kph': 'Windgeschwindigkeit (Km/h)',
'temp_c': 'Temperatur (Celsius)',
'cloud': 'Cloud coverage (Percent)'})
return df_required
except HTTPError as http_err:
print(f'HTTPS error: {http_err}')
except Exception as err:
print(f'Other error: {err}')
return pd.DataFrame({'Datum/Uhr': [], 'Windgeschwindigkeit (Km/h)': [], 'Temperatur (Celsius)': [],
'Cloud coverage (Percent)': []})