diff options
Diffstat (limited to 'main/src/cgeo/geocaching/connector/ox')
-rw-r--r-- | main/src/cgeo/geocaching/connector/ox/OXConnector.java | 22 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java | 45 |
2 files changed, 54 insertions, 13 deletions
diff --git a/main/src/cgeo/geocaching/connector/ox/OXConnector.java b/main/src/cgeo/geocaching/connector/ox/OXConnector.java index c81011f..4c53361 100644 --- a/main/src/cgeo/geocaching/connector/ox/OXConnector.java +++ b/main/src/cgeo/geocaching/connector/ox/OXConnector.java @@ -3,17 +3,20 @@ package cgeo.geocaching.connector.ox; import cgeo.geocaching.SearchResult; import cgeo.geocaching.Settings; import cgeo.geocaching.cgCache; -import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.connector.AbstractConnector; +import cgeo.geocaching.connector.capability.ISearchByCenter; +import cgeo.geocaching.connector.capability.ISearchByGeocode; +import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.utils.CancellableHandler; +import java.util.Collection; import java.util.regex.Pattern; /** * connector for OpenCaching.com * */ -public class OXConnector extends AbstractConnector { +public class OXConnector extends AbstractConnector implements ISearchByCenter, ISearchByGeocode { private static final Pattern PATTERN_GEOCODE = Pattern.compile("OX[A-Z0-9]+", Pattern.CASE_INSENSITIVE); @@ -44,13 +47,22 @@ public class OXConnector extends AbstractConnector { } @Override - public SearchResult searchByGeocode(String geocode, String guid, cgeoapplication app, CancellableHandler handler) { + public SearchResult searchByGeocode(String geocode, String guid, CancellableHandler handler) { final cgCache cache = OpenCachingApi.searchByGeoCode(geocode); if (cache == null) { return null; } - final SearchResult searchResult = new SearchResult(); - searchResult.addCache(cache); + final SearchResult searchResult = new SearchResult(cache); + return searchResult.filterSearchResults(false, false, Settings.getCacheType()); + } + + @Override + public SearchResult searchByCenter(Geopoint center) { + Collection<cgCache> caches = OpenCachingApi.searchByCenter(center); + if (caches == null) { + return null; + } + final SearchResult searchResult = new SearchResult(caches); return searchResult.filterSearchResults(false, false, Settings.getCacheType()); } } diff --git a/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java b/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java index 304429f..f06230e 100644 --- a/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java +++ b/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java @@ -5,6 +5,8 @@ import cgeo.geocaching.cgCache; import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; import cgeo.geocaching.files.GPX10Parser; +import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.geopoint.GeopointFormatter; import cgeo.geocaching.network.Network; import cgeo.geocaching.network.Parameters; import cgeo.geocaching.utils.CryptUtils; @@ -14,6 +16,7 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.http.HttpResponse; import java.util.Collection; +import java.util.Collections; import java.util.EnumSet; public class OpenCachingApi { @@ -21,28 +24,54 @@ public class OpenCachingApi { private static final String DEV_KEY = CryptUtils.rot13("PtqQnHo9RUTht3Np"); public static cgCache searchByGeoCode(final String geocode) { - final HttpResponse response = Network.getRequest("http://www.opencaching.com/api/geocache/" + geocode + ".gpx", new Parameters("Authorization", DEV_KEY)); + final HttpResponse response = Network.getRequest("http://www.opencaching.com/api/geocache/" + geocode + ".gpx", + new Parameters( + "Authorization", DEV_KEY, + "log_limit", "30", + "hint", "true", + "description", "html")); + final Collection<cgCache> caches = importCachesFromResponse(response, true); + if (CollectionUtils.isNotEmpty(caches)) { + return caches.iterator().next(); + } + return null; + } + + private static Collection<cgCache> importCachesFromResponse(final HttpResponse response, final boolean isDetailed) { if (response == null) { - return null; + return Collections.emptyList(); } Collection<cgCache> caches = null; try { caches = new GPX10Parser(StoredList.STANDARD_LIST_ID).parse(response.getEntity().getContent(), null); } catch (Exception e) { Log.e("Error importing from OpenCaching.com", e); + return Collections.emptyList(); } - if (caches != null && CollectionUtils.isNotEmpty(caches)) { - final cgCache cache = caches.iterator().next(); + for (cgCache cache : caches) { cache.setUpdated(System.currentTimeMillis()); - cache.setDetailedUpdate(cache.getUpdated()); - cache.setDetailed(true); + if (isDetailed) { + cache.setDetailedUpdate(cache.getUpdated()); + cache.setDetailed(true); + } // save full detailed caches cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); - return cache; } + return caches; + } - return null; + public static Collection<cgCache> searchByCenter(final Geopoint center) { + final HttpResponse response = Network.getRequest("http://www.opencaching.com/api/geocache/.gpx", + new Parameters( + "Authorization", DEV_KEY, + "log_limit", "0", + "hint", "false", + "description", "none", + "limit", "10", + "center", center.format(GeopointFormatter.Format.LAT_LON_DECDEGREE_COMMA))); + return importCachesFromResponse(response, false); } + } |