[CLI] Allow passing after and before message ids instead of dates (#462)

pull/466/head
Andrew Kolos 4 years ago committed by GitHub
parent 600c354ab2
commit 730fed075f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using CliFx; using CliFx;
using CliFx.Attributes; using CliFx.Attributes;
@ -10,7 +11,7 @@ using DiscordChatExporter.Domain.Exporting;
namespace DiscordChatExporter.Cli.Commands.Base namespace DiscordChatExporter.Cli.Commands.Base
{ {
public abstract class ExportCommandBase : TokenCommandBase public abstract partial class ExportCommandBase : TokenCommandBase
{ {
[CommandOption("output", 'o', [CommandOption("output", 'o',
Description = "Output file or directory path.")] Description = "Output file or directory path.")]
@ -21,12 +22,12 @@ namespace DiscordChatExporter.Cli.Commands.Base
public ExportFormat ExportFormat { get; set; } = ExportFormat.HtmlDark; public ExportFormat ExportFormat { get; set; } = ExportFormat.HtmlDark;
[CommandOption("after", [CommandOption("after",
Description = "Only include messages sent after this date.")] Description = "Only include messages sent after this date. Alternatively, provide the ID of a message.")]
public DateTimeOffset? After { get; set; } public string? After { get; set; }
[CommandOption("before", [CommandOption("before",
Description = "Only include messages sent before this date.")] Description = "Only include messages sent before this date. Alternatively, provide the ID of a message.")]
public DateTimeOffset? Before { get; set; } public string? Before { get; set; }
[CommandOption("partition", 'p', [CommandOption("partition", 'p',
Description = "Split output into partitions limited to this number of messages.")] Description = "Split output into partitions limited to this number of messages.")]
@ -56,8 +57,8 @@ namespace DiscordChatExporter.Cli.Commands.Base
channel, channel,
OutputPath, OutputPath,
ExportFormat, ExportFormat,
After, ParseRangeOption(After, "--after"),
Before, ParseRangeOption(Before, "--before"),
PartitionLimit, PartitionLimit,
ShouldDownloadMedia, ShouldDownloadMedia,
ShouldReuseMedia, ShouldReuseMedia,
@ -86,12 +87,35 @@ namespace DiscordChatExporter.Cli.Commands.Base
{ {
if (ShouldReuseMedia && !ShouldDownloadMedia) if (ShouldReuseMedia && !ShouldDownloadMedia)
{ {
throw new CommandException( throw new CommandException("The --reuse-media option cannot be used without the --media option.");
"The --reuse-media option cannot be used without the --media option."
);
} }
return default; return default;
} }
} }
public abstract partial class ExportCommandBase : TokenCommandBase
{
protected static DateTimeOffset? ParseRangeOption(string? value, string optionName)
{
if (value == null) return null;
var isSnowflake = Regex.IsMatch(value, @"^\d{18}$");
var isDate = DateTimeOffset.TryParse(value, out var datetime);
if (!isSnowflake && !isDate)
{
throw new ArgumentException($"Value for ${optionName} must be either a date or a message ID.");
}
return isSnowflake ? ExtractDateTimeFromSnowflake() : datetime;
DateTimeOffset ExtractDateTimeFromSnowflake()
{
var unixTimestampMs = (long.Parse(value) / 4194304 + 1420070400000);
return DateTimeOffset.FromUnixTimeMilliseconds(unixTimestampMs);
}
}
}
} }

@ -47,8 +47,8 @@ namespace DiscordChatExporter.Cli.Commands.Base
channel, channel,
OutputPath, OutputPath,
ExportFormat, ExportFormat,
After, ParseRangeOption(After, "--after"),
Before, ParseRangeOption(Before, "--before"),
PartitionLimit, PartitionLimit,
ShouldDownloadMedia, ShouldDownloadMedia,
ShouldReuseMedia, ShouldReuseMedia,

Loading…
Cancel
Save