Skip to content

Commit 3fe4d1a

Browse files
committed
Fix LeetCode
1 parent 4f30468 commit 3fe4d1a

File tree

3 files changed

+26
-43
lines changed

3 files changed

+26
-43
lines changed

leetcode/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies {
99
implementation(libs.log4j.api)
1010
implementation(libs.graphql.kotlin.ktor.client)
1111
implementation(libs.kotlinx.serialization.core)
12+
implementation(libs.jsoup)
1213

1314
testImplementation(libs.junit.jupiter.api)
1415
testImplementation(libs.junit.jupiter.params)

leetcode/src/main/kotlin/by/jprof/telegram/bot/leetcode/LeetCodeUpdateProcessor.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ class LeetCodeUpdateProcessor(
6363
}
6464

6565
private fun questionInfo(question: Question): String {
66-
return """
67-
|${question.level()} *${question.title.escapeMarkdownV2Common()}* \(${question.categoryTitle}\) ${question.paidIndicator()}
68-
|
69-
|${question.markdownContent()}
70-
|
71-
|_Likes: ${question.likes} / Dislikes: ${question.dislikes}_
72-
""".trimMargin()
66+
return question.markdownContent()?.let {
67+
"""|${question.level()} *${question.title.escapeMarkdownV2Common()}* \(${question.categoryTitle}\) ${question.paidIndicator()}
68+
|
69+
|${it}
70+
|
71+
|_Likes: ${question.likes} / Dislikes: ${question.dislikes}_
72+
""".trimMargin()
73+
} ?: """|${question.level()} *${question.title.escapeMarkdownV2Common()}* \(${question.categoryTitle}\) ${question.paidIndicator()}
74+
|
75+
|_Likes: ${question.likes} / Dislikes: ${question.dislikes}_
76+
""".trimMargin()
7377
}
7478
}

leetcode/src/main/kotlin/by/jprof/telegram/bot/leetcode/Question.kt

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package by.jprof.telegram.bot.leetcode
22

33
import dev.inmo.tgbotapi.utils.extensions.escapeMarkdownV2Common
44
import kotlinx.serialization.Serializable
5+
import org.jsoup.Jsoup
56

67
@Serializable
78
data class Question(
89
val title: String,
910
val titleSlug: String,
10-
val content: String,
11+
val content: String?,
1112
val isPaidOnly: Boolean,
1213
val difficulty: String,
1314
val likes: Int,
@@ -27,40 +28,17 @@ data class Question(
2728
""
2829
}
2930

30-
fun markdownContent(): String = this.content
31-
.replace(" ", " ")
32-
.replace("&lt;", "<")
33-
.replace("&gt;", ">")
34-
.replace("&quot;", "\"")
35-
.replace("&#39;", "'")
36-
.replace(Regex("<sup>(?<sup>.+?)</sup>")) {
37-
"^${it.groups["sup"]!!.value}"
31+
fun markdownContent(): String? {
32+
return try {
33+
val content = Jsoup.parse(this.content ?: return null)
34+
.select("p")
35+
.firstOrNull()
36+
?.text()
37+
?.takeUnless { it.isBlank() } ?: return null
38+
39+
((content.takeIf { it.length < 3000 } ?: (content.take(3000) + ""))).escapeMarkdownV2Common()
40+
} catch (_: Exception) {
41+
null
3842
}
39-
40-
.escapeMarkdownV2Common()
41-
42-
.replace("<code\\>", "`")
43-
.replace("</code\\>", "`")
44-
45-
.replace("<pre\\>", "```")
46-
.replace("</pre\\>", "```")
47-
48-
.replace("<strong\\>", "*")
49-
.replace("</strong\\>", "*")
50-
51-
.replace("<em\\>", "_")
52-
.replace("</em\\>", "_")
53-
54-
.replace("<li\\>", "")
55-
.replace("</li\\>", "")
56-
57-
.replace("<p\\>", "")
58-
.replace("</p\\>", "")
59-
.replace("<ul\\>", "")
60-
.replace("</ul\\>", "")
61-
.replace("<ol\\>", "")
62-
.replace("</ol\\>", "")
63-
64-
.replace("\\n", "\n")
65-
.replace("\\t", "\t")
43+
}
6644
}

0 commit comments

Comments
 (0)