Skip to content

Commit 6551cb4

Browse files
authored
Merge pull request #34 from /issues/31
Parse today's posts
2 parents b3f9716 + 3f1e316 commit 6551cb4

File tree

10 files changed

+139
-1
lines changed

10 files changed

+139
-1
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ktor = "1.6.8"
1212

1313
kotlinx-serialization = "1.3.0"
1414
jackson = "2.13.0"
15+
kaml = "0.43.0"
1516

1617
tgbotapi = "0.35.9"
1718

@@ -45,6 +46,7 @@ ktor-client-serialization = { group = "io.ktor", name = "ktor-client-serializati
4546
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
4647
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
4748
jackson-databind = { group = "com.fasterxml.jackson.core", name = "jackson-databind", version.ref = "jackson" }
49+
kaml = { group = "com.charleskorn.kaml", name = "kaml", version.ref = "kaml" }
4850

4951
tgbotapi-core = { group = "dev.inmo", name = "tgbotapi.core", version.ref = "tgbotapi" }
5052
tgbotapi-extensions-api = { group = "dev.inmo", name = "tgbotapi.extensions.api", version.ref = "tgbotapi" }

herald/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
plugins {
22
kotlin("jvm")
3+
kotlin("plugin.serialization")
34
application
45
}
56

67
application {
78
mainClass.set("by.jprof.telegram.bot.herald.AppKt")
89
}
10+
11+
dependencies {
12+
implementation(libs.kotlinx.serialization.core)
13+
implementation(libs.kaml)
14+
15+
testImplementation(libs.junit.jupiter.api)
16+
testImplementation(libs.junit.jupiter.params)
17+
testRuntimeOnly(libs.junit.jupiter.engine)
18+
testRuntimeOnly(libs.log4j.core)
19+
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package by.jprof.telegram.bot.herald
22

3+
import by.jprof.telegram.bot.herald.impl.post
4+
import by.jprof.telegram.bot.herald.impl.postFile
5+
36
suspend fun main(args: Array<String>) {
4-
println("Hello, world!")
7+
val postFile = postFile() ?: run { println("No post for today"); return }
8+
9+
println("Today's post: $postFile")
10+
11+
val post = post(postFile) ?: run { println("Cannot parse the file"); return }
12+
13+
println("Parsed the post: $post")
514
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package by.jprof.telegram.bot.herald.impl
2+
3+
import by.jprof.telegram.bot.herald.model.Post
4+
import com.charleskorn.kaml.Yaml
5+
import kotlinx.serialization.decodeFromString
6+
import java.nio.file.Path
7+
import kotlin.io.path.readText
8+
import kotlin.text.RegexOption.DOT_MATCHES_ALL
9+
import kotlin.text.RegexOption.MULTILINE
10+
11+
fun post(path: Path): Post? {
12+
val text = path.readText()
13+
val regex = Regex("-{3,}\\R(?<frontmatter>.*)\\R-{3,}\\s+(?<content>.*)", setOf(MULTILINE, DOT_MATCHES_ALL))
14+
15+
return regex.find(text)?.let {
16+
val groups = it.groups as MatchNamedGroupCollection
17+
18+
val frontmatter = groups["frontmatter"]?.value ?: return null
19+
val content = groups["content"]?.value ?: return null
20+
21+
Post(Yaml.default.decodeFromString(frontmatter), content)
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package by.jprof.telegram.bot.herald.impl
2+
3+
import java.nio.file.Path
4+
import java.time.LocalDate
5+
import java.time.format.DateTimeFormatter
6+
import kotlin.io.path.Path
7+
import kotlin.io.path.exists
8+
9+
fun postFile(): Path? {
10+
val cwd = Path("")
11+
val today = LocalDate.now()
12+
val formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd")
13+
14+
return cwd.resolve(today.format(formatter) + ".md").takeIf { it.exists() }
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package by.jprof.telegram.bot.herald.model
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class Frontmatter(
7+
val chats: List<Long>,
8+
val image: String? = null,
9+
val disableWebPagePreview: Boolean = false,
10+
val votes: List<String>? = null,
11+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package by.jprof.telegram.bot.herald.model
2+
3+
data class Post(
4+
val frontmatter: Frontmatter,
5+
val content: String,
6+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package by.jprof.telegram.bot.herald.impl
2+
3+
import by.jprof.telegram.bot.herald.model.Frontmatter
4+
import by.jprof.telegram.bot.herald.model.Post
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Named
7+
import org.junit.jupiter.params.ParameterizedTest
8+
import org.junit.jupiter.params.provider.Arguments
9+
import org.junit.jupiter.params.provider.MethodSource
10+
import java.nio.file.Path
11+
import java.util.stream.Stream
12+
13+
internal class PostTest {
14+
@ParameterizedTest(name = "{0}")
15+
@MethodSource
16+
fun post(path: Path, post: Post) {
17+
assertEquals(
18+
post,
19+
post(path)
20+
)
21+
}
22+
23+
companion object {
24+
@JvmStatic
25+
fun post() = Stream.of(
26+
Arguments.of(
27+
Named.of("001", "001".testResourcePath),
28+
Post(
29+
Frontmatter(chats = listOf(-1001146107319, -1001585354456)),
30+
"This is content.\n"
31+
)
32+
),
33+
Arguments.of(
34+
Named.of("002", "002".testResourcePath),
35+
Post(
36+
Frontmatter(chats = listOf(-1001146107319, -1001585354456), image = "002.png"),
37+
"This is a\nmultiline content!\n"
38+
)
39+
),
40+
)
41+
42+
private val String.testResourcePath: Path
43+
get() = Path.of(this@Companion::class.java.classLoader.getResource("posts/$this.md").toURI())
44+
}
45+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
chats:
3+
- -1001146107319 # JPROF.BY
4+
- -1001585354456 # JPROF.PL
5+
---
6+
7+
This is content.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
chats:
3+
- -1001146107319 # JPROF.BY
4+
- -1001585354456 # JPROF.PL
5+
image: 002.png
6+
---
7+
8+
This is a
9+
multiline content!

0 commit comments

Comments
 (0)