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 imageRegex = """!\[(?<alt>.*)]\(/d/(?<id>.*)\?f=0( "(?<name>.*)")?\)""".toRegex()
private val fileNameRegex = "\\W+".toRegex() internal val fileNameRegex = "\\W+".toRegex()
/** /**
* Download single Space document * Download single Space document

View File

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

View File

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