refactor(app): delegate Wikipedia API calls to shared KMP client
This commit is contained in:
@@ -1,85 +1,14 @@
|
||||
package app.klottr.net
|
||||
|
||||
import org.json.JSONObject
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import java.net.URLEncoder
|
||||
|
||||
/**
|
||||
* Android-adapter som behåller befintligt API men delegerar till delad KMP-klient.
|
||||
* (Stegvis migrering: inga andra anrop behöver ändras.)
|
||||
*/
|
||||
object Wikipedia {
|
||||
|
||||
suspend fun getNearby(lat: Double, lon: Double, radiusMeters: Int): List<WikipediaPage> {
|
||||
val coord = "${lat}|${lon}"
|
||||
val params = mapOf(
|
||||
"action" to "query",
|
||||
"list" to "geosearch",
|
||||
"gscoord" to coord,
|
||||
"gsradius" to radiusMeters.toString(),
|
||||
"gslimit" to "50",
|
||||
"format" to "json",
|
||||
"formatversion" to "2"
|
||||
)
|
||||
val query = params.entries.joinToString("&") { (k, v) ->
|
||||
"${URLEncoder.encode(k, "UTF-8")}=${URLEncoder.encode(v, "UTF-8")}"
|
||||
}
|
||||
val url = URL("https://sv.wikipedia.org/w/api.php?$query")
|
||||
suspend fun getNearby(lat: Double, lon: Double, radiusMeters: Int): List<WikipediaPage> =
|
||||
WikipediaShared.getNearby(lat, lon, radiusMeters)
|
||||
|
||||
val conn = (url.openConnection() as HttpURLConnection).apply {
|
||||
requestMethod = "GET"
|
||||
connectTimeout = 8000
|
||||
readTimeout = 8000
|
||||
setRequestProperty("User-Agent", "Yorlik/0.0.5 (Android)")
|
||||
}
|
||||
conn.useCaches = false
|
||||
|
||||
val code = conn.responseCode
|
||||
if (code != 200) return emptyList()
|
||||
|
||||
val body = conn.inputStream.bufferedReader(Charsets.UTF_8).use { it.readText() }
|
||||
val root = JSONObject(body)
|
||||
val arr = root.optJSONObject("query")?.optJSONArray("geosearch") ?: return emptyList()
|
||||
|
||||
val out = ArrayList<WikipediaPage>(arr.length())
|
||||
for (i in 0 until arr.length()) {
|
||||
val o = arr.optJSONObject(i) ?: continue
|
||||
val id = o.optLong("pageid")
|
||||
val t = o.optString("title")
|
||||
val a = o.optDouble("lat")
|
||||
val g = o.optDouble("lon")
|
||||
out.add(WikipediaPage(id, t, a, g))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
suspend fun getExtract(pageId: Long): WikipediaDetail {
|
||||
val params = mapOf(
|
||||
"action" to "query",
|
||||
"prop" to "extracts",
|
||||
"exintro" to "1",
|
||||
"explaintext" to "1",
|
||||
"format" to "json",
|
||||
"formatversion" to "2",
|
||||
"pageids" to pageId.toString()
|
||||
)
|
||||
val query = params.entries.joinToString("&") { (k, v) ->
|
||||
"${URLEncoder.encode(k, "UTF-8")}=${URLEncoder.encode(v, "UTF-8")}"
|
||||
}
|
||||
val url = URL("https://sv.wikipedia.org/w/api.php?$query")
|
||||
|
||||
val conn = (url.openConnection() as HttpURLConnection).apply {
|
||||
requestMethod = "GET"
|
||||
connectTimeout = 8000
|
||||
readTimeout = 8000
|
||||
setRequestProperty("User-Agent", "Yorlik/0.0.5 (Android)")
|
||||
}
|
||||
val code = conn.responseCode
|
||||
if (code != 200) return WikipediaDetail(pageId, "", "")
|
||||
|
||||
val body = conn.inputStream.bufferedReader(Charsets.UTF_8).use { it.readText() }
|
||||
val root = JSONObject(body)
|
||||
val pages = root.optJSONObject("query")?.optJSONArray("pages")
|
||||
val p = if (pages != null && pages.length() > 0) pages.optJSONObject(0) else null
|
||||
val title = p?.optString("title") ?: ""
|
||||
val extract = p?.optString("extract") ?: ""
|
||||
return WikipediaDetail(pageId, title, extract)
|
||||
}
|
||||
suspend fun getExtract(pageId: Long): WikipediaDetail =
|
||||
WikipediaShared.getExtract(pageId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user