-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheWeatherAPI.swift
196 lines (163 loc) · 7.66 KB
/
TheWeatherAPI.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
//
// TheWeather.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://api.theweather.com
//
// [email protected]/P@ssw0rd
// affiliate_id = 4p1m5jb7mnyt
// localidad for Naperville = 157281
// http://api.theweather.com/index.php?api_lang=eu&localidad=157281&affiliate_id=4p1m5jb7mnyt
import Cocoa
import Foundation
class TheWeatherAPI: NSObject, XMLParserDelegate
{
let QUERY_PREFIX1 = "http://api.theweather.com/index.php?api_lang=eu&localidad="
let QUERY_SUFFIX1 = "&affiliate_id="
var Parser = XMLParser()
var element = NSString()
var escapedCity = NSString()
var parseURL = String()
var weatherFields = WeatherFields()
var radarWindow = RadarWindow()
func beginParsing(_ inputCity: String, APIKey1: String, APIKey2: String, weatherFields: inout WeatherFields) {
DebugLog(String(format:"in beginParsing: %@", inputCity))
Parser = XMLParser()
// https://TheWeather.com
// Should emit "Powered by Yahoo!", https://poweredby.yahoo.com/purple.png
//var weatherQuery = NSString()
//weatherQuery = "http://api.openweathermap.org/data/2.5/weather?q=&appid=XYZZY&mode=xml&units=imperial"
parseURL = ""
parseURL.append(QUERY_PREFIX1)
parseURL.append(inputCity as String)
parseURL.append(QUERY_SUFFIX1)
parseURL.append(APIKey1)
InfoLog(String(format:"URL for OpenWeatherMap: %@\n", parseURL))
parseURL = ""
parseURL.append(QUERY_PREFIX1)
escapedCity = inputCity.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! as NSString
escapedCity = escapedCity.replacingOccurrences(of: ",", with: "%3D") as NSString
parseURL.append(escapedCity as String)
parseURL.append(QUERY_SUFFIX1)
parseURL.append(APIKey1)
Parser = XMLParser(contentsOf:(URL(string:parseURL as String))!)!
// Find Current weather conditions
Parser.delegate = self
Parser.parse()
DebugLog(String(format:"leaving beginParsing: %@", inputCity))
return
} // beginParsing
func setRadarWind(_ radarWindow1: RadarWindow) {
radarWindow = radarWindow1
} // extendedForecasts
// XMLParser Methods
// For testing: http://api.theweather.com/index.php?api_lang=eu&localidad=157281&affiliate_id=4p1m5jb7mnyt
var inName = 0
var inMinTemp = 0
var inMaxTemp = 0
var inDay = 0
var inSymbol = 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
if (elementName as NSString).isEqual(to: "location") {
weatherFields.title1.append(attributeDict["city"]!)
} else if (elementName as NSString).isEqual(to: "name") {
inName = 1
} else if (elementName as NSString).isEqual(to: "forecast") {
if (inMinTemp == 1)
{
let F = Int((attributeDict["value"]! as NSString).doubleValue * 9/5) + 32
weatherFields.forecastLow[weatherFields.forecastCounter].append(String(describing: F))
weatherFields.forecastCounter = weatherFields.forecastCounter + 1
}
else if (inMaxTemp == 1)
{
let F = Int((attributeDict["value"]! as NSString).doubleValue * 9/5) + 32
weatherFields.forecastHigh[weatherFields.forecastCounter].append(String(describing: F))
weatherFields.forecastCounter = weatherFields.forecastCounter + 1
}
else if (inDay == 1)
{
var string = attributeDict["value"]!
let index = string.index(string.startIndex, offsetBy: 3)
string = String(string.prefix(upTo: index))
weatherFields.forecastDay[weatherFields.forecastCounter].append(string)
weatherFields.forecastCounter = weatherFields.forecastCounter + 1
}
else if (inSymbol == 1)
{
weatherFields.forecastConditions[weatherFields.forecastCounter].append(attributeDict["value"]!)
weatherFields.forecastCode[weatherFields.forecastCounter].append(attributeDict["id"]!)
weatherFields.forecastCounter = weatherFields.forecastCounter + 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))
if (elementName as NSString).isEqual(to: "name") {
inName = 0
}
if (elementName as NSString).isEqual(to: "data") {
inMinTemp = 0
inMaxTemp = 0
inDay = 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 (element as NSString).isEqual(to: "url") {
weatherFields.URL = string
} else if (element as NSString).isEqual(to: "error") {
weatherFields.currentTemp = "9999"
weatherFields.latitude = string
} else if (inName == 1) {
if (string == "Minimum temperature") {
weatherFields.forecastCounter = 0
inMinTemp = 1
}
else if (string == "Maximum temperature") {
weatherFields.forecastCounter = 0
inMaxTemp = 1
}
else if (string == "Day symbol") {
weatherFields.forecastCounter = 0
inDay = 1
}
else if (string == "Symbol") {
weatherFields.forecastCounter = 0
inSymbol = 1
}
else
{
inSymbol = 0
}
}
DebugLog(String(format:"leaving foundCharacters: %@\n", string))
} // parser parser:foundCharacters
}