Load codes/symbols/currencies on init

This commit is contained in:
Erik C. Thauvin 2022-08-01 11:05:45 -07:00
parent bcdd8c10cf
commit d7d1f09f28
2 changed files with 3 additions and 34 deletions

View file

@ -37,7 +37,6 @@ import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice
import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.Utils.sendList import net.thauvin.erik.mobibot.Utils.sendList
import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.sendMessage
import net.thauvin.erik.mobibot.Utils.today
import org.json.JSONObject import org.json.JSONObject
import org.pircbotx.hooks.types.GenericMessageEvent import org.pircbotx.hooks.types.GenericMessageEvent
import org.slf4j.Logger import org.slf4j.Logger
@ -51,14 +50,6 @@ class CryptoPrices : ThreadedModule() {
private val logger: Logger = LoggerFactory.getLogger(CryptoPrices::class.java) private val logger: Logger = LoggerFactory.getLogger(CryptoPrices::class.java)
override val name = "CryptoPrices" override val name = "CryptoPrices"
override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
synchronized(this) {
if (pubDate != today()) {
CURRENCIES.clear()
}
}
super.commandResponse(channel, cmd, args, event)
}
/** /**
* Returns the cryptocurrency market price from * Returns the cryptocurrency market price from
@ -111,9 +102,6 @@ class CryptoPrices : ThreadedModule() {
// Currency codes keyword // Currency codes keyword
private const val CURRENCY_CODES_KEYWORD = "codes" private const val CURRENCY_CODES_KEYWORD = "codes"
// Last currencies retrieval date
private var pubDate = ""
/** /**
* Get current market price. * Get current market price.
*/ */
@ -141,14 +129,10 @@ class CryptoPrices : ThreadedModule() {
try { try {
val json = JSONObject(CryptoPrice.apiCall(listOf("currencies"))) val json = JSONObject(CryptoPrice.apiCall(listOf("currencies")))
val data = json.getJSONArray("data") val data = json.getJSONArray("data")
if (CURRENCIES.isNotEmpty()) {
CURRENCIES.clear()
}
for (i in 0 until data.length()) { for (i in 0 until data.length()) {
val d = data.getJSONObject(i) val d = data.getJSONObject(i)
CURRENCIES[d.getString("id")] = d.getString("name") CURRENCIES[d.getString("id")] = d.getString("name")
} }
pubDate = today()
} catch (e: CryptoException) { } catch (e: CryptoException) {
throw ModuleException( throw ModuleException(
"loadCurrencies(): CE", "loadCurrencies(): CE",
@ -171,5 +155,6 @@ class CryptoPrices : ThreadedModule() {
add("To list the supported currencies:") add("To list the supported currencies:")
add(helpFormat("%c $CRYPTO_CMD $CURRENCY_CODES_KEYWORD")) add(helpFormat("%c $CRYPTO_CMD $CURRENCY_CODES_KEYWORD"))
} }
loadCurrencies()
} }
} }

View file

@ -37,7 +37,6 @@ import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.Utils.reader import net.thauvin.erik.mobibot.Utils.reader
import net.thauvin.erik.mobibot.Utils.sendList import net.thauvin.erik.mobibot.Utils.sendList
import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.sendMessage
import net.thauvin.erik.mobibot.Utils.today
import net.thauvin.erik.mobibot.msg.ErrorMessage import net.thauvin.erik.mobibot.msg.ErrorMessage
import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.PublicMessage import net.thauvin.erik.mobibot.msg.PublicMessage
@ -47,7 +46,7 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL
import java.util.* import java.util.TreeMap
/** /**
@ -58,15 +57,6 @@ class CurrencyConverter : ThreadedModule() {
override val name = "CurrencyConverter" override val name = "CurrencyConverter"
override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
synchronized(this) {
if (pubDate != today()) {
SYMBOLS.clear()
}
}
super.commandResponse(channel, cmd, args, event)
}
// Reload currency codes // Reload currency codes
private fun reload() { private fun reload() {
if (SYMBOLS.isEmpty()) { if (SYMBOLS.isEmpty()) {
@ -137,9 +127,6 @@ class CurrencyConverter : ThreadedModule() {
// Currency symbols // Currency symbols
private val SYMBOLS: TreeMap<String, String> = TreeMap() private val SYMBOLS: TreeMap<String, String> = TreeMap()
// Last currency retrieval date
private var pubDate = ""
/** /**
* Converts from a currency to another. * Converts from a currency to another.
*/ */
@ -186,13 +173,9 @@ class CurrencyConverter : ThreadedModule() {
val json = JSONObject(url.reader()) val json = JSONObject(url.reader())
if (json.getBoolean("success")) { if (json.getBoolean("success")) {
val symbols = json.getJSONObject("symbols") val symbols = json.getJSONObject("symbols")
if (SYMBOLS.isNotEmpty()) {
SYMBOLS.clear()
}
for (key in symbols.keys()) { for (key in symbols.keys()) {
SYMBOLS[key] = symbols.getJSONObject(key).getString("description") SYMBOLS[key] = symbols.getJSONObject(key).getString("description")
} }
pubDate = today()
} }
} catch (e: IOException) { } catch (e: IOException) {
throw ModuleException( throw ModuleException(
@ -206,5 +189,6 @@ class CurrencyConverter : ThreadedModule() {
init { init {
commands.add(CURRENCY_CMD) commands.add(CURRENCY_CMD)
loadSymbols()
} }
} }