-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAERISWeatherAPI.swift
363 lines (317 loc) · 15.4 KB
/
AERISWeatherAPI.swift
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// AERISWeather.swift
// Meteorologist
//
// Swift code written by Ed Danley on 9/19/15.
// Copyright © 2015 The Meteorologist Group, LLC. All rights reserved.
//
// Original source by Joe Crobak and Meteorologist Group
// Some new graphics by Matthew Fahrenbacher
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
// OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// http://www.aerisweather.com/develop/
//
// [email protected]/aNX-e5y-5NB-f7y
// APIKey1
// ID:QFNxQi4uU8aCkQuboGCG5
// APIKey2
// SECRET:KwWWG9JoGs9Q8I80VXAtQ7Ktsxanq0P1sNGU59hd
//
// Sample: http://api.aerisapi.com/batch/seattle,wa?&requests=/:observations,/:forecasts&client_id=QFNxQi4uU8aCkQuboGCG5&client_secret=KwWWG9JoGs9Q8I80VXAtQ7Ktsxanq0P1sNGU59hd
import Cocoa
import Foundation
class AerisWeatherAPI: NSObject, XMLParserDelegate
{
let QUERY_PREFIX1 = "http://api.aerisapi.com/observations/"
let QUERY_SUFFIX1a = "?&client_id="
let QUERY_SUFFIX1b = "&client_secret="
let QUERY_PREFIX2 = "http://api.aerisapi.com/forecasts/"
let QUERY_SUFFIX2a = "?&client_id="
let QUERY_SUFFIX2b = "&client_secret="
var weatherFields = WeatherFields()
var radarWindow = RadarWindow()
var escapedCity = NSString()
var parseURL = String()
func fixIcon(icon: String) -> String
{
var workingString = icon
// Remove trailing .png
//workingString = String(workingString.characters.dropLast(4))
workingString = String(workingString[workingString.startIndex..<workingString.index(workingString.endIndex, offsetBy: -4)])
// Remove possible trailing n (rainn vs. rain)
if (workingString.hasSuffix("rain"))
{
}
else if (workingString.hasSuffix("n"))
{
//workingString = String(workingString.characters.dropLast(1))
workingString = String(workingString[workingString.startIndex..<workingString.index(workingString.endIndex, offsetBy: -1)])
}
// Remove possible trailing w (snoww vs. snow)
if (workingString.hasSuffix("snow"))
{
}
else if (workingString.hasSuffix("w"))
{
//workingString = String(workingString.characters.dropLast(1))
workingString = String(workingString[workingString.startIndex..<workingString.index(workingString.endIndex, offsetBy: -1)])
}
// Remove possible leading am_ (am_showers)
if ((workingString.hasPrefix("am_")) ||
(workingString.hasPrefix("pm_")))
{
//workingString = String(workingString.characters.dropFirst(3))
workingString = String(workingString[workingString.index(workingString.startIndex, offsetBy: 3)..<workingString.endIndex])
}
// Convert pcloudy or mcloudy to cloudy
if ((workingString.hasPrefix("pcloudy")) ||
(workingString.hasPrefix("mcloudy")))
{
//workingString = String(workingString.characters.dropFirst(1))
workingString = String(workingString[workingString.index(workingString.startIndex, offsetBy: 1)..<workingString.endIndex])
}
// mcloudysf (light snow vs. snow)
if (workingString.hasSuffix("sf"))
{
//workingString = String(workingString.characters.dropLast(1))
workingString = String(workingString[workingString.startIndex..<workingString.index(workingString.endIndex, offsetBy: -1)])
}
return workingString
} // fixIcon
func beginParsing(_ inputCity: String, APIKey1: String, APIKey2: String, weatherFields: inout WeatherFields) {
DebugLog(String(format:"in AerisWeatherAPI beginParsing: %@", inputCity))
//escapedCity = inputCity.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! as NSString
//escapedCity = escapedCity.replacingOccurrences(of: ",", with: "%3D") as NSString
//parseURL = QUERY_PREFIX1 + (escapedCity as String) + QUERY_SUFFIX1a + APIKey1 + QUERY_SUFFIX1b + APIKey2
parseURL = QUERY_PREFIX1 + inputCity + QUERY_SUFFIX1a + APIKey1 + QUERY_SUFFIX1b + APIKey2
InfoLog(String(format:"URL for observations AerisWeather: %@\n", parseURL))
// https://www.hackingwithswift.com/example-code/strings/how-to-load-a-string-from-a-website-url
let url = URL(string: parseURL)
var data: NSData?
data = nil
if (url != nil)
{
do {
// https://stackoverflow.com/questions/40812416/nsurl-url-and-nsdata-data?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
data = try Data(contentsOf: url!) as NSData
} catch {
ErrorLog("AERIS \(error)")
weatherFields.currentTemp = "9999"
weatherFields.latitude = "\(error)"
return
}
}
if (data == nil)
{
weatherFields.currentTemp = "9999"
weatherFields.latitude = localizedString(forKey: "InvalidKey_")
return
}
do {
let object = try JSONSerialization.jsonObject(with: data! as Data, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
readJSONObjectE(object: dictionary, weatherFields: &weatherFields)
}
} catch {
// Handle Error
weatherFields.currentTemp = "9999"
weatherFields.latitude = error.localizedDescription
ErrorLog("AERIS1 " + String(decoding: data!, as: UTF8.self))
}
if (weatherFields.currentTemp == "9999")
{
return
}
do {
let object = try JSONSerialization.jsonObject(with: data! as Data, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
readJSONObject(object: dictionary, weatherFields: &weatherFields)
}
} catch {
// Handle Error
weatherFields.currentTemp = "9999"
weatherFields.latitude = error.localizedDescription
ErrorLog("AERIS2 " + String(decoding: data!, as: UTF8.self))
}
parseURL = QUERY_PREFIX2 + inputCity + QUERY_SUFFIX2a + APIKey1 + QUERY_SUFFIX2b + APIKey2
InfoLog(String(format:"URL for forecasts AerisWeather: %@\n", parseURL))
data = NSData(contentsOf: URL(string: parseURL)!)
do {
let object = try JSONSerialization.jsonObject(with: data! as Data, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
readJSONObjectF(object: dictionary, weatherFields: &weatherFields)
}
} catch {
// Handle Error
weatherFields.currentTemp = "9999"
weatherFields.latitude = error.localizedDescription
ErrorLog("AERIS3 " + String(decoding: data!, as: UTF8.self))
}
DebugLog(String(format:"leaving AerisWeatherAPI beginParsing: %@", inputCity))
return
} // beginParsing
func setRadarWind(_ radarWindow1: RadarWindow) {
radarWindow = radarWindow1
} // setRadarWind
func readJSONObject(object: [String: AnyObject], weatherFields: inout WeatherFields) {
guard
let response = object["response"] as? [String: AnyObject]
else {
_ = "error"
return }
for r2 in [response] {
guard
let id = r2["id"] as? String,
let ob2 = r2["ob"] as? [String: AnyObject],
let place = r2["place"] as? [String: AnyObject],
let loc = r2["loc"] as? [String: AnyObject]
else {
_ = "error"
return }
for ob in [ob2] {
guard
let timestamp = ob["timestamp"] as? Double,
let tempF = ob["tempF"] as? Double,
let humidity = ob["humidity"] as? Double,
let pressureMB = ob["pressureMB"] as? Double,
let windMPH = ob["windMPH"] as? Double,
let windDirDEG = ob["windDirDEG"] as? Double,
let weather = ob["weather"] as? String,
//let weatherShort = ob["weatherShort"] as? String,
let icon = ob["icon"] as? String,
let sunrise = ob["sunrise"] as? Double,
let sunset = ob["sunset"] as? Double
else {
_ = "error"
return }
weatherFields.URL = "https://wx.aerisweather.com/?pands=" + id
weatherFields.currentConditions = weather
weatherFields.currentCode = fixIcon(icon: icon)
weatherFields.currentTemp = NSString(format: "%.2f", tempF) as String
weatherFields.humidity = NSString(format: "%.0f", humidity) as String
weatherFields.pressure = NSString(format: "%.0f", pressureMB) as String
weatherFields.windSpeed = NSString(format: "%.2f", windMPH) as String
weatherFields.windDirection = NSString(format: "%.2f", windDirDEG) as String
// Convert epoch to UTC
var unixdate: Int
unixdate = Int(sunrise)
var date = NSDate(timeIntervalSince1970: TimeInterval(unixdate))
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.timeZone = NSTimeZone.local
weatherFields.sunrise = dateFormatter.string(from: date as Date)
unixdate = Int(sunset)
date = NSDate(timeIntervalSince1970: TimeInterval(unixdate))
dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.timeZone = NSTimeZone.local
weatherFields.sunset = dateFormatter.string(from: date as Date)
// Convert epoch to UTC
unixdate = Int(timestamp)
date = NSDate(timeIntervalSince1970: TimeInterval(unixdate))
dateFormatter = DateFormatter()
dateFormatter.dateFormat = "E"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.timeZone = NSTimeZone.local
weatherFields.forecastDay[weatherFields.forecastCounter] = dateFormatter.string(from: date as Date)
// Convert epoch to DOW
dateFormatter.dateFormat = "d MMM yyyy"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.timeZone = NSTimeZone.local
weatherFields.forecastDate[weatherFields.forecastCounter] = dateFormatter.string(from: date as Date)
for l2 in [loc] {
guard
let lat = l2["lat"] as? Double,
let long = l2["long"] as? Double
else {
_ = "error"
return }
weatherFields.latitude = NSString(format: "%.2f", lat) as String
weatherFields.longitude = NSString(format: "%.2f", long) as String
}
for p in [place] {
guard
let name = p["name"] as? String
else {
_ = "error"
return }
weatherFields.title1 = name
}
}
}
} // readJSONObject
func readJSONObjectE(object: [String: AnyObject], weatherFields: inout WeatherFields) {
guard
let error = object["error"] as? [String: AnyObject]
else {
_ = "error"
return }
for e in [error] {
guard
let description = e["description"] as? String
else {
_ = "error"
return }
weatherFields.currentTemp = "9999"
weatherFields.latitude = description
}
} // readJSONObjectE
func readJSONObjectF(object: [String: AnyObject], weatherFields: inout WeatherFields) {
guard
let response = object["response"] as? [[String: AnyObject]]
else {
_ = "error"
return }
for r2 in response {
guard
let periods = r2["periods"] as? [[String: AnyObject]]
else {
_ = "error"
return }
for p in periods {
guard
let timestamp = p["timestamp"] as? Double,
let maxTempF = p["maxTempF"] as? Double,
let minTempF = p["minTempF"] as? Double,
let weather = p["weather"] as? String,
let icon = p["icon"] as? String
else {
_ = "error"
return }
// Convert epoch to UTC
let unixdate: Int
unixdate = Int(timestamp)
let date = NSDate(timeIntervalSince1970: TimeInterval(unixdate))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "E"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.timeZone = NSTimeZone.local
weatherFields.forecastDay[weatherFields.forecastCounter] = dateFormatter.string(from: date as Date)
weatherFields.forecastLow[weatherFields.forecastCounter] = NSString(format: "%.0f", minTempF) as String
weatherFields.forecastHigh[weatherFields.forecastCounter] = NSString(format: "%.0f", maxTempF) as String
weatherFields.forecastConditions[weatherFields.forecastCounter] = weather
let truncated = fixIcon(icon: icon)
weatherFields.forecastCode[weatherFields.forecastCounter] = truncated
weatherFields.forecastCounter = weatherFields.forecastCounter + 1
}
}
} // readJSONObjectF
} // class AerisWeatherAPI