-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
76 lines (72 loc) · 2.83 KB
/
weather.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
import requests
import sys
from flask import Response
import json
class Weather:
def __init__(self):
self.api_key = 'fa8a0d077c0f90dc6a4f252da0a0ec08'
def get_weather(self, api_key, location):
url = "https://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid={}".format(location, api_key)
r = requests.get(url)
return r
def get_f_weather(self, api_key, location):
url = "https://api.openweathermap.org/data/2.5/forecast?q={}&units=metric&appid={}".format(location, api_key)
r = requests.get(url)
return r
def getwxmain(self):
location = 'Lublin'
weather = self.get_weather(self.api_key, location)
wx = weather.json()
wx['main']['temp'] = round(wx['main']['temp'],0)
wx['main']['feels_like'] = round(wx['main']['feels_like'],0)
wx['main']['temp_min'] = round(wx['main']['temp_min'],0)
wx['main']['temp_max'] = round(wx['main']['temp_max'],0)
#icon
weather_id = wx['weather'][0]['id']
wx_icon = round(weather_id /100,0)
if weather_id == 800:
wx['main']['icon'] = 0
else:
wx['main']['icon'] = wx_icon
weather = json.dumps(wx)
return Response(
response=weather,
status=200,
mimetype='application/json'
)
def getwxforecast(self):
location = 'Lublin'
weather = self.get_f_weather(self.api_key, location)
weather = weather.json()
i=0
tokeep = [0,1,2,8,16,24, 32]
q=0
deleted = 0
while i < weather['cnt']:
if i not in tokeep:
del weather['list'][i-deleted]
deleted+=1
if i in tokeep:
#round temps
weather['list'][i-deleted]['main']['temp_max'] = round(weather['list'][i-deleted]['main']['temp_max'],0)
weather['list'][i-deleted]['main']['temp_min'] = round(weather['list'][i-deleted]['main']['temp_min'],0)
weather['list'][i-deleted]['main']['temp'] = round(weather['list'][i-deleted]['main']['temp'],0)
weather['list'][i-deleted]['main']['feels_like'] = round(weather['list'][i-deleted]['main']['feels_like'],0)
#icons
weather_id = weather['list'][i-deleted]['weather'][0]['id']
wx_icon = round(weather_id /100,0)
if weather_id == 800:
weather['list'][i-deleted]['main']['icon'] = 0
else:
weather['list'][i-deleted]['main']['icon'] = wx_icon
i+=1
print('elementow: ' + str(i))
wx = json.dumps(weather)
return Response(
response=wx,
status=200,
mimetype='application/json'
)
if __name__ == "__main__":
wx = Weather()
print(wx.getwxforecast())