Chats save atachments to separate directories.

This commit is contained in:
Alexander Nozik 2023-09-04 09:17:19 +03:00
parent 209a6db5fc
commit 3a47877828
3 changed files with 19 additions and 12 deletions

View File

@ -56,7 +56,7 @@ internal suspend fun SpaceClient.extractFile(
}
private val imageRegex = """!\[(?<alt>.*)]\(/d/(?<id>.*)\?f=0( "(?<name>.*)")?\)""".toRegex()
private val fileNameRegex = "\\W+".toRegex()
internal val fileNameRegex = "\\W+".toRegex()
/**
* Download single Space document

View File

@ -18,6 +18,10 @@ private suspend fun SpaceClient.writeMessages(
) {
var readDateTime: Instant? = null
var read: Int
val attachmentsDirectory = parentDirectory.resolve("attachments")
attachmentsDirectory.createDirectories()
//reading messages in batches
do {
val result: GetMessagesResponse = chats.messages.getChannelMessages(
@ -39,8 +43,6 @@ private suspend fun SpaceClient.writeMessages(
}
}
}
val attachmentsDirectory = parentDirectory.resolve("attachments")
attachmentsDirectory.createDirectories()
result.messages.forEach { message ->
writer.appendLine(
@ -121,9 +123,11 @@ suspend fun SpaceClient.extractMessages(
is M2SharedChannelContent -> ext.name
is M2ChannelContentMember -> ext.member.username
else -> channel.contact.key
}
}.replace(fileNameRegex, "_")
val file = parentDirectory.resolve("$name.md")
val chatDirectory = parentDirectory.resolve(name)
val file = chatDirectory.resolve("$name.md")
logger.info("Extracting messages from channel $id to $file")
@ -134,9 +138,6 @@ suspend fun SpaceClient.extractMessages(
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE
).bufferedWriter().use { out ->
// out.append(M2ChannelRecordStructure.serialize(channel).toPrettyString())
// out.appendLine("</hr>")
// out.appendLine()
writeMessages(parentDirectory, out, id)
writeMessages(chatDirectory, out, id)
}
}

View File

@ -200,10 +200,16 @@ private class ExtractChannelsCommand : ExtractCommand("channels", "Extract all m
SpaceAuth.ClientCredentials()
)
runBlocking {
runBlocking(Dispatchers.IO) {
if (channelId == null) {
spaceClient.chats.channels.listAllChannels(query = "").data.forEach {
spaceClient.extractMessages(ChannelIdentifier.Id(it.channelId), Path(path))
spaceClient.chats.channels.listAllChannels(query = "").data.forEach { channel ->
launch {
try {
spaceClient.extractMessages(ChannelIdentifier.Id(channel.channelId), Path(path))
} catch (ex: Exception) {
logger.error("Failed to download ${channel.name}", ex)
}
}
}
} else {
spaceClient.extractMessages(ChannelIdentifier.Id(channelId), Path(path))