Skip to content

Commit 094b804

Browse files
committed
Parse LeetCode slugs
1 parent b087414 commit 094b804

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
package by.jprof.telegram.bot.leetcode
22

33
import 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
410
import dev.inmo.tgbotapi.types.update.abstracts.Update
11+
import org.apache.logging.log4j.LogManager
512

613
class 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
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package by.jprof.telegram.bot.leetcode
2+
3+
typealias SlugExtractor = (String) -> String?
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)