protected async Task ExportChannelAsync(IConsole console, Channel channel)
        {
            // Configure settings
            if (!string.IsNullOrWhiteSpace(DateFormat))
            {
                SettingsService.DateFormat = DateFormat !;
            }

            console.Output.Write($"Exporting channel [{channel.Name}]... ");
            var progress = console.CreateProgressTicker();

            // Get chat log
            var chatLog = await DataService.GetChatLogAsync(GetToken(), channel, After, Before, progress);

            // Generate file path if not set or is a directory
            var filePath = OutputPath;

            if (string.IsNullOrWhiteSpace(filePath) || ExportHelper.IsDirectoryPath(filePath))
            {
                // Generate default file name
                var fileName = ExportHelper.GetDefaultExportFileName(ExportFormat, chatLog.Guild,
                                                                     chatLog.Channel, After, Before);

                // Combine paths
                filePath = Path.Combine(filePath ?? "", fileName);
            }

            // Export
            await ExportService.ExportChatLogAsync(chatLog, filePath, ExportFormat, PartitionLimit);

            console.Output.WriteLine();
        }
Beispiel #2
0
        public async void ExportChannels()
        {
            // Get last used token
            var token = _settingsService.LastToken !;

            // Create dialog
            var dialog = _viewModelFactory.CreateExportSetupViewModel(SelectedGuild !, SelectedChannels !);

            // Show dialog, if canceled - return
            if (await _dialogManager.ShowDialogAsync(dialog) != true)
            {
                return;
            }

            // Create a progress operation for each channel to export
            var operations = ProgressManager.CreateOperations(dialog.Channels.Count);

            // Export channels
            var successfulExportCount = 0;

            for (var i = 0; i < dialog.Channels.Count; i++)
            {
                var operation = operations[i];
                var channel   = dialog.Channels[i];

                try
                {
                    await _exportService.ExportChatLogAsync(token, dialog.Guild, channel,
                                                            dialog.OutputPath !, dialog.SelectedFormat, dialog.PartitionLimit,
                                                            dialog.After, dialog.Before, operation);

                    successfulExportCount++;
                }
                catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.Forbidden)
                {
                    Notifications.Enqueue($"You don't have access to channel [{channel.Model.Name}]");
                }
                catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
                {
                    Notifications.Enqueue($"Channel [{channel.Model.Name}] doesn't exist");
                }
                catch (DomainException ex)
                {
                    Notifications.Enqueue(ex.Message);
                }
                finally
                {
                    operation.Dispose();
                }
            }

            // Notify of overall completion
            if (successfulExportCount > 0)
            {
                Notifications.Enqueue($"Successfully exported {successfulExportCount} channel(s)");
            }
        }
Beispiel #3
0
        protected async ValueTask ExportAsync(IConsole console, Guild guild, Channel channel)
        {
            if (!string.IsNullOrWhiteSpace(DateFormat))
            {
                SettingsService.DateFormat = DateFormat;
            }

            console.Output.Write($"Exporting channel [{channel.Name}]... ");
            var progress = console.CreateProgressTicker();

            await ExportService.ExportChatLogAsync(Token, guild, channel,
                                                   OutputPath, ExportFormat, PartitionLimit,
                                                   After, Before, progress);

            console.Output.WriteLine();
        }
