|
| 1 | +package fr.free.nrw.commons.mwapi; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.LinkedHashSet; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +import javax.inject.Inject; |
| 12 | +import javax.inject.Named; |
| 13 | + |
| 14 | +import fr.free.nrw.commons.mwapi.model.ApiResponse; |
| 15 | +import fr.free.nrw.commons.mwapi.model.Page; |
| 16 | +import fr.free.nrw.commons.mwapi.model.PageCategory; |
| 17 | +import io.reactivex.Single; |
| 18 | +import okhttp3.HttpUrl; |
| 19 | +import okhttp3.OkHttpClient; |
| 20 | +import okhttp3.Request; |
| 21 | +import okhttp3.Response; |
| 22 | +import okhttp3.ResponseBody; |
| 23 | +import timber.log.Timber; |
| 24 | + |
| 25 | +/** |
| 26 | + * Uses the OkHttp library to implement calls to the Commons MediaWiki API to match GPS coordinates |
| 27 | + * with nearby Commons categories. Parses the results using GSON to obtain a list of relevant |
| 28 | + * categories. Note: that caller is responsible for executing the request() method on a background |
| 29 | + * thread. |
| 30 | + */ |
| 31 | +public class CategoryApi { |
| 32 | + |
| 33 | + private final OkHttpClient okHttpClient; |
| 34 | + private final HttpUrl mwUrl; |
| 35 | + private final Gson gson; |
| 36 | + |
| 37 | + @Inject |
| 38 | + public CategoryApi(OkHttpClient okHttpClient, Gson gson, |
| 39 | + @Named("commons_mediawiki_url") HttpUrl mwUrl) { |
| 40 | + this.okHttpClient = okHttpClient; |
| 41 | + this.mwUrl = mwUrl; |
| 42 | + this.gson = gson; |
| 43 | + } |
| 44 | + |
| 45 | + public Single<List<String>> request(String coords) { |
| 46 | + return Single.fromCallable(() -> { |
| 47 | + HttpUrl apiUrl = buildUrl(coords); |
| 48 | + Timber.d("URL: %s", apiUrl.toString()); |
| 49 | + |
| 50 | + Request request = new Request.Builder().get().url(apiUrl).build(); |
| 51 | + Response response = okHttpClient.newCall(request).execute(); |
| 52 | + ResponseBody body = response.body(); |
| 53 | + if (body == null) { |
| 54 | + return Collections.emptyList(); |
| 55 | + } |
| 56 | + |
| 57 | + ApiResponse apiResponse = gson.fromJson(body.charStream(), ApiResponse.class); |
| 58 | + Set<String> categories = new LinkedHashSet<>(); |
| 59 | + if (apiResponse != null && apiResponse.hasPages()) { |
| 60 | + for (Page page : apiResponse.query.pages) { |
| 61 | + for (PageCategory category : page.getCategories()) { |
| 62 | + categories.add(category.withoutPrefix()); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + return new ArrayList<>(categories); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Builds URL with image coords for MediaWiki API calls |
| 72 | + * Example URL: https://commons.wikimedia.org/w/api.php?action=query&prop=categories|coordinates|pageprops&format=json&clshow=!hidden&coprop=type|name|dim|country|region|globe&codistancefrompoint=38.11386944444445|13.356263888888888&generator=geosearch&redirects=&ggscoord=38.11386944444445|1.356263888888888&ggsradius=100&ggslimit=10&ggsnamespace=6&ggsprop=type|name|dim|country|region|globe&ggsprimary=all&formatversion=2 |
| 73 | + * |
| 74 | + * @param coords Coordinates to build query with |
| 75 | + * @return URL for API query |
| 76 | + */ |
| 77 | + private HttpUrl buildUrl(String coords) { |
| 78 | + return mwUrl.newBuilder() |
| 79 | + .addPathSegment("w") |
| 80 | + .addPathSegment("api.php") |
| 81 | + .addQueryParameter("action", "query") |
| 82 | + .addQueryParameter("prop", "categories|coordinates|pageprops") |
| 83 | + .addQueryParameter("format", "json") |
| 84 | + .addQueryParameter("clshow", "!hidden") |
| 85 | + .addQueryParameter("coprop", "type|name|dim|country|region|globe") |
| 86 | + .addQueryParameter("codistancefrompoint", coords) |
| 87 | + .addQueryParameter("generator", "geosearch") |
| 88 | + .addQueryParameter("ggscoord", coords) |
| 89 | + .addQueryParameter("ggsradius", "10000") |
| 90 | + .addQueryParameter("ggslimit", "10") |
| 91 | + .addQueryParameter("ggsnamespace", "6") |
| 92 | + .addQueryParameter("ggsprop", "type|name|dim|country|region|globe") |
| 93 | + .addQueryParameter("ggsprimary", "all") |
| 94 | + .addQueryParameter("formatversion", "2") |
| 95 | + .build(); |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | + |
0 commit comments