Ejemplo n.º 1
0
        public async Task <ApiResponse <string> > GenerateExportAsync([FromServices] IConfiguration configuration, [FromServices] IMapper mapper)
        {
            try
            {
                var data = await _repo.GetAsync().ConfigureAwait(false);

                var exportItems   = data.Select(it => mapper.Map <ApiProduct, CsvProduct>(it)).ToList();
                var exportService = new ExportService(configuration, "products.csv");
                await exportService.ExportAsync(exportItems).ConfigureAwait(false);

                return(new ApiResponse <string>("Success"));
            }
            catch (Exception e)
            {
                return(new ApiResponse <string>(e.Message + e.InnerException?.Message + e.GetType().ToString()));
            }
        }
Ejemplo n.º 2
0
        private static async Task ExportAll(ExportAllOptions options)
        {
            var token   = options.Token;
            var format  = options.Format;
            var year    = options.Year;
            var month   = options.Month;
            var allUpTo = options.AllUpTo;

            var settingsService     = new SettingsService();
            var dataService         = new DataService();
            var exportService       = new ExportService(settingsService);
            var messageGroupService = new MessageGroupService(settingsService);

            DateTime?from = allUpTo ? null : (DateTime?)new DateTime(year, month, 1);
            DateTime to   = new DateTime(year, month, 1).AddMonths(1);

            Console.WriteLine("Retrieving servers...");
            var guilds = new List <Guild>(await dataService.GetUserGuildsAsync(token));

            guilds.Add(Guild.DirectMessages);
            foreach (var guild in guilds)
            {
                Console.WriteLine("Retrieving channels for " + guild.Name + "...");
                IReadOnlyList <Channel> channels;
                while (true)
                {
                    try
                    {
                        if (guild == Guild.DirectMessages)
                        {
                            channels = await dataService.GetDirectMessageChannelsAsync(token);
                        }
                        else
                        {
                            channels = await dataService.GetGuildChannelsAsync(token, guild.Id);
                        }

                        break;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);

                        // If we get an error, it might be Discord asking us to kindly back off.
                        // Give them a short break.
                        Thread.Sleep(10000);
                        throw;
                    }
                }
                foreach (var channel in channels)
                {
                    var filePath = "Export/" + guild.Name + "/" + channel.Name + "/" + year + "-" + month + (format == ExportFormat.PlainText ? ".txt" : ".html");

                    // Get messages
                    Console.WriteLine("Retrieving messages for " + guild.Name + ":" + channel.Name);

                    IReadOnlyList <Message> messages = null;
                    while (true)
                    {
                        try
                        {
                            messages = await dataService.GetChannelMessagesAsync(token, channel.Id, from, to);

                            break;
                        }
                        catch (HttpErrorStatusCodeException e)
                        {
                            // This indicates a channel we don't have access to.
                            if (e.StatusCode == HttpStatusCode.Forbidden)
                            {
                                break;
                            }

                            Console.WriteLine(e);

                            // If we get an error, it might be Discord asking us to kindly back off.
                            // Give them a short break.
                            Thread.Sleep(10000);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);

                            // If we get an error, it might be Discord asking us to kindly back off.
                            // Give them a short break.
                            Thread.Sleep(10000);
                            throw;
                        }
                    }
                    if (messages == null)
                    {
                        continue;
                    }

                    // Group them
                    var messageGroups = messageGroupService.GroupMessages(messages);

                    // Create log
                    var log = new ChannelChatLog(guild, channel, messageGroups, messages.Count);

                    // Export
                    Console.WriteLine("Exporting messages...");
                    Directory.CreateDirectory("Export/" + guild.Name + "/" + channel.Name + "/");
                    await exportService.ExportAsync(format, filePath, log);
                }
            }
        }