All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jp.nephy.kchroner.Config.kt Maven / Gradle / Ivy

package jp.nephy.kchroner

import ch.qos.logback.classic.Level
import com.google.gson.JsonObject
import io.ktor.client.engine.apache.Apache
import jp.nephy.jsonkt.*
import jp.nephy.penicillin.PenicillinClient
import jp.nephy.penicillin.core.emulation.EmulationMode
import kotlinx.coroutines.experimental.CommonPool
import java.nio.file.Paths

data class Config(override val json: JsonObject): JsonModel {
    companion object {
        private val configPath = Paths.get("config.json")

        fun load(): Config {
            return configPath.toFile().reader().use {
                it.readText().toJsonObject().parse()
            }
        }
    }

    val pluginsDirectory by json.byNullableLambda("plugins_directory") { Paths.get(string) }

    val wuiHost by json.byString("wui_host") { "127.0.0.1" }
    val wuiPort by json.byInt("wui_port") { 8080 }

    private val logLevelString by json.byString("log_level") { "info" }
    val logLevel by lazy { Level.toLevel(logLevelString, Level.INFO)!! }

    val accounts by json.byModel()

    data class Accounts(override val json: JsonObject): JsonModel {
        val twitter by json.byModelList()

        data class TwitterAccount(override val json: JsonObject): JsonModel {
            val name by json.byString
            private val ck by json.byString
            private val cs by json.byString
            private val at by json.byString
            private val ats by json.byString

            val client = client()
            val user by lazy {
                client.account.verifyCredentials().complete().result
            }

            fun client(mode: EmulationMode = EmulationMode.None): PenicillinClient {
                return PenicillinClient {
                    account {
                        application(ck, cs)
                        token(at, ats)
                    }
                    httpClient(Apache) {
                        engine {
                            dispatcher = CommonPool
                        }
                    }
                    emulate(mode)
                }
            }
        }
    }

    fun twitterAccount(name: String): Accounts.TwitterAccount {
        return accounts?.twitter?.find { it.name == name } ?: throw IllegalArgumentException("$name is not found in config.json.")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy