-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDarkSkyAPI.swift
288 lines (257 loc) · 12.7 KB
/
DarkSkyAPI.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
//
// Forecast.Io.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.
//
// https://darksky.net/dev/
//
// This API Key belongs to me (Ed Danley).
// It is part of their FREE package.
// At some point in time, it's possible somebody else
// will need to register another account
// Depending on the load the Meteo community puts on this account,
// it's possible individuals will need either own API Key.
//
// API Key = 1f76884583c747841b6dd66979a24b3e ([email protected])
// API Key2 (my personal account - [email protected]) e40017609822499c1e04ca9b186c4a2c
// BASE_URL = https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE
// https://api.forecast.io/forecast/e40017609822499c1e04ca9b186c4a2c/37.8267,-122.423?exclude=minutely,hourly,alerts,flags&lang=en&units=us
//
import Cocoa
import Foundation
class DarkSkyAPI: NSObject, XMLParserDelegate
{
let QUERY_PREFIX1 = "https://api.forecast.io/forecast/"
let QUERY_SUFFIX1 = "?exclude=minutely,hourly,alerts,flags&lang="
let QUERY_SUFFIX2 = "&units=us"
var escapedCity = String()
var parseURL = String()
var radarWindow = RadarWindow()
func beginParsing(_ inputCity: String, APIKey1: String, APIKey2: String, weatherFields: inout WeatherFields) {
DebugLog(String(format:"in DarkSkyAPI beginParsing: %@", inputCity))
let defaults = UserDefaults.standard
let updateFrequency = Int(defaults.string(forKey: "updateFrequency")!)
if ((updateFrequency! < 60) && (APIKey1 == ""))
{
defaults.setValue("60", forKey: "updateFrequency")
}
var APIKey = APIKey1
if (APIKey == "")
{
APIKey = "1f76884583c747841b6dd66979a24b3e"
}
parseURL = ""
parseURL.append(QUERY_PREFIX1)
parseURL.append(APIKey) // For now, not using user key
parseURL.append("/")
// lat = inputCity before "," or " "
// lon = inputCity after "," or " "
var token = [String]()
escapedCity = inputCity.trimmingCharacters(in: .whitespacesAndNewlines)
if (escapedCity.contains(", ")) {
token = escapedCity.components(separatedBy: ", ")
escapedCity = token[0] + "," + token[1]
} else if (escapedCity.contains(" ")) {
token = escapedCity.components(separatedBy: " ")
escapedCity = token[0] + "," + token[1]
} else if (escapedCity.contains(" ,")) {
token = escapedCity.components(separatedBy: " ,")
escapedCity = token[0] + "," + token[1]
} else if (escapedCity.contains(",")) {
token = escapedCity.components(separatedBy: ",")
escapedCity = token[0] + "," + token[1]
} else {
//escapedCity = escapedCity
}
parseURL.append(escapedCity as String)
parseURL.append(QUERY_SUFFIX1)
let languageCode = (Locale.current as NSLocale).object(forKey: .languageCode) as? String
parseURL.append(languageCode!)
parseURL.append(QUERY_SUFFIX2)
InfoLog(String(format:"URL for DarkSky: %@\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
// ToDo: Need 3s timeout on the Data() call
} catch {
ErrorLog("DarkSky \(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] {
readJSONObject(object: dictionary, weatherFields: &weatherFields)
}
} catch {
// Handle Error
weatherFields.currentTemp = "9999"
weatherFields.latitude = error.localizedDescription
ErrorLog("DarkSky2 " + String(decoding: data!, as: UTF8.self))
}
DebugLog(String(format:"leaving DarkSkyAPI beginParsing: %@", inputCity))
return
} // beginParsing
func setRadarWind(_ radarWindow1: RadarWindow) {
radarWindow = radarWindow1
} // setRadarWind
func readJSONObject(object: [String: AnyObject], weatherFields: inout WeatherFields) {
guard
let latitude = object["latitude"] as? Double,
let longitude = object["longitude"] as? Double,
let c = object["currently"] as? [String: AnyObject],
let d = object["daily"] as? [String: AnyObject]
else
{
_ = "error"
return
}
weatherFields.latitude = NSString(format: "%.2f", latitude) as String
weatherFields.longitude = NSString(format: "%.2f", longitude) as String
let currently = [c] as [[String: AnyObject]]
for current in currently {
guard
let time = current["time"] as? Double,
let summary = current["summary"] as? String,
let icon = current["icon"] as? String,
let temperature = current["temperature"] as? Double,
let humidity = current["humidity"] as? Double,
let windSpeed = current["windSpeed"] as? Double,
let windGust = current["windGust"] as? Double,
let windBearing = current["windBearing"] as? Double,
let pressure = current["pressure"] as? Double,
let uvIndex = current["uvIndex"] as? Double,
let visibility = current["visibility"] as? Double
else {
_ = "error"
return }
// Convert epoch to UTC
let unixdate: Int
unixdate = Int(time)
let date = NSDate(timeIntervalSince1970: TimeInterval(unixdate))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.timeZone = NSTimeZone.local
weatherFields.date = dateFormatter.string(from: date as Date)
weatherFields.windSpeed = NSString(format: "%.2f", windSpeed) as String
weatherFields.windDirection = NSString(format: "%.2f", windBearing) as String
weatherFields.humidity = NSString(format: "%.0f", humidity * 100.0) as String
weatherFields.pressure = NSString(format: "%.2f", pressure) as String
weatherFields.visibility = NSString(format: "%.1f", visibility + 0.05) as String
weatherFields.windSpeed = NSString(format: "%.2f", windSpeed) as String
weatherFields.windGust = NSString(format: "%.2f", windGust) as String
weatherFields.UVIndex = NSString(format: "%.0f", uvIndex) as String
weatherFields.currentConditions = summary
weatherFields.currentCode = icon
weatherFields.currentTemp = NSString(format: "%.2f", temperature) as String
} // readJSONObject
let daily = [d] as [[String: AnyObject]]
for day in daily {
guard
//let summary = day["summary"] as? String,
//let icon = day["icon"] as? String,
let datum = day["data"] as? [[String: AnyObject]]
else
{
_ = "error"
return
}
for dat in datum {
guard
let time = dat["time"] as? Double,
let summary = dat["summary"] as? String,
let sunriseTime = dat["sunriseTime"] as? Double,
let sunsetTime = dat["sunsetTime"] as? Double,
let temperatureMin = dat["temperatureMin"] as? Double,
let temperatureMax = dat["temperatureMax"] as? Double,
//let pressure = dat["pressure"] as? Double,
let icon = dat["icon"] as? String
else
{
_ = "error"
return
}
// This is for sunrise/sunset for today
if (weatherFields.forecastCounter == 0)
{
// Convert epoch to UTC
var unixdate: Int
unixdate = Int(sunriseTime)
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)
// Convert epoch to UTC
unixdate = Int(sunsetTime)
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
let unixdate: Int
unixdate = Int(time)
let date = NSDate(timeIntervalSince1970: TimeInterval(unixdate))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "E"
//dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.timeZone = TimeZone.ReferenceType.local
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 = TimeZone.ReferenceType.local
dateFormatter.timeZone = NSTimeZone.local
weatherFields.forecastDate[weatherFields.forecastCounter] = dateFormatter.string(from: date as Date)
//var forecastDate = [String]()
weatherFields.forecastCode[weatherFields.forecastCounter] = icon
weatherFields.forecastHigh[weatherFields.forecastCounter] = NSString(format: "%.2f", temperatureMax) as String
weatherFields.forecastLow[weatherFields.forecastCounter] = NSString(format: "%.2f", temperatureMin) as String
weatherFields.forecastConditions[weatherFields.forecastCounter] = summary
weatherFields.forecastCounter = weatherFields.forecastCounter + 1
}
}
}
} // class DarkSkyAPI