mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-02-23 01:34:42 +00:00
Refactor
This commit is contained in:
60
DiscordChatExporter.Core/Discord/Dump/DataDump.cs
Normal file
60
DiscordChatExporter.Core/Discord/Dump/DataDump.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Compression;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using JsonExtensions.Reading;
|
||||
|
||||
namespace DiscordChatExporter.Core.Discord.Dump;
|
||||
|
||||
public partial class DataDump
|
||||
{
|
||||
public IReadOnlyList<DataDumpChannel> Channels { get; }
|
||||
|
||||
public DataDump(IReadOnlyList<DataDumpChannel> channels) => Channels = channels;
|
||||
}
|
||||
|
||||
public partial class DataDump
|
||||
{
|
||||
public static DataDump Parse(JsonElement json)
|
||||
{
|
||||
var channels = new List<DataDumpChannel>();
|
||||
|
||||
foreach (var property in json.EnumerateObjectOrEmpty())
|
||||
{
|
||||
var channelId = Snowflake.Parse(property.Name);
|
||||
var channelName = property.Value.GetString();
|
||||
|
||||
// Null items refer to deleted channels
|
||||
if (channelName is null)
|
||||
continue;
|
||||
|
||||
var channel = new DataDumpChannel(channelId, channelName);
|
||||
channels.Add(channel);
|
||||
}
|
||||
|
||||
return new DataDump(channels);
|
||||
}
|
||||
|
||||
public static async ValueTask<DataDump> LoadAsync(
|
||||
string zipFilePath,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
using var archive = ZipFile.OpenRead(zipFilePath);
|
||||
|
||||
var entry = archive.GetEntry("messages/index.json");
|
||||
if (entry is null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Could not find the channel index inside the data package."
|
||||
);
|
||||
}
|
||||
|
||||
await using var stream = entry.Open();
|
||||
using var document = await JsonDocument.ParseAsync(stream, default, cancellationToken);
|
||||
|
||||
return Parse(document.RootElement);
|
||||
}
|
||||
}
|
||||
3
DiscordChatExporter.Core/Discord/Dump/DataDumpChannel.cs
Normal file
3
DiscordChatExporter.Core/Discord/Dump/DataDumpChannel.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace DiscordChatExporter.Core.Discord.Dump;
|
||||
|
||||
public record DataDumpChannel(Snowflake Id, string Name);
|
||||
Reference in New Issue
Block a user