Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import io.ktor.client.*
- import io.ktor.client.engine.cio.*
- import io.ktor.client.features.*
- import io.ktor.client.request.*
- import io.ktor.client.statement.*
- import kotlinx.coroutines.runBlocking
- private inline fun readIntUntilOk(message: String, additionalChecks: (Int) -> Boolean): Int {
- while (true) {
- print(message)
- readLine()
- ?.trim()
- ?.toIntOrNull()
- ?.takeIf(additionalChecks)
- ?.let { return it }
- ?: println("Не пон")
- }
- }
- fun main() = runBlocking {
- val y = readIntUntilOk("Введи номер года: ") { it in 1000..9999 }
- val m = readIntUntilOk("Введи номер месяца (1 - 12): ") { it in 1..12 }
- val d = readIntUntilOk("Введи номер дня (число в календаре, > 0 и < 32): ") { it in 1..31 }
- val client = HttpClient(CIO) {
- expectSuccess = false
- }
- println("Жди...")
- val response = client.get<HttpResponse>(
- "https://isdayoff.ru/$y${"$m".padStart(2, '0')}${"$d".padStart(2, '0')}"
- )
- when (response.status.value) {
- 200 -> when (response.readText().toIntOrNull()) {
- 0 -> "Рабочий"
- 1 -> "Выходной"
- 100 -> "Некорректная дата"
- else -> "Хз"
- }
- 400 -> "Некорректная дата"
- else -> "Ошибка сервера"
- }.let(::print)
- }
Add Comment
Please, Sign In to add comment