| 1 |
.pragma library |
| 2 |
/* |
| 3 |
* Copyright (C) 2013 Canonical Ltd. |
| 4 |
* Copyright (C) 2024 Maciej Sopyło <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software: you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License version 3 as |
| 8 |
* published by the Free Software Foundation. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* Authored by: Raúl Yeguas <[email protected]> |
| 19 |
* Martin Borho <[email protected]> |
| 20 |
* Andrew Starr-Bochicchio <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* Version of the response data format. |
| 25 |
* Increase this number to force a refresh. |
| 26 |
*/ |
| 27 |
var RESPONSE_DATA_VERSION = 20230101; |
| 28 |
|
| 29 |
/** |
| 30 |
* Helper functions |
| 31 |
*/ |
| 32 |
// Remove just the API key |
| 33 |
function trimAPIKey(data) { |
| 34 |
if (data === undefined) { |
| 35 |
return undefined; |
| 36 |
} |
| 37 |
|
| 38 |
const GEONAMES_KEY_PARAM = "username="; |
| 39 |
|
| 40 |
const geonamesIndex = data.indexOf(GEONAMES_KEY_PARAM); |
| 41 |
|
| 42 |
if (geonamesIndex > -1) { |
| 43 |
const ampIndex = data.indexOf("&", geonamesIndex); |
| 44 |
const rangeEnd = ampIndex > -1 ? ampIndex : data.length; |
| 45 |
data = `${data.slice(0, geonamesIndex)}removed_for_logging${data.slice(ampIndex)}`; |
| 46 |
} |
| 47 |
|
| 48 |
return data; |
| 49 |
} |
| 50 |
|
| 51 |
const GeoipApi = (() => { |
| 52 |
const BASE_URL = "http://geoip.ubuntu.com/lookup"; |
| 53 |
|
| 54 |
return { |
| 55 |
getLatLong(params, apiCaller, onSuccess, onError) { |
| 56 |
const request = { |
| 57 |
type: "geolookup", |
| 58 |
url: BASE_URL, |
| 59 |
}; |
| 60 |
function resultHandler(request, xmlDoc) { |
| 61 |
const coords = {}; |
| 62 |
const childNodes = xmlDoc.childNodes; |
| 63 |
|
| 64 |
for (let i = 0; i < childNodes.length; i++) { |
| 65 |
if (childNodes[i].nodeName === "Latitude") { |
| 66 |
coords.lat = childNodes[i].firstChild.nodeValue; |
| 67 |
} else if (childNodes[i].nodeName === "Longitude") { |
| 68 |
coords.lon = childNodes[i].firstChild.nodeValue; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
onSuccess(coords); |
| 73 |
} |
| 74 |
|
| 75 |
function retryHandler(err) { |
| 76 |
console.log("geolookup retry of " + err.request.url); |
| 77 |
apiCaller(request, resultHandler, onError); |
| 78 |
} |
| 79 |
|
| 80 |
apiCaller(request, resultHandler, retryHandler); |
| 81 |
} |
| 82 |
} |
| 83 |
})(); |
| 84 |
|
| 85 |
const GeonamesApi = (() => { |
| 86 |
/** |
| 87 |
provides necessary methods for requesting and preparing data from Geonames.org |
| 88 |
*/ |
| 89 |
const BASE_URL = "http://api.geonames.org/"; |
| 90 |
const USERNAME = "uweatherdev" |
| 91 |
const ADD_PARAMS = "&maxRows=25&featureClass=P" |
| 92 |
|
| 93 |
function _buildSearchResult(request, data) { |
| 94 |
const searchResult = { locations: [], request: request }; |
| 95 |
|
| 96 |
if (!data.geonames) { |
| 97 |
return searchResult; |
| 98 |
} |
| 99 |
|
| 100 |
data.geonames.forEach((location) => { |
| 101 |
searchResult.locations.push({ |
| 102 |
name: location.name, |
| 103 |
coord: { |
| 104 |
lat: location.lat, |
| 105 |
lon: location.lng, |
| 106 |
}, |
| 107 |
country: location.countryCode, |
| 108 |
countryName: location.countryName, |
| 109 |
timezone: location.timezone, |
| 110 |
adminName1: location.adminName1, |
| 111 |
adminName2: location.adminName2, |
| 112 |
adminName3: location.adminName3, |
| 113 |
services: { |
| 114 |
"geonames": location.geonameId, |
| 115 |
}, |
| 116 |
}); |
| 117 |
}); |
| 118 |
|
| 119 |
return searchResult; |
| 120 |
} |
| 121 |
|
| 122 |
return { |
| 123 |
search(mode, params, apiCaller, onSuccess, onError) { |
| 124 |
function retryHandler(err) { |
| 125 |
console.log("search retry of " + err.request.url); |
| 126 |
apiCaller(request, searchResponseHandler, onError); |
| 127 |
} |
| 128 |
|
| 129 |
function searchResponseHandler(request, data) { |
| 130 |
onSuccess(_buildSearchResult(request, data)); |
| 131 |
} |
| 132 |
|
| 133 |
let request; |
| 134 |
if (mode === "point") { |
| 135 |
request = { |
| 136 |
type: "search", |
| 137 |
url: BASE_URL + "findNearbyPlaceNameJSON?style=full&username=" + encodeURIComponent(USERNAME) |
| 138 |
+ "&lat=" + encodeURIComponent(params.coords.lat) + "&lng=" + encodeURIComponent(params.coords.lon) |
| 139 |
+ "&lang=" + encodeURIComponent(params.lang).replace("_","-") |
| 140 |
+ ADD_PARAMS, |
| 141 |
}; |
| 142 |
} else { |
| 143 |
request = { |
| 144 |
type: "search", |
| 145 |
url: BASE_URL + "searchJSON?style=full&username=" + encodeURIComponent(USERNAME) |
| 146 |
+ "&name_startsWith=" + encodeURIComponent(params.name) |
| 147 |
+ "&lang=" + encodeURIComponent(params.lang).replace("_","-") |
| 148 |
+ ADD_PARAMS, |
| 149 |
}; |
| 150 |
} |
| 151 |
|
| 152 |
apiCaller(request, searchResponseHandler, retryHandler); |
| 153 |
} |
| 154 |
} |
| 155 |
})(); |
| 156 |
|
| 157 |
const WeatherApi = ((_services) => { |
| 158 |
/** |
| 159 |
proxy for requesting weather apis, the passed _services are providing the respective api endpoints |
| 160 |
and formatters to build a uniform response object |
| 161 |
*/ |
| 162 |
function _getService(name) { |
| 163 |
if (_services[name] !== undefined) { |
| 164 |
return _services[name]; |
| 165 |
} |
| 166 |
|
| 167 |
return _services["geonames"]; |
| 168 |
} |
| 169 |
|
| 170 |
function _sendRequest(request, onSuccess, onError) { |
| 171 |
const xhr = new XMLHttpRequest(); |
| 172 |
if (!xhr) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
xhr.open('GET', request.url, true); |
| 177 |
xhr.onreadystatechange = () => { |
| 178 |
try { |
| 179 |
if (xhr.readyState !== 4) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
if (xhr.status !== 200) { |
| 184 |
request.url = trimAPIKey(request.url); |
| 185 |
onError({ |
| 186 |
msg: "received error code, got " + xhr.status + ", details: " + xhr.responseText, |
| 187 |
request: request |
| 188 |
}); |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
if (xhr.responseXML) { |
| 193 |
onSuccess(request, xhr.responseXML.documentElement); |
| 194 |
} else { |
| 195 |
const json = JSON.parse(xhr.responseText); |
| 196 |
onSuccess(request, json); |
| 197 |
} |
| 198 |
} catch (e) { |
| 199 |
print("Exception: " + e); |
| 200 |
request.url = trimAPIKey(request.url); |
| 201 |
onError({ msg: "wrong response data format", request: request }); |
| 202 |
} |
| 203 |
}; |
| 204 |
xhr.send(null); |
| 205 |
} |
| 206 |
|
| 207 |
return { |
| 208 |
geoLookup(params, onSuccess, onError) { |
| 209 |
const service = _getService('geoip'); |
| 210 |
const geoNameService = _getService('geonames'); |
| 211 |
|
| 212 |
function lookupHandler(data) { |
| 213 |
print("Geolookup: " + JSON.stringify(data)); |
| 214 |
geoNameService.search("point", { coords: data }, _sendRequest, onSuccess, onError); |
| 215 |
} |
| 216 |
|
| 217 |
service.getLatLong(params, _sendRequest, lookupHandler, onError) |
| 218 |
}, |
| 219 |
|
| 220 |
search(mode, params, onSuccess, onError) { |
| 221 |
const service = _getService('geonames'); |
| 222 |
service.search(mode, params, _sendRequest, onSuccess, onError); |
| 223 |
}, |
| 224 |
} |
| 225 |
})({ |
| 226 |
"geonames": GeonamesApi, |
| 227 |
"geoip": GeoipApi |
| 228 |
}); |
| 229 |
|
| 230 |
function sendRequest(message, responseCallback) { |
| 231 |
// handles the response data |
| 232 |
function finished(result) { |
| 233 |
responseCallback({ |
| 234 |
action: message.action, |
| 235 |
result: result, |
| 236 |
}); |
| 237 |
} |
| 238 |
|
| 239 |
// handles errors |
| 240 |
function onError(err) { |
| 241 |
console.log(JSON.stringify(err, null, true)); |
| 242 |
responseCallback({ error: err }); |
| 243 |
} |
| 244 |
|
| 245 |
// perform the api calls |
| 246 |
if (message.action === "searchByName") { |
| 247 |
WeatherApi.search("name", message.params, finished, onError); |
| 248 |
} else if(message.action === "searchByPoint" |
| 249 |
|| message.action === "searchByUrl") { |
| 250 |
WeatherApi.search("point", message.params, finished, onError); |
| 251 |
} else if(message.action === "getGeoIp") { |
| 252 |
WeatherApi.geoLookup(message.params, finished, onError); |
| 253 |
} |
| 254 |
} |