-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYahooWeatherAPI.swift
411 lines (354 loc) · 17.5 KB
/
YahooWeatherAPI.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//
// YahooWeatherAPI.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://developer.yahoo.com/weather
//
import Cocoa
import Foundation
// aka APIKey1/Client ID (Consumer Key)
let ConsumerKey = "dj0yJmk9ZGZ6MVN0a1BYUnF0JmQ9WVdrOVRVbEJXV2RWTkc4bWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1mOA--"
// aka APIKey2/Client Secret (Consumer Secret)
let ConsumerSecret = "5ccc0d61e58514d24eac2f95fad475e270b97b84"
// App ID
let appID = "MIAYgU4o"
class YahooWeatherAPI: NSObject, XMLParserDelegate {
let QUERY_PREFIX1 = "https://weather-ydn-yql.media.yahoo.com/forecastrss?location="
let QUERY_PREFIX1A = "https://weather-ydn-yql.media.yahoo.com/forecastrss?lat="
let QUERY_PREFIX1B = "&lon="
let QUERY_PREFIX1A1 = "https://weather-ydn-yql.media.yahoo.com/forecastrss?woeid="
let QUERY_SUFFIX1 = "&u=f&format=json"
// Optionally &lat=12.345,&lon=123.456 instead of location=
var element = NSString()
var localWeatherFields = WeatherFields()
var radarWindow = RadarWindow()
var data: Data?
func beginParsing(_ inputCity: String, displayCity: String, APIKey1: String, APIKey2: String, weatherFields: inout WeatherFields) {
DebugLog(String(format:"in beginParsing: %@", inputCity))
var escapedCity = String()
var parseURL = String()
//var Parser = XMLParser()
// https://developer.yahoo.com/weather/
localWeatherFields = weatherFields
parseURL = ""
escapedCity = inputCity.replacingOccurrences(of: ", ", with: ",")
escapedCity = escapedCity.replacingOccurrences(of: " ,", with: ",")
escapedCity = escapedCity.replacingOccurrences(of: " ", with: "-")
escapedCity = escapedCity.replacingOccurrences(of: "&", with: "")
// Is escapedCity a city, state or a lat,long or a woeid?
let arr = escapedCity.components(separatedBy: ",")
if (arr.count == 1) {
let n1 = arr[0]
let f1 = Int(n1)
let s1 = f1?.description
if ((arr[0] == s1) && (escapedCity.count != 5)) {
parseURL.append(QUERY_PREFIX1A1)
parseURL.append(n1)
} else {
parseURL.append(QUERY_PREFIX1)
parseURL.append(escapedCity)
}
} else if (arr.count == 2) {
let n1 = arr[0]
let f1 = Double(n1)
let s1 = f1?.description
let n2 = arr[1]
let f2 = Double(n2)
let s2 = f2?.description
if ((arr[0] == s1) && (arr[1] == s2)) {
parseURL.append(QUERY_PREFIX1A)
parseURL.append(n1)
parseURL.append(QUERY_PREFIX1B)
parseURL.append(n2)
} else {
parseURL.append(QUERY_PREFIX1)
parseURL.append(escapedCity)
}
} else {
parseURL.append(QUERY_PREFIX1)
parseURL.append(escapedCity)
}
parseURL.append(QUERY_SUFFIX1)
InfoLog(String(format:"URL for Yahoo: %@\n", parseURL))
//print(String(format:"URL for Yahoo: %@\n", parseURL))
var ak1 = APIKey1
var ak2 = APIKey2
if ((ak1 == "") || (ak2 == "")) {
ak1 = ConsumerKey
ak2 = ConsumerSecret
}
// https://github.com/mw99/OhhAuth
//let uc = (key: "", secret: "")
let myURL = URL(string: parseURL)
if (myURL == nil) {
self.localWeatherFields.currentTemp = "9999"
self.localWeatherFields.latitude = "Invalid Location"
} else {
let cc = (key: ak1, secret: ak2)
var req = URLRequest(url: myURL!)
let paras = ["X-Yahoo-App-Id": appID, "Content-Type": "application/json"]
//let paras = ["": ""]
req.oAuthSign(method: "POST", urlFormParameters: paras, consumerCredentials: cc, userCredentials: nil)
req.timeoutInterval = 3.0
let group = DispatchGroup()
group.enter()
let task = URLSession(configuration: .ephemeral).dataTask(with: req) { (data, response, error) in
if let error = error {
print(error)
self.localWeatherFields.currentTemp = "9999"
self.localWeatherFields.latitude = error.localizedDescription
}
else if let data = data {
//print(String(data: data, encoding: .utf8) ?? "Does not look like a utf8 response :(")
//let sData = String(decoding: data, as: UTF8.self)
self.data = data
//InfoLog("Data for: " + parseURL)
//InfoLog(String(decoding: data, as: UTF8.self))
//print(String(decoding: data, as: UTF8.self))
self.processWeatherData(data)
if (self.localWeatherFields.forecastCounter == -1) {
self.localWeatherFields.forecastCounter = 0
}
}
group.leave()
}
task.resume()
group.wait()
}
//self.localWeatherFields.currentTemp = "9999"
//self.localWeatherFields.latitude = localizedString(forKey: "Unknown Error")
if (self.localWeatherFields.forecastCounter == -1) {
self.localWeatherFields.forecastCounter = 0
}
weatherFields = self.localWeatherFields
DebugLog(String(format:"leaving beginParsing: %@", inputCity))
} // beginParsing
func processWeatherData(_ data: Data) {
localWeatherFields.forecastCounter = 0
do {
let object = try JSONSerialization.jsonObject(with: data as Data, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
readJSONObject(object: dictionary, weatherFields: &localWeatherFields)
}
} catch {
// Handle Error
self.localWeatherFields.currentTemp = "9999"
self.localWeatherFields.latitude = error.localizedDescription
ErrorLog("Yahoo2 " + String(decoding: data, as: UTF8.self))
}
} // processWeatherData
func convert_Inches_mbar(_ temp: String) -> String
{
// millibar value = kPa val ue x 33.8637526
let answer = String(Int((temp as NSString).doubleValue * 33.8637526))
return answer
} // convert_Inches_mbar() -> String
func setRadarWind(_ radarWindow1: RadarWindow) {
radarWindow = radarWindow1
} // extendedForecasts
// MARK: - XML support
// XMLParser Methods
var inLat = 0
var inLong = 0
var inLink = 0
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
DebugLog(String(format:"in didStartElement: %@", elementName))
element = elementName as NSString
// This is for City name lookup
if (elementName as NSString).isEqual(to: "yweather:location") {
localWeatherFields.title1 = attributeDict["city"]! + attributeDict["region"]!
} else if (elementName as NSString).isEqual(to: "yweather:wind") {
localWeatherFields.windSpeed = attributeDict["speed"]!
localWeatherFields.windDirection = attributeDict["direction"]!
} else if (elementName as NSString).isEqual(to: "yweather:atmosphere") {
localWeatherFields.humidity = attributeDict["humidity"]!
localWeatherFields.pressure = convert_Inches_mbar(attributeDict["pressure"]!) // Convert Inches to millibars
localWeatherFields.visibility = attributeDict["visibility"]!
} else if (elementName as NSString).isEqual(to: "yweather:astronomy") {
localWeatherFields.sunrise = attributeDict["sunrise"]!
localWeatherFields.sunset = attributeDict["sunset"]!
} else if (elementName as NSString).isEqual(to: "yweather:condition") {
localWeatherFields.date = attributeDict["date"]!
localWeatherFields.currentTemp = attributeDict["temp"]!
localWeatherFields.currentCode = attributeDict["code"]!
localWeatherFields.currentConditions = attributeDict["text"]!
} else if (elementName as NSString).isEqual(to: "yweather:forecast") {
localWeatherFields.forecastCounter = localWeatherFields.forecastCounter + 1
localWeatherFields.forecastDate[localWeatherFields.forecastCounter] = attributeDict["date"]!
localWeatherFields.forecastDay[localWeatherFields.forecastCounter] = attributeDict["day"]!
localWeatherFields.forecastHigh[localWeatherFields.forecastCounter] = attributeDict["high"]!
localWeatherFields.forecastLow[localWeatherFields.forecastCounter] = attributeDict["low"]!
localWeatherFields.forecastCode[localWeatherFields.forecastCounter] = attributeDict["code"]!
localWeatherFields.forecastConditions[localWeatherFields.forecastCounter] = attributeDict["text"]!
} else if (elementName as NSString).isEqual(to: "link") {
inLink = 1
} else if (elementName as NSString).isEqual(to: "geo:lat") {
inLat = 1
} else if (elementName as NSString).isEqual(to: "geo:long") {
inLong = 1
}
DebugLog(String(format:"leaving didStartElement: %@", elementName))
} // parser parser:didStartElement
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
DebugLog(String(format:"in didEndElement: %@", elementName))
// This is for City name lookup
if (elementName as NSString).isEqual(to: "link") {
inLink = 0
} else if (elementName as NSString).isEqual(to: "geo:lat") {
inLat = 0
} else if (elementName as NSString).isEqual(to: "geo:long") {
inLong = 0
}
DebugLog(String(format:"leaving didEndElement: %@", elementName))
} // parser parser:didEndElement
func parser(_ parser: XMLParser, foundCharacters string: String) {
DebugLog(String(format:"in foundCharacters: %@\n", string))
if (inLink == 1) {
let regex = try! NSRegularExpression(pattern: "^(.*?)\\s*https:\\s*(.*)$", options: .caseInsensitive)
if let match = regex.firstMatch(in: string, range: NSRange(string.startIndex ..< string.endIndex, in: string)) {
localWeatherFields.URL = "https:" + String(string[Range(match.range(at: 2), in: string)!])
}
} else if (inLat == 1) {
localWeatherFields.latitude = string
} else if (inLong == 1) {
localWeatherFields.longitude = string
}
DebugLog(String(format:"leaving foundCharacters: %@\n", string))
} // parser parser:foundCharacters
// MARK: - JSON support
func readJSONObject(object: [String: AnyObject], weatherFields: inout WeatherFields) {
guard
let location = object["location"] as? [String: AnyObject],
let co = object["current_observation"] as? [String: AnyObject],
let forecasts = object["forecasts"] as? [[String: AnyObject]]
else {
_ = "error"
return }
for l in [location] {
guard
//let city = l["city"] as? String,
//let region = l["region"] as? String,
//let country = l["country"] as? String,
let lat = l["lat"] as? Double,
let long = l["long"] as? Double,
//let timezone_id = l["timezone_id"] as? String,
let iWoeid = l["woeid"] as? Int
else {
_ = "error"
return
}
weatherFields.latitude = String(describing: lat)
weatherFields.longitude = String(describing: long)
weatherFields.URL = "https://www.yahoo.com/news/weather/forecast/" + String(describing: iWoeid)
}
for c in [co] {
guard
let wind = c["wind"] as? [String: AnyObject],
let atmosphere = c["atmosphere"] as? [String: AnyObject],
let astronomy = c["astronomy"] as? [String: AnyObject],
let pubDate = c["pubDate"] as? Int,
let condition = c["condition"] as? [String: AnyObject]
else {
_ = "error"
return }
for w in [wind] {
guard
let direction = w["direction"] as? Int,
//let chill = w["chill"] as? Int,
let speed = w["speed"] as? Double
else {
_ = "error"
return }
weatherFields.windSpeed = String(describing: speed)
weatherFields.windDirection = String(describing: direction)
}
for atmo in [atmosphere] {
guard
let humidity = atmo["humidity"] as? Int,
//let visiblity = atmo["visiblity"] as? Int,
let pressure = atmo["pressure"] as? Double
else {
_ = "error"
return }
weatherFields.humidity = String(describing: humidity)
weatherFields.pressure = convert_Inches_mbar(String(describing: pressure))
}
for astro in [astronomy] {
guard
let sunrise = astro["sunrise"] as? String,
let sunset = astro["sunset"] as? String
else {
_ = "error"
return }
weatherFields.sunrise = sunrise
weatherFields.sunset = sunset
}
for cond in [condition] {
guard
let cText = cond["text"] as? String,
let cTemperature = cond["temperature"] as? Int,
let cCode = cond["code"] as? Int
else {
_ = "error"
return }
weatherFields.currentCode = String(describing: cCode)
weatherFields.currentTemp = String(describing: cTemperature)
//weatherFields.currentLink = cLink
weatherFields.currentConditions = cText
}
// Convert epoch to m DDD yyyy
let time = NSDate(timeIntervalSince1970: TimeInterval(pubDate))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
weatherFields.date = dateFormatter.string(from: time as Date)
}
for f in forecasts {
guard
let fDay = f["day"] as? String,
let fDate = f["date"] as? Int,
let fLow = f["low"] as? Int,
let fHigh = f["high"] as? Int,
let fText = f["text"] as? String,
let fCode = f["code"] as? Int
else {
_ = "error"
return }
weatherFields.forecastCode[weatherFields.forecastCounter] = String(describing: fCode)
weatherFields.forecastLow[weatherFields.forecastCounter] = String(describing: fLow)
weatherFields.forecastHigh[weatherFields.forecastCounter] = String(describing: fHigh)
weatherFields.forecastDay[weatherFields.forecastCounter] = fDay
// Convert epoch to m DDD yyyy
let date = NSDate(timeIntervalSince1970: TimeInterval(fDate))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "d MMM yyyy"
weatherFields.forecastDate[weatherFields.forecastCounter] = dateFormatter.string(from: date as Date)
weatherFields.forecastConditions[weatherFields.forecastCounter] = fText
weatherFields.forecastCounter = weatherFields.forecastCounter + 1
}
} // readJSONObject
} // class YahooWeatherAPI