-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weather.py
100 lines (82 loc) · 2.63 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import requests
import sys
import json
import datetime
from os.path import getmtime, exists
class Weather:
def __init__(self):
self.response = []
self.forecast_data = {}
self.counter = 0
def get_response(self, key):
url = "https://visual-crossing-weather.p.rapidapi.com/forecast"
querystring = {
"location": "Krakow,Poland",
"aggregateHours": "24",
"shortColumnNames": "0",
"unitGroup": "metric",
"contentType": "json",
}
headers = {
"x-rapidapi-key": key,
"x-rapidapi-host": "visual-crossing-weather.p.rapidapi.com",
}
self.response = requests.request(
"GET", url, headers=headers, params=querystring
).json()
def load_response(self, key, file):
if not exists(file):
self.get_response(key)
self.save_response(file)
return
sec = getmtime(file)
current = datetime.datetime.now().timestamp()
if current - sec < 60 * 60 * 24:
with open(file, "r") as f:
self.response = json.load(f)
else:
self.get_response(key)
self.save_response(file)
def save_response(self, file):
with open(file, "w") as fp:
file_content = json.dumps(self.response)
fp.write(file_content)
return True
def forecast(self):
for curr_val in self.response["locations"]["Krakow,Poland"]["values"]:
date = datetime.datetime.utcfromtimestamp(
curr_val["datetime"] / 1000
).strftime("%Y-%m-%d")
daily_forecast = curr_val["conditions"]
self.forecast_data[date] = daily_forecast
def __getitem__(self, item):
if item not in self.forecast_data:
return "NIE WIEM."
if "Rain" in self.forecast_data[item]:
return "BĘDZIE PADAC."
else:
return "NIE BEDZIE PADAC."
def items(self):
return self.forecast_data.items()
def __iter__(self):
self.counter = 0
return self
def __next__(self):
if len(self.forecast_data) <= self.counter:
raise StopIteration
d = list(self.forecast_data)[self.counter]
self.counter += 1
return d
weather = Weather()
key = input()
outfile = sys.argv[1]
weather.load_response(key, outfile)
weather.forecast()
if len(sys.argv) >= 3:
print(weather[sys.argv[2]])
if len(sys.argv) < 3:
x = datetime.date.today() + datetime.timedelta(days=1)
y = x.strftime("%Y-%m-%d")
print(weather[y])
for d in weather:
print(d)