aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java
blob: e9dcded8e9413636fdec6ebb227778ef0c932b56 (plain)
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
package cgeo.geocaching.connector.opencaching;

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.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 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 = "fields=code|name|location|type|status|owner|founds|notfounds|size|difficulty|terrain|rating|rating_votes|recommendations|description|hint|images|latest_logs|date_hidden";

    public static cgCache getCache(final String geoCode) {
        final String params = "cache_code=" + geoCode + "&" + SERVICE_CACHE_FIELDS;
        final String data = request(geoCode, SERVICE_CACHE, params, 1);

        if (StringUtils.isBlank(data)) {
            return null;
        }

        final cgCache cache = parseCache(data);

        cache.updated = new Date().getTime();
        cache.detailedUpdate = new Date().getTime();

        return cache;
    }

    private static cgCache parseCache(final String data) {
        final cgCache cache = new cgCache();
        try {
            final JSONObject response = new JSONObject(data);
            cache.geocode = response.getString(CACHE_CODE);
            cache.name = response.getString(CACHE_NAME);
            // not used: names
            setLocation(cache, response.getString(CACHE_LOCATION));
            cache.type = getCacheType(response.getString(CACHE_TYPE));

            final String status = response.getString(CACHE_STATUS);
            cache.disabled = status.equalsIgnoreCase("Temporarily unavailable");
            cache.archived = status.equalsIgnoreCase("Archived");

            // not used: url
            final JSONObject owner = response.getJSONObject(CACHE_OWNER);
            cache.owner = parseUser(owner);

            cache.logCounts.put(cgBase.LOG_FOUND_IT, response.getInt(CACHE_FOUNDS));
            cache.logCounts.put(cgBase.LOG_DIDNT_FIND_IT, response.getInt(CACHE_NOTFOUNDS));
            cache.size = getCacheSize(response);
            cache.difficulty = (float) response.getDouble(CACHE_DIFFICULTY);
            cache.terrain = (float) response.getDouble(CACHE_TERRAIN);
            if (response.has(CACHE_RATING) && !isNull(response.getString(CACHE_RATING))) {
                cache.rating = (float) response.getDouble(CACHE_RATING);
            }
            cache.votes = response.getInt(CACHE_VOTES);

            cache.favouriteCnt = response.getInt(CACHE_RECOMMENDATIONS);
            // not used: req_password
            cache.description = response.getString(CACHE_DESCRIPTION);
            cache.hint = Html.fromHtml(response.getString(CACHE_HINT)).toString();
            // not used: hints

            final JSONArray images = response.getJSONArray(CACHE_IMAGES);
            if (images != null) {
                JSONObject imageResponse;
                cgImage image;
                for (int i = 0; i < images.length(); i++) {
                    imageResponse = images.getJSONObject(i);
                    if (imageResponse.getBoolean(CACHE_IMAGE_IS_SPOILER)) {
                        image = new cgImage();
                        image.title = imageResponse.getString(CACHE_IMAGE_CAPTION);
                        image.url = absoluteUrl(imageResponse.getString(CACHE_IMAGE_URL), cache.geocode);
                        if (cache.spoilers == null) {
                            cache.spoilers = new ArrayList<cgImage>();
                        }
                        cache.spoilers.add(image);
                    }
                }
            }

            // not used: attrnames
            cache.logs = parseLogs(response.getJSONArray(CACHE_LATEST_LOGS));
            cache.hidden = parseDate(response.getString(CACHE_HIDDEN));

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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);
            return "http://" + connector.getHost() + "/" + 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) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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 lines should be a setter at cgCache
        cache.coords = GeopointParser.parse(latitude, longitude);
        cache.latitudeString = cgBase.formatLatitude(cache.coords.getLatitude(), true);
        cache.longitudeString = cgBase.formatLongitude(cache.coords.getLongitude(), true);
    }

    private static CacheSize getCacheSize(final JSONObject response) {
        double size = 0;
        try {
            size = response.getDouble("size");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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 String getCacheType(final String type) {
        if (type.equalsIgnoreCase("Traditional")) {
            return "traditional";
        } else if (type.equalsIgnoreCase("Multi")) {
            return "multi";
        } else if (type.equalsIgnoreCase("Quiz")) {
            return "mystery";
        } else if (type.equalsIgnoreCase("Virtual")) {
            return "virtual";
        }
        return "other";
    }

    private static String request(final String geoCode, final String service, final String params, final int level) {
        final IConnector connector = ConnectorFactory.getConnector(geoCode);
        if (connector == null) {
            return null;
        }
        if (!(connector instanceof ApiOpenCachingConnector)) {
            return null;
        }

        return cgBase.requestJSON(connector.getHost(), service, params + ((ApiOpenCachingConnector) connector).getAuthentication(level));
    }
}