From 60a5a69aa4fca093b48d196b12e2b4b35faeeb55 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Fri, 27 Oct 2017 22:35:04 +0300 Subject: [PATCH] Improve message date filtering, convert from snowflake to get the 'to' message ID --- DiscordChatExporter/Services/DataService.cs | 28 +++++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/DiscordChatExporter/Services/DataService.cs b/DiscordChatExporter/Services/DataService.cs index 53c1b55..8f82c92 100644 --- a/DiscordChatExporter/Services/DataService.cs +++ b/DiscordChatExporter/Services/DataService.cs @@ -108,7 +108,7 @@ namespace DiscordChatExporter.Services // We are going backwards from last message to first // collecting everything between them in batches - string beforeId = null; + var beforeId = to != null ? DateTimeToSnowflake(to.Value) : null; while (true) { // Form request url @@ -126,6 +126,14 @@ namespace DiscordChatExporter.Services string currentMessageId = null; foreach (var message in messages) { + // Break when the message is older than from date + if (from != null && message.TimeStamp < from) + { + currentMessageId = null; + break; + } + + // Add message result.Add(message); currentMessageId = message.Id; } @@ -134,10 +142,6 @@ namespace DiscordChatExporter.Services if (currentMessageId == null) break; - // If last message is older than from date - break - if (from != null && result.Last().TimeStamp < from) - break; - // Otherwise offset the next request beforeId = currentMessageId; } @@ -145,12 +149,6 @@ namespace DiscordChatExporter.Services // Messages appear newest first, we need to reverse result.Reverse(); - // Filter - if (from != null) - result.RemoveAll(m => m.TimeStamp < from); - if (to != null) - result.RemoveAll(m => m.TimeStamp > to); - return result; } @@ -171,6 +169,14 @@ namespace DiscordChatExporter.Services public partial class DataService { + private static string DateTimeToSnowflake(DateTime dateTime) + { + const long epoch = 62135596800000; + var unixTime = dateTime.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond - epoch; + var value = ((ulong) unixTime - 1420070400000UL) << 22; + return value.ToString(); + } + private static Guild ParseGuild(JToken token) { var id = token.Value("id");