|
| 1 | +package fr.free.nrw.commons.mwapi |
| 2 | + |
| 3 | +import com.google.gson.Gson |
| 4 | +import fr.free.nrw.commons.BuildConfig |
| 5 | +import fr.free.nrw.commons.category.CATEGORY_PREFIX |
| 6 | +import fr.free.nrw.commons.category.CategoryItem |
| 7 | +import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse |
| 8 | +import io.reactivex.Single |
| 9 | +import okhttp3.HttpUrl |
| 10 | +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull |
| 11 | +import okhttp3.OkHttpClient |
| 12 | +import okhttp3.Request |
| 13 | +import timber.log.Timber |
| 14 | +import javax.inject.Inject |
| 15 | + |
| 16 | +/** |
| 17 | + * Uses the OkHttp library to implement calls to the Commons MediaWiki API to match GPS coordinates |
| 18 | + * with nearby Commons categories. Parses the results using GSON to obtain a list of relevant |
| 19 | + * categories. Note: that caller is responsible for executing the request() method on a background |
| 20 | + * thread. |
| 21 | + */ |
| 22 | +class CategoryApi @Inject constructor( |
| 23 | + private val okHttpClient: OkHttpClient, |
| 24 | + private val gson: Gson |
| 25 | +) { |
| 26 | + private val apiUrl : HttpUrl by lazy { BuildConfig.WIKIMEDIA_API_HOST.toHttpUrlOrNull()!! } |
| 27 | + |
| 28 | + fun request(coords: String): Single<List<CategoryItem>> = Single.fromCallable { |
| 29 | + val apiUrl = buildUrl(coords) |
| 30 | + Timber.d("URL: %s", apiUrl.toString()) |
| 31 | + |
| 32 | + val request: Request = Request.Builder().get().url(apiUrl).build() |
| 33 | + val response = okHttpClient.newCall(request).execute() |
| 34 | + val body = response.body ?: return@fromCallable emptyList<CategoryItem>() |
| 35 | + |
| 36 | + val apiResponse = gson.fromJson(body.charStream(), MwQueryResponse::class.java) |
| 37 | + val categories: MutableSet<CategoryItem> = mutableSetOf() |
| 38 | + if (apiResponse?.query() != null && apiResponse.query()!!.pages() != null) { |
| 39 | + for (page in apiResponse.query()!!.pages()!!) { |
| 40 | + if (page.categories() != null) { |
| 41 | + for (category in page.categories()!!) { |
| 42 | + categories.add( |
| 43 | + CategoryItem( |
| 44 | + name = category.title().replace(CATEGORY_PREFIX, ""), |
| 45 | + description = "", |
| 46 | + thumbnail = "", |
| 47 | + isSelected = false |
| 48 | + ) |
| 49 | + ) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + ArrayList<CategoryItem>(categories) |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Builds URL with image coords for MediaWiki API calls |
| 59 | + * 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 |
| 60 | + * |
| 61 | + * @param coords Coordinates to build query with |
| 62 | + * @return URL for API query |
| 63 | + */ |
| 64 | + private fun buildUrl(coords: String): HttpUrl = apiUrl.newBuilder() |
| 65 | + .addQueryParameter("action", "query") |
| 66 | + .addQueryParameter("prop", "categories|coordinates|pageprops") |
| 67 | + .addQueryParameter("format", "json") |
| 68 | + .addQueryParameter("clshow", "!hidden") |
| 69 | + .addQueryParameter("coprop", "type|name|dim|country|region|globe") |
| 70 | + .addQueryParameter("codistancefrompoint", coords) |
| 71 | + .addQueryParameter("generator", "geosearch") |
| 72 | + .addQueryParameter("ggscoord", coords) |
| 73 | + .addQueryParameter("ggsradius", "10000") |
| 74 | + .addQueryParameter("ggslimit", "10") |
| 75 | + .addQueryParameter("ggsnamespace", "6") |
| 76 | + .addQueryParameter("ggsprop", "type|name|dim|country|region|globe") |
| 77 | + .addQueryParameter("ggsprimary", "all") |
| 78 | + .addQueryParameter("formatversion", "2") |
| 79 | + .build() |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | + |
0 commit comments