Skip to content

Instantly share code, notes, and snippets.

@antonis
Created March 21, 2019 11:23
Show Gist options
  • Select an option

  • Save antonis/4af8b14a36f173b4e8e1390dbb191e99 to your computer and use it in GitHub Desktop.

Select an option

Save antonis/4af8b14a36f173b4e8e1390dbb191e99 to your computer and use it in GitHub Desktop.
import com.google.gson.*
fun wikipedia(keyword: String): String {
val url = "https://en.wikipedia.org/w/api.php" +
"?action=query&format=json&prop=extracts" +
"&exsectionformat=plain&exsentences=2&explaintext=1" +
"&titles=$keyword"
val json = java.net.URL(url).readText()
val jsonObject = Gson().fromJson<JsonObject>(json, JsonObject::class.java)
val pages = jsonObject["query"].asJsonObject["pages"].asJsonObject
val extract = pages[pages.keySet().first()].asJsonObject["extract"]
return extract.asString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment