Skip to content

Instantly share code, notes, and snippets.

@MrPowerGamerBR
Last active August 20, 2017 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrPowerGamerBR/64517980fae1efe6925e608ed2e69d4d to your computer and use it in GitHub Desktop.
Save MrPowerGamerBR/64517980fae1efe6925e608ed2e69d4d to your computer and use it in GitHub Desktop.
Kotinator: A small Akiantor (reversed engineered) API implementation in Kotlin
package com.mrpowergamerbr.akinator
import com.github.kevinsawicki.http.HttpRequest
import com.github.salomonbrys.kotson.array
import com.github.salomonbrys.kotson.get
import com.github.salomonbrys.kotson.int
import com.github.salomonbrys.kotson.long
import com.github.salomonbrys.kotson.obj
import com.github.salomonbrys.kotson.string
import com.google.gson.JsonParser
import org.json.XML
fun main(args: Array<String>) {
val response = HttpRequest.get("http://api-pt3.akinator.com/ws/new_session.php?base=0&partner=410&premium=0&player=Android-Phone&uid=6fe3a92130c49446&do_geoloc=1&prio=0&constraint=ETAT%3C%3E'AV'&channel=0&only_minibase=0")
.body()
val xmlJSONObj = XML.toJSONObject(response);
val jsonPrettyPrintString = xmlJSONObj.toString(4);
val jsonSession = JsonParser().parse(jsonPrettyPrintString).obj["RESULT"]
val identification = jsonSession["PARAMETERS"]["IDENTIFICATION"].obj
val channel = identification["CHANNEL"].long
val session = identification["SESSION"].long
val signature = identification["SIGNATURE"].long
val stepInfo = jsonSession["PARAMETERS"]["STEP_INFORMATION"]
var question = stepInfo["QUESTION"].string
var progression = stepInfo["PROGRESSION"].int
var step = stepInfo["STEP"].int
var answers = stepInfo["ANSWERS"]["ANSWER"].array
println(question)
println("Progresso: " + progression)
var idx = 0
for (answer in answers) {
println("$idx. " + answer.string)
idx++
}
while (true) {
println("Escolha o número...")
val line = readLine()
val answer = line!!.toInt()
val response = HttpRequest.get("http://api-pt3.akinator.com/ws/answer.php?base=0&channel=$channel&session=$session&signature=$signature&step=$step&answer=$answer")
.body()
val xmlJSONObj = XML.toJSONObject(response);
val jsonPrettyPrintString = xmlJSONObj.toString(4);
val jsonAnswer = JsonParser().parse(jsonPrettyPrintString).obj["RESULT"]["PARAMETERS"]
var question = jsonAnswer["QUESTION"].string
var progression = jsonAnswer["PROGRESSION"].int
step = jsonAnswer["STEP"].int
if (95 >= progression) {
println(question)
println("Progresso: " + progression)
var idx = 0
for (answer in answers) {
println("$idx. " + answer.string)
idx++
}
} else {
val response = HttpRequest.get("http://api-pt3.akinator.com/ws/list.php?base=0&channel=$channel&session=$session&signature=$signature&step=$step&size=1&max_pic_width=360&max_pic_height=640&mode_question=0")
.body()
val xmlJSONObj = XML.toJSONObject(response);
val jsonPrettyPrintString = xmlJSONObj.toString(4);
val jsonAnswer = JsonParser().parse(jsonPrettyPrintString).obj["RESULT"]["PARAMETERS"]["ELEMENTS"]["ELEMENT"]
println("Foto: " + jsonAnswer["ABSOLUTE_PICTURE_PATH"].string)
println("Descrição: " + jsonAnswer["DESCRIPTION"].string)
println("Nome: " + jsonAnswer["NAME"].string)
println("Ranking: " + jsonAnswer["RANKING"].string)
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment