Add full chat extraction

This commit is contained in:
Alexander Nozik 2023-08-28 23:15:32 +03:00
parent 2734c18b11
commit 3eb43d4d19
2 changed files with 20 additions and 6 deletions

View File

@ -47,7 +47,7 @@ private suspend fun SpaceClient.writeMessages(
writer.appendLine( writer.appendLine(
""" """
|* **(${message.created}) ${message.author.name}:** |* **(${message.created}) ${message.author.name}:**
|${message.text.trimIndent()} |${message.text}
""".replaceIndentByMargin(prefix) """.replaceIndentByMargin(prefix)
) )
@ -106,6 +106,8 @@ suspend fun SpaceClient.extractMessages(
val file = parentDirectory.resolve("$name.md") val file = parentDirectory.resolve("$name.md")
logger.info("Extracting messages from channel $chatId to $file")
file.parent.createDirectories() file.parent.createDirectories()
file.outputStream( file.outputStream(

View File

@ -10,6 +10,7 @@ import space.jetbrains.api.runtime.SpaceAppInstance
import space.jetbrains.api.runtime.SpaceAuth import space.jetbrains.api.runtime.SpaceAuth
import space.jetbrains.api.runtime.SpaceClient import space.jetbrains.api.runtime.SpaceClient
import space.jetbrains.api.runtime.ktorClientForSpace import space.jetbrains.api.runtime.ktorClientForSpace
import space.jetbrains.api.runtime.resources.chats
import space.jetbrains.api.runtime.resources.projects import space.jetbrains.api.runtime.resources.projects
import space.jetbrains.api.runtime.types.FolderIdentifier import space.jetbrains.api.runtime.types.FolderIdentifier
import space.jetbrains.api.runtime.types.ProjectIdentifier import space.jetbrains.api.runtime.types.ProjectIdentifier
@ -170,7 +171,7 @@ private class ExtractRepositoriesCommand : ExtractCommand("repos", "Extract repo
} }
private class ExtractChatsCommand: ExtractCommand("chat","Extract all messages from a chat"){ private class ExtractMessagesCommand : ExtractCommand("messages", "Extract all messages from a chat") {
val path: String by option( val path: String by option(
ArgType.String, ArgType.String,
@ -182,7 +183,7 @@ private class ExtractChatsCommand: ExtractCommand("chat","Extract all messages f
val spaceUrl = urlMatch.groups["spaceUrl"]?.value ?: error("Space Url token not recognized") val spaceUrl = urlMatch.groups["spaceUrl"]?.value ?: error("Space Url token not recognized")
val chatId = urlMatch.groups["chatId"]?.value ?: error("Chat id token not recognized") val chatId = urlMatch.groups["chatId"]?.value
val appInstance = SpaceAppInstance( val appInstance = SpaceAppInstance(
clientId ?: System.getProperty("space.clientId"), clientId ?: System.getProperty("space.clientId"),
@ -197,13 +198,19 @@ private class ExtractChatsCommand: ExtractCommand("chat","Extract all messages f
) )
runBlocking { runBlocking {
if (chatId == null) {
spaceClient.chats.channels.listAllChannels(query = "").data.forEach {
spaceClient.extractMessages(it.channelId, Path(path))
}
} else {
spaceClient.extractMessages(chatId, Path(path)) spaceClient.extractMessages(chatId, Path(path))
} }
} }
}
companion object { companion object {
private val urlRegex = private val urlRegex =
"""(?<spaceUrl>https?:\/\/[^\/]*)\/im\/group\/(?<chatId>.*)""".toRegex() """(?<spaceUrl>https?:\/\/[^\/]*)(\/im\/group\/(?<chatId>.*))?""".toRegex()
} }
} }
@ -272,7 +279,12 @@ private class ExtractProjectCommand : ExtractCommand("project", "Extract all dat
fun main(args: Array<String>) { fun main(args: Array<String>) {
val parser = ArgParser("space-export") val parser = ArgParser("space-export")
parser.subcommands(ExtractDocumentsCommand(), ExtractRepositoriesCommand(), ExtractProjectCommand(), ExtractChatsCommand()) parser.subcommands(
ExtractDocumentsCommand(),
ExtractRepositoriesCommand(),
ExtractProjectCommand(),
ExtractMessagesCommand()
)
parser.parse(args) parser.parse(args)
} }