Parcourir la source

added weather images

Vadik Sirekanyan il y a 5 ans
Parent
commit
aaabfcfd06

+ 9 - 3
src/main/kotlin/com/sirekanyan/andersrobot/AndersRobot.kt

@@ -33,15 +33,21 @@ class AndersRobot : DefaultAbsSender(DefaultBotOptions()), LongPollingBot {
         val delCityCommand = getDelCityCommand(message.text)
         when {
             !cityCommand.isNullOrEmpty() -> {
-                val temperature = weather.getTemperature(cityCommand, accuracy)
+                val temperature = weather.getTemperature(cityCommand)
                 if (temperature == null) {
                     sendText(chatId, "Не знаю такого города")
                 } else {
-                    sendText(chatId, listOf(temperature).joinToString("\n"))
+                    val text = temperature.format(accuracy)
+                    val icon = temperature.findImageFile()
+                    if (icon == null) {
+                        sendText(chatId, text)
+                    } else {
+                        sendPhoto(chatId, icon, text)
+                    }
                 }
             }
             !addCityCommand.isNullOrEmpty() -> {
-                val temperature = weather.getTemperature(addCityCommand, accuracy)
+                val temperature = weather.getTemperature(addCityCommand)
                 if (temperature == null) {
                     sendText(chatId, "Не знаю такого города")
                 } else {

+ 25 - 2
src/main/kotlin/com/sirekanyan/andersrobot/api/Weather.kt

@@ -1,14 +1,37 @@
 package com.sirekanyan.andersrobot.api
 
 import kotlinx.serialization.Serializable
+import java.io.File
 
 @Serializable
-data class Weather(val main: MainInfo, val name: String, val sys: Sys) {
+data class Weather(
+    val main: MainInfo,
+    val name: String,
+    val sys: System,
+    val weather: List<Condition>
+) {
 
     @Serializable
     data class MainInfo(val temp: Double)
 
     @Serializable
-    data class Sys(val country: String)
+    data class System(val country: String)
+
+    @Serializable
+    data class Condition(val id: Int)
+
+    fun findImageFile(): File? {
+        val w = weather.firstOrNull() ?: return null
+        return File("data/${w.id}.png").takeIf { it.exists() }
+    }
+
+    fun format(accuracy: Int): String =
+        formatTemperature(accuracy) + " — " + formatCity()
+
+    private fun formatTemperature(accuracy: Int): String =
+        "%.${accuracy}f°C".format(main.temp)
+
+    private fun formatCity(): String =
+        "$name (${sys.country})"
 
 }

+ 7 - 18
src/main/kotlin/com/sirekanyan/andersrobot/api/WeatherApi.kt

@@ -15,22 +15,17 @@ class WeatherApi {
 
     private val httpClient = HttpClient()
     private val apiKey = Config[WEATHER_API_KEY]
+    private val comparator = compareBy<Weather> { it.sys.country.toLowerCase().replace("ru", "aa") }.thenBy { it.name }
 
-    fun getTemperature(city: String, accuracy: Int): String? = runBlocking {
-        val weather = getWeather(city)
-        weather?.let {
-            formatWeather(weather, accuracy) + " — " + formatCity(weather)
-        }
+    fun getTemperature(city: String): Weather? = runBlocking {
+        getWeather(city)
     }
 
     fun getTemperatures(cities: List<String>, accuracy: Int): List<String> = runBlocking {
-        cities.associateWith { city ->
-            async { getWeather(city) }
-        }.map { (city, weather) ->
-            weather.await()?.let {
-                formatWeather(it, accuracy) + " — $city"
-            }
-        }.filterNotNull()
+        cities.map { city -> async { getWeather(city) } }
+            .mapNotNull { it.await() }
+            .sortedWith(comparator)
+            .map { it.format(accuracy) }
     }
 
     private suspend fun getWeather(city: String): Weather? =
@@ -48,10 +43,4 @@ class WeatherApi {
             null
         }
 
-    private fun formatWeather(weather: Weather, accuracy: Int): String =
-        "%.${accuracy}f°C".format(weather.main.temp)
-
-    private fun formatCity(weather: Weather): String =
-        "${weather.name} (${weather.sys.country})"
-
 }

+ 6 - 0
src/main/kotlin/com/sirekanyan/andersrobot/extensions/AbsSender.kt

@@ -1,8 +1,14 @@
 package com.sirekanyan.andersrobot.extensions
 
 import org.telegram.telegrambots.meta.api.methods.send.SendMessage
+import org.telegram.telegrambots.meta.api.methods.send.SendPhoto
 import org.telegram.telegrambots.meta.api.objects.Message
 import org.telegram.telegrambots.meta.bots.AbsSender
+import java.io.File
 
 fun AbsSender.sendText(chatId: Long, text: String): Message =
     execute(SendMessage(chatId, text))
+
+fun AbsSender.sendPhoto(chatId: Long, file: File, caption: String) {
+    execute(SendPhoto().setChatId(chatId).setPhoto(file).setCaption(caption))
+}