Load codes/symbols/currencies on init
This commit is contained in:
parent
bcdd8c10cf
commit
d7d1f09f28
2 changed files with 3 additions and 34 deletions
|
@ -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.sendList
|
||||
import net.thauvin.erik.mobibot.Utils.sendMessage
|
||||
import net.thauvin.erik.mobibot.Utils.today
|
||||
import org.json.JSONObject
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent
|
||||
import org.slf4j.Logger
|
||||
|
@ -51,14 +50,6 @@ class CryptoPrices : ThreadedModule() {
|
|||
private val logger: Logger = LoggerFactory.getLogger(CryptoPrices::class.java)
|
||||
|
||||
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
|
||||
|
@ -111,9 +102,6 @@ class CryptoPrices : ThreadedModule() {
|
|||
// Currency codes keyword
|
||||
private const val CURRENCY_CODES_KEYWORD = "codes"
|
||||
|
||||
// Last currencies retrieval date
|
||||
private var pubDate = ""
|
||||
|
||||
/**
|
||||
* Get current market price.
|
||||
*/
|
||||
|
@ -141,14 +129,10 @@ class CryptoPrices : ThreadedModule() {
|
|||
try {
|
||||
val json = JSONObject(CryptoPrice.apiCall(listOf("currencies")))
|
||||
val data = json.getJSONArray("data")
|
||||
if (CURRENCIES.isNotEmpty()) {
|
||||
CURRENCIES.clear()
|
||||
}
|
||||
for (i in 0 until data.length()) {
|
||||
val d = data.getJSONObject(i)
|
||||
CURRENCIES[d.getString("id")] = d.getString("name")
|
||||
}
|
||||
pubDate = today()
|
||||
} catch (e: CryptoException) {
|
||||
throw ModuleException(
|
||||
"loadCurrencies(): CE",
|
||||
|
@ -171,5 +155,6 @@ class CryptoPrices : ThreadedModule() {
|
|||
add("To list the supported currencies:")
|
||||
add(helpFormat("%c $CRYPTO_CMD $CURRENCY_CODES_KEYWORD"))
|
||||
}
|
||||
loadCurrencies()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,6 @@ import net.thauvin.erik.mobibot.Utils.helpFormat
|
|||
import net.thauvin.erik.mobibot.Utils.reader
|
||||
import net.thauvin.erik.mobibot.Utils.sendList
|
||||
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.Message
|
||||
import net.thauvin.erik.mobibot.msg.PublicMessage
|
||||
|
@ -47,7 +46,7 @@ import org.slf4j.Logger
|
|||
import org.slf4j.LoggerFactory
|
||||
import java.io.IOException
|
||||
import java.net.URL
|
||||
import java.util.*
|
||||
import java.util.TreeMap
|
||||
|
||||
|
||||
/**
|
||||
|
@ -58,15 +57,6 @@ class CurrencyConverter : ThreadedModule() {
|
|||
|
||||
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
|
||||
private fun reload() {
|
||||
if (SYMBOLS.isEmpty()) {
|
||||
|
@ -137,9 +127,6 @@ class CurrencyConverter : ThreadedModule() {
|
|||
// Currency symbols
|
||||
private val SYMBOLS: TreeMap<String, String> = TreeMap()
|
||||
|
||||
// Last currency retrieval date
|
||||
private var pubDate = ""
|
||||
|
||||
/**
|
||||
* Converts from a currency to another.
|
||||
*/
|
||||
|
@ -186,13 +173,9 @@ class CurrencyConverter : ThreadedModule() {
|
|||
val json = JSONObject(url.reader())
|
||||
if (json.getBoolean("success")) {
|
||||
val symbols = json.getJSONObject("symbols")
|
||||
if (SYMBOLS.isNotEmpty()) {
|
||||
SYMBOLS.clear()
|
||||
}
|
||||
for (key in symbols.keys()) {
|
||||
SYMBOLS[key] = symbols.getJSONObject(key).getString("description")
|
||||
}
|
||||
pubDate = today()
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
throw ModuleException(
|
||||
|
@ -206,5 +189,6 @@ class CurrencyConverter : ThreadedModule() {
|
|||
|
||||
init {
|
||||
commands.add(CURRENCY_CMD)
|
||||
loadSymbols()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue