From ac288e3030429d1819be0fb1ea15c990e168a281 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 4 Aug 2016 20:59:32 +0200 Subject: [PATCH] Some fixes, and performance improvements to stream notifications and getstringasync --- NadekoBot/Classes/SearchHelper.cs | 27 ++++++++++++++++--- .../Searches/Commands/StreamNotifications.cs | 19 ++++++++++--- NadekoBot/Modules/Searches/SearchesModule.cs | 9 ++++--- NadekoBot/NadekoBot.cs | 10 +++++-- 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/NadekoBot/Classes/SearchHelper.cs b/NadekoBot/Classes/SearchHelper.cs index a0a59e9b..e5bbcc2e 100644 --- a/NadekoBot/Classes/SearchHelper.cs +++ b/NadekoBot/Classes/SearchHelper.cs @@ -61,10 +61,31 @@ namespace NadekoBot.Classes IEnumerable> headers = null, RequestHttpMethod method = RequestHttpMethod.Get) { - - using (var streamReader = new StreamReader(await GetResponseStreamAsync(url, headers, method).ConfigureAwait(false))) + if (string.IsNullOrWhiteSpace(url)) + throw new ArgumentNullException(nameof(url)); + var cl = new HttpClient(); + cl.DefaultRequestHeaders.Clear(); + switch (method) { - return await streamReader.ReadToEndAsync().ConfigureAwait(false); + case RequestHttpMethod.Get: + if (headers != null) + { + foreach (var header in headers) + { + cl.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value); + } + } + return await cl.GetStringAsync(url).ConfigureAwait(false); + case RequestHttpMethod.Post: + FormUrlEncodedContent formContent = null; + if (headers != null) + { + formContent = new FormUrlEncodedContent(headers); + } + var message = await cl.PostAsync(url, formContent).ConfigureAwait(false); + return await message.Content.ReadAsStringAsync().ConfigureAwait(false); + default: + throw new NotImplementedException("That type of request is unsupported."); } } diff --git a/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs b/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs index 11609db2..ea864960 100644 --- a/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs +++ b/NadekoBot/Modules/Searches/Commands/StreamNotifications.cs @@ -20,18 +20,24 @@ namespace NadekoBot.Modules.Searches.Commands }; private ConcurrentDictionary> cachedStatuses = new ConcurrentDictionary>(); + private bool FirstPass { get; set; } = true; public StreamNotifications(DiscordModule module) : base(module) { - checkTimer.Elapsed += async (s, e) => + checkTimer.Elapsed += (s, e) => Task.Run(async () => { + cachedStatuses.Clear(); try { var streams = SpecificConfigurations.Default.AllConfigs.SelectMany(c => c.ObservingStreams); if (!streams.Any()) return; + var clr = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Blue; + Console.WriteLine($"Getting {streams.Count()} streams."); + Console.ForegroundColor = clr; foreach (var stream in streams) { Tuple data; @@ -47,6 +53,8 @@ namespace NadekoBot.Modules.Searches.Commands if (data.Item1 != stream.LastStatus) { stream.LastStatus = data.Item1; + if (FirstPass) + continue; var server = NadekoBot.Client.GetServer(stream.ServerId); var channel = server?.GetChannel(stream.ChannelId); if (channel == null) @@ -66,10 +74,15 @@ namespace NadekoBot.Modules.Searches.Commands await channel.SendMessage(msg).ConfigureAwait(false); } } + FirstPass = false; + + clr = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Blue; + Console.WriteLine($"Getting {streams.Count()} streams."); + Console.ForegroundColor = clr; } catch { } - await ConfigHandler.SaveConfig().ConfigureAwait(false); - }; + }); //start checking only after ready, because we need all servers to be initialized NadekoBot.OnReady += checkTimer.Start; } diff --git a/NadekoBot/Modules/Searches/SearchesModule.cs b/NadekoBot/Modules/Searches/SearchesModule.cs index 2662107a..4caa708b 100644 --- a/NadekoBot/Modules/Searches/SearchesModule.cs +++ b/NadekoBot/Modules/Searches/SearchesModule.cs @@ -300,10 +300,11 @@ $@"🌍 **Weather for** 【{obj["target"]}】 { var items = JObject.Parse(res); var sb = new System.Text.StringBuilder(); - sb.AppendLine($"`Term:` {items["list"][0]["word"].ToString()}"); - sb.AppendLine($"`Definition:` {items["list"][0]["definition"].ToString()}"); - sb.Append($"`Link:` <{await items["list"][0]["permalink"].ToString().ShortenUrl().ConfigureAwait(false)}>"); - await e.Channel.SendMessage(sb.ToString()); + var item = items["list"][0]; + sb.AppendLine($"`Term:` {item["word"].ToString()}"); + sb.AppendLine($"`Definition:` {item["definition"].ToString()}"); + sb.Append($"`Link:` <{item["permalink"].ToString()}>"); + await e.Channel.SendMessage(sb.ToString()).ConfigureAwait(false); } catch { diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index b706b95d..e5387849 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -117,7 +117,7 @@ namespace NadekoBot Client = new DiscordClient(new DiscordConfigBuilder() { MessageCacheSize = 10, - ConnectionTimeout = 180000, + ConnectionTimeout = 200000, LogLevel = LogSeverity.Warning, LogHandler = (s, e) => Console.WriteLine($"Severity: {e.Severity}" + @@ -197,7 +197,7 @@ namespace NadekoBot return; } #if NADEKO_RELEASE - await Task.Delay(150000).ConfigureAwait(false); + await Task.Delay(180000).ConfigureAwait(false); #else await Task.Delay(1000).ConfigureAwait(false); #endif @@ -228,6 +228,12 @@ namespace NadekoBot if (string.IsNullOrWhiteSpace(request.Content)) e.Cancel = true; }; +#if NADEKO_RELEASE + Client.ClientAPI.SentRequest += (s, e) => + { + Console.WriteLine($"[Request sent in {e.Milliseconds}]"); + }; +#endif PermissionsHandler.Initialize(); NadekoBot.Ready = true; NadekoBot.OnReady();