Beispiel #4
0
        private async Task ExportGuildChatLogs()
        {
            // Filter and order channels
            channels = channels.Where(c => c.Type == ChannelType.GuildTextChat || c.Type == ChannelType.News).OrderBy(c => c.Name).ToArray();

            ChatLog[] chatLogs = new ChatLog[channels.Count];

            // Loop through channels
            for (int i = 0; i < channels.Count; i++)
            {
                try
                {
                    // Track progress
                    Console.Write($"Exporting channel [{channels[i].Name}]... ");
                    using (InlineProgress progress = new InlineProgress())
                    {
                        // Get chat log
                        chatLogs[i] = await dataService.GetChatLogAsync(Options.GetToken(), guild, channels[i],
                                                                        Options.After, Options.Before, progress);

                        // Generate default file name
                        string fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, guild,
                                                                                chatLogs[i].Channel, Options.After, Options.Before);

                        // Generate file path
                        string filePath = Path.Combine(Options.OutputPath ?? "", fileName);

                        // Export
                        await exportService.ExportChatLogAsync(chatLogs[i], filePath, Options.ExportFormat,
                                                               Options.PartitionLimit, Options.BucketName);

                        // Report successful completion
                        progress.ReportCompletion();
                    }
                }
                catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.Forbidden)
                {
                    Console.Error.WriteLine("You don't have access to this channel");
                }
                catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
                {
                    Console.Error.WriteLine("This channel doesn't exist");
                }
            }
        }
        public async void ExportChannels()
        {
            // Get last used token
            var token = _settingsService.LastToken;

            // Create dialog
            var dialog = _viewModelFactory.CreateExportSetupViewModel(SelectedGuild, SelectedChannels);

            // Show dialog, if canceled - return
            if (await _dialogManager.ShowDialogAsync(dialog) != true)
            {
                return;
            }

            // Create a progress operation for each channel to export
            var operations = ProgressManager.CreateOperations(dialog.Channels.Count);

            // Export channels
            var successfulExportCount = 0;

            for (var i = 0; i < dialog.Channels.Count; i++)
            {
                // Get operation and channel
                var operation = operations[i];
                var channel   = dialog.Channels[i];

                try
                {
                    // Generate file path if necessary
                    var filePath = dialog.OutputPath;
                    if (ExportHelper.IsDirectoryPath(filePath))
                    {
                        // Generate default file name
                        var fileName = ExportHelper.GetDefaultExportFileName(dialog.SelectedFormat, dialog.Guild,
                                                                             channel, dialog.After, dialog.Before);

                        // Combine paths
                        filePath = Path.Combine(filePath, fileName);
                    }

                    // Get chat log
                    var chatLog = await _dataService.GetChatLogAsync(token, dialog.Guild, channel,
                                                                     dialog.After, dialog.Before, operation);

                    // Export
                    await _exportService.ExportChatLogAsync(chatLog, filePath, dialog.SelectedFormat,
                                                            dialog.PartitionLimit);

                    // Report successful export
                    successfulExportCount++;
                }
                catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.Forbidden)
                {
                    Notifications.Enqueue($"You don't have access to channel [{channel.Model.Name}]");
                }
                catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
                {
                    Notifications.Enqueue($"Channel [{channel.Model.Name}] doesn't exist");
                }
                finally
                {
                    // Dispose progress operation
                    operation.Dispose();
                }
            }

            // Notify of overall completion
            Notifications.Enqueue($"Successfully exported {successfulExportCount} channel(s)");
        }
        protected async ValueTask ExportMultipleAsync(IConsole console, IReadOnlyList <Channel> channels)
        {
            // This uses a separate route from ExportCommandBase because the progress ticker is not thread-safe
            // Ugly code ahead. Will need to refactor.

            if (!string.IsNullOrWhiteSpace(DateFormat))
            {
                SettingsService.DateFormat = DateFormat;
            }

            // Progress
            console.Output.Write($"Exporting {channels.Count} channels... ");
            var ticker = console.CreateProgressTicker();

            // TODO: refactor this after improving Gress
            var progressManager = new ProgressManager();

            progressManager.PropertyChanged += (sender, args) => ticker.Report(progressManager.Progress);

            var operations = progressManager.CreateOperations(channels.Count);

            // Export channels
            using var semaphore = new SemaphoreSlim(ParallelLimit.ClampMin(1));

            var errors = new List <string>();

            await Task.WhenAll(channels.Select(async(channel, i) =>
            {
                var operation = operations[i];
                await semaphore.WaitAsync();

                var guild = await DataService.GetGuildAsync(Token, channel.GuildId);

                try
                {
                    await ExportService.ExportChatLogAsync(Token, guild, channel,
                                                           OutputPath, ExportFormat, PartitionLimit,
                                                           After, Before, operation);
                }
                catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.Forbidden)
                {
                    errors.Add("You don't have access to this channel.");
                }
                catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
                {
                    errors.Add("This channel doesn't exist.");
                }
                catch (DomainException ex)
                {
                    errors.Add(ex.Message);
                }
                finally
                {
                    semaphore.Release();
                    operation.Dispose();
                }
            }));

            ticker.Report(1);
            console.Output.WriteLine();

            foreach (var error in errors)
            {
                console.Error.WriteLine(error);
            }

            console.Output.WriteLine("Done.");
        }