ai.platon.pulsar.skeleton.common.options.Converters.kt Maven / Gradle / Ivy
package ai.platon.pulsar.skeleton.common.options
import ai.platon.pulsar.common.SParser
import ai.platon.pulsar.common.browser.BrowserType
import ai.platon.pulsar.persist.metadata.FetchMode
import com.beust.jcommander.IStringConverter
import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.math.NumberUtils
import org.apache.commons.lang3.tuple.Pair
import java.awt.Dimension
import java.time.Duration
import java.time.Instant
import java.util.*
/**
* Created by vincent on 17-4-7.
* Copyright @ 2013-2023 Platon AI. All rights reserved
*/
class DurationConverter : IStringConverter {
override fun convert(value: String): Duration {
return SParser(value).duration
}
}
class InstantConverter : IStringConverter {
override fun convert(value: String): Instant {
return SParser(value).getInstant(Instant.EPOCH)
}
}
class PairConverter : IStringConverter> {
override fun convert(value: String): Pair {
val parts = value.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
return Pair.of(Integer.valueOf(parts[0]), Integer.valueOf(parts[1]))
}
}
class BrowserTypeConverter : IStringConverter {
override fun convert(value: String): BrowserType {
return BrowserType.fromString(value)
}
}
class FetchModeConverter : IStringConverter {
override fun convert(value: String): FetchMode {
return FetchMode.fromString(value)
}
}
enum class ItemExtractor {
DEFAULT, BOILERPIPE;
override fun toString(): String {
return name.lowercase(Locale.getDefault())
}
companion object {
fun fromString(s: String?): ItemExtractor {
if (s.isNullOrEmpty()) {
return DEFAULT
}
return try {
valueOf(s.uppercase(Locale.getDefault()))
} catch (e: Throwable) {
DEFAULT
}
}
}
}
class ItemExtractorConverter : IStringConverter {
override fun convert(value: String): ItemExtractor {
return ItemExtractor.fromString(value)
}
}
/**
* Created by vincent on 17-4-7.
* Copyright @ 2013-2023 Platon AI. All rights reserved
*/
class WeightedKeywordsConverter : IStringConverter