2016-12-20 20:28:45 +00:00
|
|
|
package telegra.ph
|
|
|
|
|
|
|
|
import com.afollestad.bridge.Bridge
|
|
|
|
import org.json.JSONArray
|
|
|
|
import org.json.JSONObject
|
|
|
|
|
|
|
|
class Api {
|
|
|
|
|
|
|
|
private val ApiBase = "https://api.telegra.ph/"
|
|
|
|
|
2016-12-21 19:57:36 +00:00
|
|
|
fun getPage(id: String?, accessToken: String?, callback: (page: Page?) -> Unit) {
|
|
|
|
Bridge.get("${ApiBase}getPage/$id?return_content=true" + if (accessToken != null) "&access_token=$accessToken" else "").asJsonObject { response, jsonObject, bridgeException ->
|
2016-12-20 21:12:08 +00:00
|
|
|
if (jsonObject != null) callback(jsonObject.parsePage())
|
|
|
|
else callback(null)
|
2016-12-20 20:28:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 19:57:36 +00:00
|
|
|
fun createPage(accessToken: String?, content: String?, title: String?, callback: (Page?) -> Unit) {
|
|
|
|
Bridge.get("${ApiBase}createPage?access_token=$accessToken&title=%s&content=$content&return_content=true", title).asJsonObject { response, jsonObject, bridgeException ->
|
|
|
|
if (jsonObject != null) callback(jsonObject.parsePage())
|
|
|
|
else callback(null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun editPage(accessToken: String?, path: String?, content: String?, title: String?, callback: (Page?) -> Unit) {
|
|
|
|
Bridge.get("${ApiBase}editPage/$path?access_token=$accessToken&title=%s&content=$content&return_content=true", title).asJsonObject { response, jsonObject, bridgeException ->
|
|
|
|
if (jsonObject != null) callback(jsonObject.parsePage())
|
|
|
|
else callback(null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun createAccount(callback: (accessToken: String?) -> Unit) {
|
|
|
|
Bridge.get("${ApiBase}createAccount?short_name=teleposter").asJsonObject { response, jsonObject, bridgeException ->
|
|
|
|
if (jsonObject != null) callback(jsonObject.optJSONObject("result")?.optString("access_token"))
|
|
|
|
else callback(null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 20:28:45 +00:00
|
|
|
private fun JSONObject.parsePage(): Page? {
|
|
|
|
val result: Page = Page()
|
|
|
|
if (optBoolean("ok", false)) {
|
2016-12-20 21:12:08 +00:00
|
|
|
optJSONObject("result")?.let {
|
2016-12-20 20:28:45 +00:00
|
|
|
result.path = it.optString("path", "")
|
|
|
|
result.url = it.optString("url", "")
|
|
|
|
result.title = it.optString("title", "")
|
|
|
|
result.description = it.optString("description", "")
|
|
|
|
result.author_name = it.optString("author_name", "")
|
|
|
|
result.author_url = it.optString("author_url", "")
|
|
|
|
result.image_url = it.optString("image_url", "")
|
|
|
|
it.optJSONArray("content")?.parseContent(result)
|
|
|
|
result.views = it.optInt("views", 0)
|
|
|
|
result.can_edit = it.optBoolean("can_edit", false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun JSONArray.parseContent(result: Page) {
|
2016-12-21 08:34:25 +00:00
|
|
|
for (i in 0 until length()) {
|
|
|
|
optJSONObject(i)?.let {
|
|
|
|
result.content += "<${it.optString("tag", "")}"
|
|
|
|
it.optJSONObject("attrs")?.let {
|
|
|
|
for (key in it.keys()) {
|
|
|
|
result.content += " $key=\"${it.optString(key, "")}\""
|
2016-12-20 20:28:45 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-21 08:34:25 +00:00
|
|
|
result.content += ">"
|
|
|
|
it.optJSONArray("children")?.parseContent(result)
|
|
|
|
result.content += "</${it.optString("tag", "")}>"
|
|
|
|
}
|
|
|
|
if (optJSONObject(i) == null) optString(i)?.let {
|
|
|
|
result.content += it
|
2016-12-20 20:28:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class Page(
|
|
|
|
var path: String = "",
|
|
|
|
var url: String = "",
|
|
|
|
var title: String = "",
|
|
|
|
var description: String = "",
|
|
|
|
var author_name: String = "",
|
|
|
|
var author_url: String = "",
|
|
|
|
var image_url: String = "",
|
|
|
|
var content: String = "",
|
|
|
|
var views: Int = 0,
|
|
|
|
var can_edit: Boolean = false
|
|
|
|
)
|