mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-05-22 16:55:11 +00:00
C#10ify
This commit is contained in:
@@ -14,119 +14,118 @@ using DiscordChatExporter.Core.Discord;
|
||||
using DiscordChatExporter.Core.Exporting;
|
||||
using JsonExtensions;
|
||||
|
||||
namespace DiscordChatExporter.Cli.Tests.Fixtures
|
||||
namespace DiscordChatExporter.Cli.Tests.Fixtures;
|
||||
|
||||
public class ExportWrapperFixture : IDisposable
|
||||
{
|
||||
public class ExportWrapperFixture : IDisposable
|
||||
private string DirPath { get; } = Path.Combine(
|
||||
Path.GetDirectoryName(typeof(ExportWrapperFixture).Assembly.Location) ?? Directory.GetCurrentDirectory(),
|
||||
"ExportCache",
|
||||
Guid.NewGuid().ToString()
|
||||
);
|
||||
|
||||
public ExportWrapperFixture() => DirectoryEx.Reset(DirPath);
|
||||
|
||||
private async ValueTask<string> ExportAsync(Snowflake channelId, ExportFormat format)
|
||||
{
|
||||
private string DirPath { get; } = Path.Combine(
|
||||
Path.GetDirectoryName(typeof(ExportWrapperFixture).Assembly.Location) ?? Directory.GetCurrentDirectory(),
|
||||
"ExportCache",
|
||||
Guid.NewGuid().ToString()
|
||||
var fileName = channelId.ToString() + '.' + format.GetFileExtension();
|
||||
var filePath = Path.Combine(DirPath, fileName);
|
||||
|
||||
// Perform export only if it hasn't been done before
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
await new ExportChannelsCommand
|
||||
{
|
||||
TokenValue = Secrets.DiscordToken,
|
||||
IsBotToken = Secrets.IsDiscordTokenBot,
|
||||
ChannelIds = new[] { channelId },
|
||||
ExportFormat = format,
|
||||
OutputPath = filePath
|
||||
}.ExecuteAsync(new FakeConsole());
|
||||
}
|
||||
|
||||
return await File.ReadAllTextAsync(filePath);
|
||||
}
|
||||
|
||||
public async ValueTask<IHtmlDocument> ExportAsHtmlAsync(Snowflake channelId)
|
||||
{
|
||||
var data = await ExportAsync(channelId, ExportFormat.HtmlDark);
|
||||
return Html.Parse(data);
|
||||
}
|
||||
|
||||
public async ValueTask<JsonElement> ExportAsJsonAsync(Snowflake channelId)
|
||||
{
|
||||
var data = await ExportAsync(channelId, ExportFormat.Json);
|
||||
return Json.Parse(data);
|
||||
}
|
||||
|
||||
public async ValueTask<string> ExportAsPlainTextAsync(Snowflake channelId)
|
||||
{
|
||||
var data = await ExportAsync(channelId, ExportFormat.PlainText);
|
||||
return data;
|
||||
}
|
||||
|
||||
public async ValueTask<string> ExportAsCsvAsync(Snowflake channelId)
|
||||
{
|
||||
var data = await ExportAsync(channelId, ExportFormat.Csv);
|
||||
return data;
|
||||
}
|
||||
|
||||
public async ValueTask<IReadOnlyList<IElement>> GetMessagesAsHtmlAsync(Snowflake channelId)
|
||||
{
|
||||
var document = await ExportAsHtmlAsync(channelId);
|
||||
return document.QuerySelectorAll("[data-message-id]").ToArray();
|
||||
}
|
||||
|
||||
public async ValueTask<IReadOnlyList<JsonElement>> GetMessagesAsJsonAsync(Snowflake channelId)
|
||||
{
|
||||
var document = await ExportAsJsonAsync(channelId);
|
||||
return document.GetProperty("messages").EnumerateArray().ToArray();
|
||||
}
|
||||
|
||||
public async ValueTask<IElement> GetMessageAsHtmlAsync(Snowflake channelId, Snowflake messageId)
|
||||
{
|
||||
var messages = await GetMessagesAsHtmlAsync(channelId);
|
||||
|
||||
var message = messages.SingleOrDefault(e =>
|
||||
string.Equals(
|
||||
e.GetAttribute("data-message-id"),
|
||||
messageId.ToString(),
|
||||
StringComparison.OrdinalIgnoreCase
|
||||
)
|
||||
);
|
||||
|
||||
public ExportWrapperFixture() => DirectoryEx.Reset(DirPath);
|
||||
|
||||
private async ValueTask<string> ExportAsync(Snowflake channelId, ExportFormat format)
|
||||
if (message is null)
|
||||
{
|
||||
var fileName = channelId.ToString() + '.' + format.GetFileExtension();
|
||||
var filePath = Path.Combine(DirPath, fileName);
|
||||
|
||||
// Perform export only if it hasn't been done before
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
await new ExportChannelsCommand
|
||||
{
|
||||
TokenValue = Secrets.DiscordToken,
|
||||
IsBotToken = Secrets.IsDiscordTokenBot,
|
||||
ChannelIds = new[] { channelId },
|
||||
ExportFormat = format,
|
||||
OutputPath = filePath
|
||||
}.ExecuteAsync(new FakeConsole());
|
||||
}
|
||||
|
||||
return await File.ReadAllTextAsync(filePath);
|
||||
}
|
||||
|
||||
public async ValueTask<IHtmlDocument> ExportAsHtmlAsync(Snowflake channelId)
|
||||
{
|
||||
var data = await ExportAsync(channelId, ExportFormat.HtmlDark);
|
||||
return Html.Parse(data);
|
||||
}
|
||||
|
||||
public async ValueTask<JsonElement> ExportAsJsonAsync(Snowflake channelId)
|
||||
{
|
||||
var data = await ExportAsync(channelId, ExportFormat.Json);
|
||||
return Json.Parse(data);
|
||||
}
|
||||
|
||||
public async ValueTask<string> ExportAsPlainTextAsync(Snowflake channelId)
|
||||
{
|
||||
var data = await ExportAsync(channelId, ExportFormat.PlainText);
|
||||
return data;
|
||||
}
|
||||
|
||||
public async ValueTask<string> ExportAsCsvAsync(Snowflake channelId)
|
||||
{
|
||||
var data = await ExportAsync(channelId, ExportFormat.Csv);
|
||||
return data;
|
||||
}
|
||||
|
||||
public async ValueTask<IReadOnlyList<IElement>> GetMessagesAsHtmlAsync(Snowflake channelId)
|
||||
{
|
||||
var document = await ExportAsHtmlAsync(channelId);
|
||||
return document.QuerySelectorAll("[data-message-id]").ToArray();
|
||||
}
|
||||
|
||||
public async ValueTask<IReadOnlyList<JsonElement>> GetMessagesAsJsonAsync(Snowflake channelId)
|
||||
{
|
||||
var document = await ExportAsJsonAsync(channelId);
|
||||
return document.GetProperty("messages").EnumerateArray().ToArray();
|
||||
}
|
||||
|
||||
public async ValueTask<IElement> GetMessageAsHtmlAsync(Snowflake channelId, Snowflake messageId)
|
||||
{
|
||||
var messages = await GetMessagesAsHtmlAsync(channelId);
|
||||
|
||||
var message = messages.SingleOrDefault(e =>
|
||||
string.Equals(
|
||||
e.GetAttribute("data-message-id"),
|
||||
messageId.ToString(),
|
||||
StringComparison.OrdinalIgnoreCase
|
||||
)
|
||||
throw new InvalidOperationException(
|
||||
$"Message '{messageId}' does not exist in export of channel '{channelId}'."
|
||||
);
|
||||
|
||||
if (message is null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Message '{messageId}' does not exist in export of channel '{channelId}'."
|
||||
);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public async ValueTask<JsonElement> GetMessageAsJsonAsync(Snowflake channelId, Snowflake messageId)
|
||||
{
|
||||
var messages = await GetMessagesAsJsonAsync(channelId);
|
||||
|
||||
var message = messages.FirstOrDefault(j =>
|
||||
string.Equals(
|
||||
j.GetProperty("id").GetString(),
|
||||
messageId.ToString(),
|
||||
StringComparison.OrdinalIgnoreCase
|
||||
)
|
||||
);
|
||||
|
||||
if (message.ValueKind == JsonValueKind.Undefined)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Message '{messageId}' does not exist in export of channel '{channelId}'."
|
||||
);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public void Dispose() => DirectoryEx.DeleteIfExists(DirPath);
|
||||
return message;
|
||||
}
|
||||
|
||||
public async ValueTask<JsonElement> GetMessageAsJsonAsync(Snowflake channelId, Snowflake messageId)
|
||||
{
|
||||
var messages = await GetMessagesAsJsonAsync(channelId);
|
||||
|
||||
var message = messages.FirstOrDefault(j =>
|
||||
string.Equals(
|
||||
j.GetProperty("id").GetString(),
|
||||
messageId.ToString(),
|
||||
StringComparison.OrdinalIgnoreCase
|
||||
)
|
||||
);
|
||||
|
||||
if (message.ValueKind == JsonValueKind.Undefined)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Message '{messageId}' does not exist in export of channel '{channelId}'."
|
||||
);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public void Dispose() => DirectoryEx.DeleteIfExists(DirPath);
|
||||
}
|
||||
@@ -2,22 +2,21 @@
|
||||
using System.IO;
|
||||
using DiscordChatExporter.Cli.Tests.Utils;
|
||||
|
||||
namespace DiscordChatExporter.Cli.Tests.Fixtures
|
||||
namespace DiscordChatExporter.Cli.Tests.Fixtures;
|
||||
|
||||
public class TempOutputFixture : IDisposable
|
||||
{
|
||||
public class TempOutputFixture : IDisposable
|
||||
{
|
||||
public string DirPath { get; } = Path.Combine(
|
||||
Path.GetDirectoryName(typeof(TempOutputFixture).Assembly.Location) ?? Directory.GetCurrentDirectory(),
|
||||
"Temp",
|
||||
Guid.NewGuid().ToString()
|
||||
);
|
||||
public string DirPath { get; } = Path.Combine(
|
||||
Path.GetDirectoryName(typeof(TempOutputFixture).Assembly.Location) ?? Directory.GetCurrentDirectory(),
|
||||
"Temp",
|
||||
Guid.NewGuid().ToString()
|
||||
);
|
||||
|
||||
public TempOutputFixture() => DirectoryEx.Reset(DirPath);
|
||||
public TempOutputFixture() => DirectoryEx.Reset(DirPath);
|
||||
|
||||
public string GetTempFilePath(string fileName) => Path.Combine(DirPath, fileName);
|
||||
public string GetTempFilePath(string fileName) => Path.Combine(DirPath, fileName);
|
||||
|
||||
public string GetTempFilePath() => GetTempFilePath(Guid.NewGuid() + ".tmp");
|
||||
public string GetTempFilePath() => GetTempFilePath(Guid.NewGuid() + ".tmp");
|
||||
|
||||
public void Dispose() => DirectoryEx.DeleteIfExists(DirPath);
|
||||
}
|
||||
public void Dispose() => DirectoryEx.DeleteIfExists(DirPath);
|
||||
}
|
||||
Reference in New Issue
Block a user