File tree Expand file tree Collapse file tree 4 files changed +69
-0
lines changed
main/kotlin/by/jprof/telegram/bot/leetcode
test/kotlin/by/jprof/telegram/bot/leetcode Expand file tree Collapse file tree 4 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 11package by.jprof.telegram.bot.leetcode
22
33import by.jprof.telegram.bot.core.UpdateProcessor
4+ import dev.inmo.tgbotapi.types.MessageEntity.textsources.TextLinkTextSource
5+ import dev.inmo.tgbotapi.types.MessageEntity.textsources.URLTextSource
6+ import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
7+ import dev.inmo.tgbotapi.types.message.abstracts.Message
8+ import dev.inmo.tgbotapi.types.message.content.TextContent
9+ import dev.inmo.tgbotapi.types.update.MessageUpdate
410import dev.inmo.tgbotapi.types.update.abstracts.Update
11+ import org.apache.logging.log4j.LogManager
512
613class LeetCodeUpdateProcessor : UpdateProcessor {
14+ companion object {
15+ private val logger = LogManager .getLogger(LeetCodeUpdateProcessor ::class .java)!!
16+ private val slugExtractor: SlugExtractor = ::NaiveRegexSlugExtractor
17+ }
18+
719 override suspend fun process (update : Update ) {
20+ @Suppress(" NAME_SHADOWING" ) val update = update as ? MessageUpdate ? : return
21+
22+ val slugs = extractSlugs(update.data) ? : return
23+
24+ logger.debug(" LeetCode slugs: {}" , slugs)
825 }
26+
27+ private fun extractSlugs (message : Message ): List <String >? =
28+ (message as ? ContentMessage <* >)?.let { contentMessage ->
29+ (contentMessage.content as ? TextContent )?.let { content ->
30+ content
31+ .textSources
32+ .mapNotNull {
33+ (it as ? URLTextSource )?.source ? : (it as ? TextLinkTextSource )?.url
34+ }
35+ .mapNotNull {
36+ slugExtractor(it)
37+ }
38+ }
39+ }
940}
Original file line number Diff line number Diff line change 1+ package by.jprof.telegram.bot.leetcode
2+
3+ private val slugRegex = " https?://leetcode\\ .com/problems/(?<slug>.+?)/?" .toRegex()
4+
5+ @Suppress(" FunctionName" )
6+ fun NaiveRegexSlugExtractor (message : String ): String? {
7+ return slugRegex.matchEntire(message)?.groups?.get(" slug" )?.value
8+ }
Original file line number Diff line number Diff line change 1+ package by.jprof.telegram.bot.leetcode
2+
3+ typealias SlugExtractor = (String ) -> String?
Original file line number Diff line number Diff line change 1+ package by.jprof.telegram.bot.leetcode
2+
3+ import org.junit.jupiter.api.Assertions.assertEquals
4+ import org.junit.jupiter.api.TestInstance
5+ import org.junit.jupiter.params.ParameterizedTest
6+ import org.junit.jupiter.params.provider.Arguments
7+ import org.junit.jupiter.params.provider.MethodSource
8+ import java.util.stream.Stream
9+ import kotlin.streams.asStream
10+
11+ @TestInstance(TestInstance .Lifecycle .PER_CLASS )
12+ internal class NaiveRegexSlugExtractorTest {
13+ @ParameterizedTest
14+ @MethodSource
15+ fun extract (text : String , slug : String? ) {
16+ assertEquals(slug, NaiveRegexSlugExtractor (text))
17+ }
18+
19+ private fun extract (): Stream <Arguments > = sequence {
20+ yield (Arguments .of(" " , null ))
21+ yield (Arguments .of(" " , null ))
22+ yield (Arguments .of(" test" , null ))
23+ yield (Arguments .of(" https://google.com" , null ))
24+ yield (Arguments .of(" https://leetcode.com/problems/two-sum/" , " two-sum" ))
25+ yield (Arguments .of(" https://leetcode.com/problems/3sum" , " 3sum" ))
26+ }.asStream()
27+ }
You can’t perform that action at this time.
0 commit comments