SupportChild/Transcriber.cs

139 lines
3.8 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.IO.Compression;
2022-02-21 08:40:09 +00:00
using System.Threading.Tasks;
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Exporting;
using DiscordChatExporter.Core.Exporting.Filtering;
using DiscordChatExporter.Core.Exporting.Partitioning;
2022-08-21 07:33:27 +00:00
namespace SupportChild;
internal static class Transcriber
2022-02-21 08:40:09 +00:00
{
internal static async Task ExecuteAsync(ulong channelID, uint ticketID)
{
DiscordClient discordClient = new DiscordClient(Config.token);
ChannelExporter exporter = new ChannelExporter(discordClient);
string htmlPath = GetHtmlPath(ticketID);
string zipPath = GetZipPath(ticketID);
string assetDirPath = GetAssetDirPath(ticketID);
string assetDirName = GetAssetDirName(ticketID);
string htmlFilename = GetHTMLFilename(ticketID);
if (File.Exists(htmlPath))
{
File.Delete(htmlPath);
}
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
if (Directory.Exists(assetDirPath))
{
Directory.Delete(assetDirPath, true);
}
Channel channel = await discordClient.GetChannelAsync(new Snowflake(channelID));
Guild guild = await discordClient.GetGuildAsync(channel.GuildId);
2024-10-29 09:52:02 +00:00
ExportRequest request = new(
guild,
channel,
htmlPath,
assetDirPath,
2024-10-29 09:52:02 +00:00
ExportFormat.HtmlDark,
null,
null,
PartitionLimit.Null,
MessageFilter.Null,
true,
true,
true,
"en-SE",
2024-10-29 09:52:02 +00:00
true
);
await exporter.ExportChannelAsync(request);
string[] assetFiles;
try
{
assetFiles = Directory.GetFiles(assetDirPath);
}
catch (Exception)
{
assetFiles = [];
}
if (assetFiles.Length > 0)
{
using (ZipArchive zip = ZipFile.Open(zipPath, ZipArchiveMode.Create))
{
zip.CreateEntryFromFile(htmlPath, htmlFilename, CompressionLevel.SmallestSize);
foreach (string assetFile in assetFiles)
{
zip.CreateEntryFromFile(assetFile, assetDirName + "/" + Path.GetFileName(assetFile), CompressionLevel.SmallestSize);
}
}
Directory.Delete(assetDirPath, true);
}
}
2024-12-27 06:36:02 +00:00
private static string GetTranscriptDir()
{
string transcriptDir = "./transcripts";
if (!string.IsNullOrEmpty(SupportChild.commandLineArgs.transcriptDir))
{
transcriptDir = SupportChild.commandLineArgs.transcriptDir;
}
else if (!string.IsNullOrEmpty(Config.transcriptDir))
{
transcriptDir = Config.transcriptDir;
}
if (!Directory.Exists(transcriptDir))
{
Directory.CreateDirectory(transcriptDir);
}
return transcriptDir;
}
internal static string GetHtmlPath(uint ticketNumber)
{
2024-12-27 06:36:02 +00:00
return GetTranscriptDir() + "/" + GetHTMLFilename(ticketNumber);
}
internal static string GetZipPath(uint ticketNumber)
{
2024-12-27 06:36:02 +00:00
return GetTranscriptDir() + "/" + GetZipFilename(ticketNumber);
}
internal static string GetAssetDirPath(uint ticketNumber)
{
2024-12-27 06:36:02 +00:00
return GetTranscriptDir() + "/" + GetAssetDirName(ticketNumber);
}
internal static string GetAssetDirName(uint ticketNumber)
{
return "ticket-" + ticketNumber.ToString("00000") + "-assets";
}
internal static string GetHTMLFilename(uint ticketNumber)
{
return "ticket-" + ticketNumber.ToString("00000") + ".html";
}
internal static string GetZipFilename(uint ticketNumber)
{
return "ticket-" + ticketNumber.ToString("00000") + ".zip";
}
2022-05-17 13:11:57 +00:00
}