From 3f1e3163bb86d0e700ff38a4500ae8b801cc002c Mon Sep 17 00:00:00 2001 From: madhead Date: Mon, 18 Apr 2022 15:00:12 +0200 Subject: [PATCH] Parse today's posts --- gradle/libs.versions.toml | 2 + herald/build.gradle.kts | 11 +++++ .../by/jprof/telegram/bot/herald/App.kt | 11 ++++- .../by/jprof/telegram/bot/herald/impl/post.kt | 23 ++++++++++ .../telegram/bot/herald/impl/postFile.kt | 15 +++++++ .../telegram/bot/herald/model/Frontmatter.kt | 11 +++++ .../jprof/telegram/bot/herald/model/Post.kt | 6 +++ .../telegram/bot/herald/impl/PostTest.kt | 45 +++++++++++++++++++ herald/src/test/resources/posts/001.md | 7 +++ herald/src/test/resources/posts/002.md | 9 ++++ 10 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 herald/src/main/kotlin/by/jprof/telegram/bot/herald/impl/post.kt create mode 100644 herald/src/main/kotlin/by/jprof/telegram/bot/herald/impl/postFile.kt create mode 100644 herald/src/main/kotlin/by/jprof/telegram/bot/herald/model/Frontmatter.kt create mode 100644 herald/src/main/kotlin/by/jprof/telegram/bot/herald/model/Post.kt create mode 100644 herald/src/test/kotlin/by/jprof/telegram/bot/herald/impl/PostTest.kt create mode 100644 herald/src/test/resources/posts/001.md create mode 100644 herald/src/test/resources/posts/002.md diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2ec6182a..3c181b1a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -12,6 +12,7 @@ ktor = "1.6.8" kotlinx-serialization = "1.3.0" jackson = "2.13.0" +kaml = "0.43.0" tgbotapi = "0.35.9" @@ -45,6 +46,7 @@ ktor-client-serialization = { group = "io.ktor", name = "ktor-client-serializati kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" } jackson-databind = { group = "com.fasterxml.jackson.core", name = "jackson-databind", version.ref = "jackson" } +kaml = { group = "com.charleskorn.kaml", name = "kaml", version.ref = "kaml" } tgbotapi-core = { group = "dev.inmo", name = "tgbotapi.core", version.ref = "tgbotapi" } tgbotapi-extensions-api = { group = "dev.inmo", name = "tgbotapi.extensions.api", version.ref = "tgbotapi" } diff --git a/herald/build.gradle.kts b/herald/build.gradle.kts index 9f40d1fe..ca71e9de 100644 --- a/herald/build.gradle.kts +++ b/herald/build.gradle.kts @@ -1,8 +1,19 @@ plugins { kotlin("jvm") + kotlin("plugin.serialization") application } application { mainClass.set("by.jprof.telegram.bot.herald.AppKt") } + +dependencies { + implementation(libs.kotlinx.serialization.core) + implementation(libs.kaml) + + testImplementation(libs.junit.jupiter.api) + testImplementation(libs.junit.jupiter.params) + testRuntimeOnly(libs.junit.jupiter.engine) + testRuntimeOnly(libs.log4j.core) +} diff --git a/herald/src/main/kotlin/by/jprof/telegram/bot/herald/App.kt b/herald/src/main/kotlin/by/jprof/telegram/bot/herald/App.kt index f9ab0277..d9efed6c 100644 --- a/herald/src/main/kotlin/by/jprof/telegram/bot/herald/App.kt +++ b/herald/src/main/kotlin/by/jprof/telegram/bot/herald/App.kt @@ -1,5 +1,14 @@ package by.jprof.telegram.bot.herald +import by.jprof.telegram.bot.herald.impl.post +import by.jprof.telegram.bot.herald.impl.postFile + suspend fun main(args: Array) { - println("Hello, world!") + val postFile = postFile() ?: run { println("No post for today"); return } + + println("Today's post: $postFile") + + val post = post(postFile) ?: run { println("Cannot parse the file"); return } + + println("Parsed the post: $post") } diff --git a/herald/src/main/kotlin/by/jprof/telegram/bot/herald/impl/post.kt b/herald/src/main/kotlin/by/jprof/telegram/bot/herald/impl/post.kt new file mode 100644 index 00000000..ad495cbf --- /dev/null +++ b/herald/src/main/kotlin/by/jprof/telegram/bot/herald/impl/post.kt @@ -0,0 +1,23 @@ +package by.jprof.telegram.bot.herald.impl + +import by.jprof.telegram.bot.herald.model.Post +import com.charleskorn.kaml.Yaml +import kotlinx.serialization.decodeFromString +import java.nio.file.Path +import kotlin.io.path.readText +import kotlin.text.RegexOption.DOT_MATCHES_ALL +import kotlin.text.RegexOption.MULTILINE + +fun post(path: Path): Post? { + val text = path.readText() + val regex = Regex("-{3,}\\R(?.*)\\R-{3,}\\s+(?.*)", setOf(MULTILINE, DOT_MATCHES_ALL)) + + return regex.find(text)?.let { + val groups = it.groups as MatchNamedGroupCollection + + val frontmatter = groups["frontmatter"]?.value ?: return null + val content = groups["content"]?.value ?: return null + + Post(Yaml.default.decodeFromString(frontmatter), content) + } +} diff --git a/herald/src/main/kotlin/by/jprof/telegram/bot/herald/impl/postFile.kt b/herald/src/main/kotlin/by/jprof/telegram/bot/herald/impl/postFile.kt new file mode 100644 index 00000000..35a9e0e2 --- /dev/null +++ b/herald/src/main/kotlin/by/jprof/telegram/bot/herald/impl/postFile.kt @@ -0,0 +1,15 @@ +package by.jprof.telegram.bot.herald.impl + +import java.nio.file.Path +import java.time.LocalDate +import java.time.format.DateTimeFormatter +import kotlin.io.path.Path +import kotlin.io.path.exists + +fun postFile(): Path? { + val cwd = Path("") + val today = LocalDate.now() + val formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd") + + return cwd.resolve(today.format(formatter) + ".md").takeIf { it.exists() } +} diff --git a/herald/src/main/kotlin/by/jprof/telegram/bot/herald/model/Frontmatter.kt b/herald/src/main/kotlin/by/jprof/telegram/bot/herald/model/Frontmatter.kt new file mode 100644 index 00000000..d3ae91a5 --- /dev/null +++ b/herald/src/main/kotlin/by/jprof/telegram/bot/herald/model/Frontmatter.kt @@ -0,0 +1,11 @@ +package by.jprof.telegram.bot.herald.model + +import kotlinx.serialization.Serializable + +@Serializable +data class Frontmatter( + val chats: List, + val image: String? = null, + val disableWebPagePreview: Boolean = false, + val votes: List? = null, +) diff --git a/herald/src/main/kotlin/by/jprof/telegram/bot/herald/model/Post.kt b/herald/src/main/kotlin/by/jprof/telegram/bot/herald/model/Post.kt new file mode 100644 index 00000000..1ae1ee69 --- /dev/null +++ b/herald/src/main/kotlin/by/jprof/telegram/bot/herald/model/Post.kt @@ -0,0 +1,6 @@ +package by.jprof.telegram.bot.herald.model + +data class Post( + val frontmatter: Frontmatter, + val content: String, +) diff --git a/herald/src/test/kotlin/by/jprof/telegram/bot/herald/impl/PostTest.kt b/herald/src/test/kotlin/by/jprof/telegram/bot/herald/impl/PostTest.kt new file mode 100644 index 00000000..1a342645 --- /dev/null +++ b/herald/src/test/kotlin/by/jprof/telegram/bot/herald/impl/PostTest.kt @@ -0,0 +1,45 @@ +package by.jprof.telegram.bot.herald.impl + +import by.jprof.telegram.bot.herald.model.Frontmatter +import by.jprof.telegram.bot.herald.model.Post +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Named +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource +import java.nio.file.Path +import java.util.stream.Stream + +internal class PostTest { + @ParameterizedTest(name = "{0}") + @MethodSource + fun post(path: Path, post: Post) { + assertEquals( + post, + post(path) + ) + } + + companion object { + @JvmStatic + fun post() = Stream.of( + Arguments.of( + Named.of("001", "001".testResourcePath), + Post( + Frontmatter(chats = listOf(-1001146107319, -1001585354456)), + "This is content.\n" + ) + ), + Arguments.of( + Named.of("002", "002".testResourcePath), + Post( + Frontmatter(chats = listOf(-1001146107319, -1001585354456), image = "002.png"), + "This is a\nmultiline content!\n" + ) + ), + ) + + private val String.testResourcePath: Path + get() = Path.of(this@Companion::class.java.classLoader.getResource("posts/$this.md").toURI()) + } +} diff --git a/herald/src/test/resources/posts/001.md b/herald/src/test/resources/posts/001.md new file mode 100644 index 00000000..db1381a2 --- /dev/null +++ b/herald/src/test/resources/posts/001.md @@ -0,0 +1,7 @@ +--- +chats: + - -1001146107319 # JPROF.BY + - -1001585354456 # JPROF.PL +--- + +This is content. diff --git a/herald/src/test/resources/posts/002.md b/herald/src/test/resources/posts/002.md new file mode 100644 index 00000000..b1e4daa6 --- /dev/null +++ b/herald/src/test/resources/posts/002.md @@ -0,0 +1,9 @@ +--- +chats: + - -1001146107319 # JPROF.BY + - -1001585354456 # JPROF.PL +image: 002.png +--- + +This is a +multiline content!