-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.gd
338 lines (303 loc) · 11.8 KB
/
Main.gd
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
extends Control
#preloading these scenese so I can make nodes out of them later on
var Hourly = preload("res://Hourly.tscn")
var Daily = preload("res://DailyWeather.tscn")
var currentTime
var currentDateAndTime
var click_mouse_poition
onready var options = $Options
enum OptionsIds{
SHOW_HOURLY,
SHOW_DAILY,
QUIT
}
#true - show hourly/daily nodes
#false - hide hourly/daily nodes
var showHourly = true
var showDaily = true
#string that represents the API used for requesting AccuWeather data
var API = null
func _ready():
#setting the visibility of main nodes and transparency of the whole windows
get_tree().get_root().set_transparent_background(true)
beginning()
#checking if the options file exists and reading
var file = File.new()
file.open("res://options.dat", File.READ)
if (file.is_open()):
API = file.get_line()
showHourly = bool(int(file.get_line()))
showDaily = bool(int(file.get_line()))
file.close()
else:
$LineEdit.visible = true
if (API != null):
$Content.visible = true
start_everything()
#creating the context menu items
options.add_check_item("Show hourly weather", OptionsIds.SHOW_HOURLY)
options.set_item_checked(OptionsIds.SHOW_HOURLY, showHourly)
options.add_check_item("Show daily weather", OptionsIds.SHOW_DAILY)
options.set_item_checked(OptionsIds.SHOW_DAILY, showDaily)
options.add_item("Quit", OptionsIds.QUIT)
func _input(event):
#opening the context menu
if (event is InputEventMouseButton and event.is_pressed() and event.button_index == BUTTON_RIGHT):
click_mouse_poition = get_global_mouse_position()
options.popup(Rect2(click_mouse_poition.x, click_mouse_poition.y, options.rect_size.x, options.rect_size.y))
func _process(_delta):
#handles the text API input
if (API == null):
if (Input.is_action_just_pressed("ui_accept")):
API = $LineEdit.text
$LineEdit.visible = false
$Content.visible = true
$HideHints.start()
$Moving/MovingArea.visible = true
$ScrollingArea.visible = true
save_settings()
else:
$Content/ScrollContainer.visible = showHourly
$Content/Daily.visible = showDaily
currentDateAndTime = OS.get_datetime()
var hours
var minutes
#by default, if it is, for instance, 09:07 Godot will see it as 9:7
#these if statements fix that
if (currentDateAndTime["hour"] < 10):
hours = "0" + str(currentDateAndTime["hour"])
else:
hours = str(currentDateAndTime["hour"])
if (currentDateAndTime["minute"] < 10):
minutes = "0" + str(currentDateAndTime["minute"])
else:
minutes = str(currentDateAndTime["minute"])
$Content/CurrentWeather.currentTime = hours + ":" + minutes
match (currentDateAndTime["month"]):
1:
$Content/CurrentWeather.currentDate = "January " + str(currentDateAndTime["day"])
2:
$Content/CurrentWeather.currentDate = "February " + str(currentDateAndTime["day"])
3:
$Content/CurrentWeather.currentDate = "March " + str(currentDateAndTime["day"])
4:
$Content/CurrentWeather.currentDate = "April " + str(currentDateAndTime["day"])
5:
$Content/CurrentWeather.currentDate = "May " + str(currentDateAndTime["day"])
6:
$Content/CurrentWeather.currentDate = "June " + str(currentDateAndTime["day"])
7:
$Content/CurrentWeather.currentDate = "July " + str(currentDateAndTime["day"])
8:
$Content/CurrentWeather.currentDate = "August " + str(currentDateAndTime["day"])
9:
$Content/CurrentWeather.currentDate = "September " + str(currentDateAndTime["day"])
10:
$Content/CurrentWeather.currentDate = "October " + str(currentDateAndTime["day"])
11:
$Content/CurrentWeather.currentDate = "November " + str(currentDateAndTime["day"])
12:
$Content/CurrentWeather.currentDate = "December " + str(currentDateAndTime["day"])
#function that starts everything
func start_everything():
accuweather_current()
create_hourly_weather_nodes()
$CurrentAndHourlyTimer.start()
create_daily_weather_nodes()
$DailyTimer.start()
#deletes old Hourly nodes (if they exist) and calls accuweather_hourly() to create new ones
func create_hourly_weather_nodes():
if ($Content/ScrollContainer/Hourly.get_child_count() != 0):
for child in $Content/ScrollContainer/Hourly.get_children():
$Content/ScrollContainer/Hourly.remove_child(child)
child.queue_free()
accuweather_hourly()
#deletes old Daily nodes (if they exist) and calls accuweather_daily() to create new ones
func create_daily_weather_nodes():
if ($Content/Daily.get_child_count() != 0):
for child in $Content/Daily.get_children():
$Content/Daily.remove_child(child)
child.queue_free()
accuweather_daily()
#making a new http request node, requesting data and calling current_weather_api
func accuweather_current():
var accuWeatherCurrent = HTTPRequest.new()
add_child(accuWeatherCurrent)
accuWeatherCurrent.connect("request_completed", self, "current_weather_api")
var requestAPI = "http://dataservice.accuweather.com/currentconditions/v1/298198?apikey=" + API + "%20&details=true"
var errorCurrent = accuWeatherCurrent.request(requestAPI)
if (errorCurrent != OK):
var errorLabel = Label.new()
add_child(errorLabel)
errorLabel.text = "There has been an error while requesting data"
#making a new http request node, requesting data and calling hourly_weather_api
func accuweather_hourly():
var accuWeatherHourly = HTTPRequest.new()
add_child(accuWeatherHourly)
accuWeatherHourly.connect("request_completed", self, "hourly_weather_api")
var requestAPI = "http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/298198?apikey=" + API + "%20&details=true&metric=true"
var errorHourly = accuWeatherHourly.request(requestAPI)
if (errorHourly != OK):
var errorLabel = Label.new()
add_child(errorLabel)
errorLabel.text = "There has been an error while requesting data"
#making a new http request node, requesting data and calling daily_weather_api
func accuweather_daily():
var accuWeatherDaily = HTTPRequest.new()
add_child(accuWeatherDaily)
accuWeatherDaily.connect("request_completed", self, "daily_weather_api")
var requestAPI = "http://dataservice.accuweather.com/forecasts/v1/daily/5day/298198?apikey=" + API + "&details=true&metric=true"
var errorDaily = accuWeatherDaily.request(requestAPI)
if (errorDaily != OK):
var errorLabel = Label.new()
add_child(errorLabel)
errorLabel.text = "There has been an error while requesting data"
#parsing the body of the response as json
func current_weather_api(_result, _response_code, _headers, body):
var json = JSON.parse(body.get_string_from_utf8())
#check if the returned result is valid
if (typeof(json.result) == TYPE_DICTIONARY):
var message = Label.new()
get_node(".").add_child(message)
if ("The allowed number" in str(json.result)):
message.text = "You have exceeded the number of free daily API calls"
message.set_global_position(Vector2(300, 170))
yield(get_tree().create_timer(7), "timeout")
get_tree().quit()
else:
API = null
beginning()
$LineEdit.visible = true
else:
var apiResult = json.result[0]
$Content/CurrentWeather.currentTemp = str(int(apiResult["Temperature"]["Metric"]["Value"]))
$Content/CurrentWeather.bodyTemp = str(int(apiResult["RealFeelTemperature"]["Metric"]["Value"]))
#geting the name and path of the correct weather icon to show
var iconPath = "res://Icons/" + str(apiResult["WeatherIcon"]) + "-s.png"
#loading the weather icon
var icon = load(iconPath)
$Content/CurrentWeather.weatherIcon = icon
func hourly_weather_api(_result, _response_code, _headers, body):
var json = JSON.parse(body.get_string_from_utf8())
#scrollHbox var is used as a parent node later to create children hourly nodes to
var scrollHbox = get_node("Content/ScrollContainer/Hourly")
#check if the returned result is valid
if (typeof(json.result) == TYPE_DICTIONARY):
var message = Label.new()
get_node(".").add_child(message)
if ("The allowed number" in str(json.result)):
message.text = "You have exceeded the number of free daily API calls"
message.set_global_position(Vector2(300, 270))
yield(get_tree().create_timer(7), "timeout")
get_tree().quit()
else:
#there is weather for the next 12 hours given for free through the AccuWeather API
#each loop is for 1 hour
for x in 12:
var apiResult = json.result[x]
#hourly is an instance of the preloaded scene Hourly and than added as a child
var hourly = Hourly.instance()
scrollHbox.add_child(hourly)
hourly.temp = str(int(apiResult["Temperature"]["Value"]))
hourly.bodyTemp = str(int(apiResult["RealFeelTemperature"]["Value"]))
var iconPath = "res://Icons/" + str(apiResult["WeatherIcon"]) + "-s.png"
var icon = load(iconPath)
hourly.weatherIcon = icon
var time = apiResult["DateTime"]
#the DateTime key in the json file contains more than just the time
#with the substr I am just taking the time portion of the whole value
hourly.time = time.substr(11, 5)
#the same from hourly_weather_api applies here
func daily_weather_api(_result, _response_code, _headers, body):
var json = JSON.parse(body.get_string_from_utf8())
#check if the returned result is valid
if ("The allowed number" in str(json.result)):
var message = Label.new()
get_node(".").add_child(message)
message.text = "Check your AccuWeather App diagnostics"
message.set_global_position(Vector2(300, 370))
yield(get_tree().create_timer(7), "timeout")
get_tree().quit()
elif("Unauthorized" in str(json.result)):
pass
else:
var HBox = get_node("Content/Daily")
var result = json.result["DailyForecasts"]
for x in range(5):
var apiResult = result[x]
var day
var daily = Daily.instance()
HBox.add_child(daily)
var dayInt = OS.get_datetime()["weekday"]
dayInt += x
if (dayInt >= 7):
var remainder = dayInt - 7
dayInt = 0 + remainder
match (dayInt):
0:
day = "Monday"
1:
day = "Tuesday"
2:
day = "Wednesday"
3:
day = "Thursday"
4:
day = "Friday"
5:
day = "Saturday"
6:
day = "Sunday"
daily.day = day
var iconPath = "res://Icons/" + str(apiResult["Day"]["Icon"]) + "-s.png"
var icon = load(iconPath)
daily.weatherIcon = icon
daily.minTemp = str(int(apiResult["Temperature"]["Minimum"]["Value"]))
daily.maxTemp = str(int(apiResult["Temperature"]["Maximum"]["Value"]))
#when an item from the context menu is clicked, it's ID is parsed here
func _on_Options_id_pressed(id):
match (id):
OptionsIds.SHOW_HOURLY:
showHourly = !showHourly
options.set_item_checked(OptionsIds.SHOW_HOURLY, showHourly)
OptionsIds.SHOW_DAILY:
showDaily = !showDaily
options.set_item_checked(OptionsIds.SHOW_DAILY, showDaily)
OptionsIds.QUIT:
#when the QUIT button is clicked, the options.dat file is deleted and new one created with new data
var dir = Directory.new()
dir.remove("res://options.dat")
save_settings()
get_tree().quit()
#timeout event that happens every hour (3600 seconds)
#every time it happens old nodes get deleted, new ones get created and the timer is strarted again
func _on_CurrentAndHourlyTimer_timeout():
accuweather_current()
create_hourly_weather_nodes()
$CurrentAndHourlyTimer.start()
#timeout event that hides hints after 3.5 seconds if there is no options.dat file to be read
func _on_HideHints_timeout():
$ScrollingArea.visible = false
$Moving/MovingArea.visible = false
start_everything()
#timeout event that happens every 12 hours (43200 seconds)
func _on_DailyTimer_timeout():
create_daily_weather_nodes()
$DailyTimer.start()
func save_settings():
var file = File.new()
file.open("res://options.dat", File.WRITE)
if (file.is_open()):
file.store_line(API)
file.store_line(str(int(showHourly)))
file.store_line(str(int(showDaily)))
file.close()
func beginning():
$Content/ScrollContainer.visible = showHourly
$Content/Daily.visible = showDaily
$MovingBackground.hide()
$Content/ScrollContainer.get_h_scrollbar().rect_scale.x = 0
$Content.visible = false
$LineEdit.visible = false
$Moving/MovingArea.visible = false
$ScrollingArea.visible = false