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
|
package cgeo.geocaching.connector.opencaching;
import cgeo.geocaching.Parameters;
import cgeo.geocaching.Settings;
import cgeo.geocaching.cgBase;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgImage;
import cgeo.geocaching.cgLog;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.IConnector;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter;
import cgeo.geocaching.geopoint.GeopointParser;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.Uri;
import android.text.Html;
import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
final public class OkapiClient {
private static final String CACHE_VOTES = "rating_votes";
private static final String CACHE_NOTFOUNDS = "notfounds";
private static final String CACHE_FOUNDS = "founds";
private static final String CACHE_HIDDEN = "date_hidden";
private static final String CACHE_LATEST_LOGS = "latest_logs";
private static final String CACHE_IMAGE_URL = "url";
private static final String CACHE_IMAGE_CAPTION = "caption";
private static final String CACHE_IMAGE_IS_SPOILER = "is_spoiler";
private static final String CACHE_IMAGES = "images";
private static final String CACHE_HINT = "hint";
private static final String CACHE_DESCRIPTION = "description";
private static final String CACHE_RECOMMENDATIONS = "recommendations";
private static final String CACHE_RATING = "rating";
private static final String CACHE_TERRAIN = "terrain";
private static final String CACHE_DIFFICULTY = "difficulty";
private static final String CACHE_OWNER = "owner";
private static final String CACHE_STATUS = "status";
private static final String CACHE_TYPE = "type";
private static final String CACHE_LOCATION = "location";
private static final String CACHE_NAME = "name";
private static final String CACHE_CODE = "code";
private static final String LOG_TYPE = "type";
private static final String LOG_COMMENT = "comment";
private static final String LOG_DATE = "date";
private static final String LOG_USER = "user";
private static final String USER_USERNAME = "username";
private static final String SERVICE_CACHE = "/okapi/services/caches/geocache";
private static final String SERVICE_CACHE_FIELDS = "code|name|location|type|status|owner|founds|notfounds|size|difficulty|terrain|rating|rating_votes|recommendations|description|hint|images|latest_logs|date_hidden";
private static final String SERVICE_NEAREST = "/okapi/services/caches/search/nearest";
public static cgCache getCache(final String geoCode) {
final Parameters params = new Parameters("cache_code", geoCode, "fields", SERVICE_CACHE_FIELDS);
final JSONObject data = request(ConnectorFactory.getConnector(geoCode), SERVICE_CACHE, params);
if (data == null) {
return null;
}
final cgCache cache = parseCache(data);
long time = new Date().getTime();
cache.setUpdated(time);
cache.setDetailedUpdate(time);
return cache;
}
public static List<cgCache> getCachesAround(final Geopoint center, IConnector connector) {
String centerString = GeopointFormatter.format(GeopointFormatter.Format.LAT_DECDEGREE_RAW, center) + "|" + GeopointFormatter.format(GeopointFormatter.Format.LON_DECDEGREE_RAW, center);
final Parameters params = new Parameters("center", centerString);
final JSONObject data = request(connector, SERVICE_NEAREST, params);
if (data == null) {
return null;
}
return parseCaches(data);
}
private static List<cgCache> parseCaches(final JSONObject response) {
try {
final JSONArray cachesResponse = response.getJSONArray("results");
if (cachesResponse != null) {
ArrayList<String> geocodes = new ArrayList<String>(cachesResponse.length());
for (int i = 0; i < cachesResponse.length(); i++) {
String geocode = cachesResponse.getString(i);
if (StringUtils.isNotBlank(geocode)) {
geocodes.add(geocode);
}
}
List<cgCache> caches = new ArrayList<cgCache>(geocodes.size());
for (String geocode : geocodes) {
cgCache cache = getCache(geocode);
if (cache != null) {
caches.add(cache);
}
}
return caches;
}
} catch (JSONException e) {
Log.e(Settings.tag, "OkapiClient.parseCaches", e);
}
return null;
}
private static cgCache parseCache(final JSONObject response) {
final cgCache cache = new cgCache();
try {
cache.setGeocode(response.getString(CACHE_CODE));
cache.setName(response.getString(CACHE_NAME));
// not used: names
setLocation(cache, response.getString(CACHE_LOCATION));
cache.setType(getCacheType(response.getString(CACHE_TYPE)));
final String status = response.getString(CACHE_STATUS);
cache.setDisabled(status.equalsIgnoreCase("Temporarily unavailable"));
cache.setArchived(status.equalsIgnoreCase("Archived"));
// not used: url
final JSONObject owner = response.getJSONObject(CACHE_OWNER);
cache.setOwner(parseUser(owner));
cache.getLogCounts().put(cgBase.LOG_FOUND_IT, response.getInt(CACHE_FOUNDS));
cache.getLogCounts().put(cgBase.LOG_DIDNT_FIND_IT, response.getInt(CACHE_NOTFOUNDS));
cache.setSize(getCacheSize(response));
cache.setDifficulty((float) response.getDouble(CACHE_DIFFICULTY));
cache.setTerrain((float) response.getDouble(CACHE_TERRAIN));
if (response.has(CACHE_RATING) && !isNull(response.getString(CACHE_RATING))) {
cache.setRating((float) response.getDouble(CACHE_RATING));
}
cache.setVotes(response.getInt(CACHE_VOTES));
cache.setFavouritePoints(response.getInt(CACHE_RECOMMENDATIONS));
// not used: req_password
cache.setDescription(response.getString(CACHE_DESCRIPTION));
cache.setHint(Html.fromHtml(response.getString(CACHE_HINT)).toString());
// not used: hints
final JSONArray images = response.getJSONArray(CACHE_IMAGES);
if (images != null) {
JSONObject imageResponse;
for (int i = 0; i < images.length(); i++) {
imageResponse = images.getJSONObject(i);
if (imageResponse.getBoolean(CACHE_IMAGE_IS_SPOILER)) {
final String title = imageResponse.getString(CACHE_IMAGE_CAPTION);
final String url = absoluteUrl(imageResponse.getString(CACHE_IMAGE_URL), cache.getGeocode());
final cgImage image = new cgImage(url, title);
if (cache.getSpoilers() == null) {
cache.setSpoilers(new ArrayList<cgImage>());
}
cache.getSpoilers().add(image);
}
}
}
// not used: attrnames
cache.setLogs(parseLogs(response.getJSONArray(CACHE_LATEST_LOGS)));
cache.setHidden(parseDate(response.getString(CACHE_HIDDEN)));
} catch (JSONException e) {
Log.e(Settings.tag, "OkapiClient.parseCache", e);
}
return cache;
}
private static String absoluteUrl(String url, String geocode) {
final Uri uri = Uri.parse(url);
if (!uri.isAbsolute()) {
final IConnector connector = ConnectorFactory.getConnector(geocode);
final String host = connector.getHost();
if (StringUtils.isNotBlank(host)) {
return "http://" + host + "/" + url;
}
}
return url;
}
private static boolean isNull(String string) {
return string.equalsIgnoreCase("null");
}
private static String parseUser(JSONObject user) throws JSONException {
return user.getString(USER_USERNAME);
}
private static List<cgLog> parseLogs(JSONArray logsJSON) {
List<cgLog> result = null;
for (int i = 0; i < logsJSON.length(); i++) {
try {
JSONObject logResponse = logsJSON.getJSONObject(i);
cgLog log = new cgLog();
log.date = parseDate(logResponse.getString(LOG_DATE)).getTime();
log.log = logResponse.getString(LOG_COMMENT).trim();
log.type = parseLogType(logResponse.getString(LOG_TYPE));
log.author = parseUser(logResponse.getJSONObject(LOG_USER));
if (result == null) {
result = new ArrayList<cgLog>();
}
result.add(log);
} catch (JSONException e) {
Log.e(Settings.tag, "OkapiClient.parseLogs", e);
}
}
return result;
}
private static int parseLogType(String logType) {
if ("Found it".equalsIgnoreCase(logType)) {
return cgBase.LOG_FOUND_IT;
}
else if ("Didn't find it".equalsIgnoreCase(logType)) {
return cgBase.LOG_DIDNT_FIND_IT;
}
return cgBase.LOG_NOTE;
}
private static Date parseDate(final String date) {
final SimpleDateFormat ISO8601DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
final String strippedDate = date.replaceAll("\\+0([0-9]){1}\\:00", "+0$100");
try {
return ISO8601DATEFORMAT.parse(strippedDate);
} catch (ParseException e) {
Log.e(Settings.tag, "OkapiClient.parseDate", e);
}
return null;
}
private static void setLocation(final cgCache cache, final String location) {
final String latitude = StringUtils.substringBefore(location, "|");
final String longitude = StringUtils.substringAfter(location, "|");
// FIXME: the next line should be a setter at cgCache
cache.setCoords(GeopointParser.parse(latitude, longitude));
}
private static CacheSize getCacheSize(final JSONObject response) {
double size = 0;
try {
size = response.getDouble("size");
} catch (JSONException e) {
Log.e(Settings.tag, "OkapiClient.getCacheSize", e);
}
switch ((int) Math.round(size)) {
case 1:
return CacheSize.MICRO;
case 2:
return CacheSize.SMALL;
case 3:
return CacheSize.REGULAR;
case 4:
return CacheSize.LARGE;
case 5:
return CacheSize.LARGE;
default:
break;
}
return CacheSize.NOT_CHOSEN;
}
private static CacheType getCacheType(final String cacheType) {
if (cacheType.equalsIgnoreCase("Traditional")) {
return CacheType.TRADITIONAL;
} else if (cacheType.equalsIgnoreCase("Multi")) {
return CacheType.MULTI;
} else if (cacheType.equalsIgnoreCase("Quiz")) {
return CacheType.MYSTERY;
} else if (cacheType.equalsIgnoreCase("Virtual")) {
return CacheType.VIRTUAL;
}
return CacheType.UNKNOWN;
}
private static JSONObject request(final IConnector connector, final String service, final Parameters params) {
if (connector == null) {
return null;
}
if (!(connector instanceof ApiOpenCachingConnector)) {
return null;
}
final String host = connector.getHost();
if (StringUtils.isBlank(host)) {
return null;
}
final String uri = "http://" + host + service;
((ApiOpenCachingConnector) connector).addAuthentication(params);
return cgBase.requestJSON(uri, params);
}
}
|