aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java
blob: 5e3d0a1bdf56b1c60f4ce1214f9899ea9f8e7eee (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
package cgeo.geocaching.connector.ox;

import cgeo.geocaching.Geocache;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter;
import cgeo.geocaching.geopoint.Viewport;
import cgeo.geocaching.list.StoredList;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.utils.CryptUtils;
import cgeo.geocaching.utils.Log;

import ch.boye.httpclientandroidlib.HttpResponse;

import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;
import java.util.Collections;

public class OpenCachingApi {

    private static final String API_URL_CACHES_GPX = "http://www.opencaching.com/api/geocache.gpx";
    private static final String DEV_KEY = CryptUtils.rot13("PtqQnHo9RUTht3Np");

    public static Geocache searchByGeoCode(final String geocode) {
        final HttpResponse response = getRequest("http://www.opencaching.com/api/geocache/" + geocode + ".gpx",
                new Parameters(
                        "log_limit", "50",
                        "hint", "true",
                        "description", "html"));
        final Collection<Geocache> caches = importCachesFromResponse(response, true);
        if (CollectionUtils.isNotEmpty(caches)) {
            return caches.iterator().next();
        }
        return null;
    }

    private static HttpResponse getRequest(String string, Parameters parameters) {
        parameters.add("Authorization", DEV_KEY);
        return Network.getRequest(string, parameters);
    }

    private static Collection<Geocache> importCachesFromResponse(final HttpResponse response, final boolean isDetailed) {
        if (response == null) {
            return Collections.emptyList();
        }
        Collection<Geocache> caches;
        try {
            caches = new OXGPXParser(StoredList.STANDARD_LIST_ID, isDetailed).parse(response.getEntity().getContent(), null);
        } catch (Exception e) {
            Log.e("Error importing from OpenCaching.com", e);
            return Collections.emptyList();
        }
        return caches;
    }

    public static Collection<Geocache> searchByCenter(final Geopoint center) {
        final HttpResponse response = getRequest(API_URL_CACHES_GPX,
                new Parameters(
                        "log_limit", "0",
                        "hint", "false",
                        "description", "none",
                        "limit", "20",
                        "center", center.format(GeopointFormatter.Format.LAT_LON_DECDEGREE_COMMA)));
        return importCachesFromResponse(response, false);
    }


    public static Collection<Geocache> searchByBoundingBox(final Viewport viewport) {
        final String bbox = viewport.bottomLeft.format(GeopointFormatter.Format.LAT_LON_DECDEGREE_COMMA) + "," + viewport.topRight.format(GeopointFormatter.Format.LAT_LON_DECDEGREE_COMMA);
        final HttpResponse response = getRequest(API_URL_CACHES_GPX,
                new Parameters(
                        "log_limit", "0",
                        "hint", "false",
                        "description", "none",
                        "limit", "100",
                        "bbox", bbox));
        return importCachesFromResponse(response, false);
    }

}