Some fixes, and performance improvements to stream notifications and getstringasync

This commit is contained in:
Kwoth 2016-08-04 20:59:32 +02:00
parent f5fc34ff64
commit ac288e3030
4 changed files with 53 additions and 12 deletions

View File

@ -61,10 +61,31 @@ namespace NadekoBot.Classes
IEnumerable<KeyValuePair<string, string>> headers = null, IEnumerable<KeyValuePair<string, string>> headers = null,
RequestHttpMethod method = RequestHttpMethod.Get) RequestHttpMethod method = RequestHttpMethod.Get)
{ {
if (string.IsNullOrWhiteSpace(url))
using (var streamReader = new StreamReader(await GetResponseStreamAsync(url, headers, method).ConfigureAwait(false))) 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.");
} }
} }

View File

@ -20,18 +20,24 @@ namespace NadekoBot.Modules.Searches.Commands
}; };
private ConcurrentDictionary<string, Tuple<bool, string>> cachedStatuses = new ConcurrentDictionary<string, Tuple<bool, string>>(); private ConcurrentDictionary<string, Tuple<bool, string>> cachedStatuses = new ConcurrentDictionary<string, Tuple<bool, string>>();
private bool FirstPass { get; set; } = true;
public StreamNotifications(DiscordModule module) : base(module) public StreamNotifications(DiscordModule module) : base(module)
{ {
checkTimer.Elapsed += async (s, e) => checkTimer.Elapsed += (s, e) => Task.Run(async () =>
{ {
cachedStatuses.Clear(); cachedStatuses.Clear();
try try
{ {
var streams = SpecificConfigurations.Default.AllConfigs.SelectMany(c => c.ObservingStreams); var streams = SpecificConfigurations.Default.AllConfigs.SelectMany(c => c.ObservingStreams);
if (!streams.Any()) return; 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) foreach (var stream in streams)
{ {
Tuple<bool, string> data; Tuple<bool, string> data;
@ -47,6 +53,8 @@ namespace NadekoBot.Modules.Searches.Commands
if (data.Item1 != stream.LastStatus) if (data.Item1 != stream.LastStatus)
{ {
stream.LastStatus = data.Item1; stream.LastStatus = data.Item1;
if (FirstPass)
continue;
var server = NadekoBot.Client.GetServer(stream.ServerId); var server = NadekoBot.Client.GetServer(stream.ServerId);
var channel = server?.GetChannel(stream.ChannelId); var channel = server?.GetChannel(stream.ChannelId);
if (channel == null) if (channel == null)
@ -66,10 +74,15 @@ namespace NadekoBot.Modules.Searches.Commands
await channel.SendMessage(msg).ConfigureAwait(false); 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 { } catch { }
await ConfigHandler.SaveConfig().ConfigureAwait(false); });
};
//start checking only after ready, because we need all servers to be initialized //start checking only after ready, because we need all servers to be initialized
NadekoBot.OnReady += checkTimer.Start; NadekoBot.OnReady += checkTimer.Start;
} }

View File

@ -300,10 +300,11 @@ $@"🌍 **Weather for** 【{obj["target"]}】
{ {
var items = JObject.Parse(res); var items = JObject.Parse(res);
var sb = new System.Text.StringBuilder(); var sb = new System.Text.StringBuilder();
sb.AppendLine($"`Term:` {items["list"][0]["word"].ToString()}"); var item = items["list"][0];
sb.AppendLine($"`Definition:` {items["list"][0]["definition"].ToString()}"); sb.AppendLine($"`Term:` {item["word"].ToString()}");
sb.Append($"`Link:` <{await items["list"][0]["permalink"].ToString().ShortenUrl().ConfigureAwait(false)}>"); sb.AppendLine($"`Definition:` {item["definition"].ToString()}");
await e.Channel.SendMessage(sb.ToString()); sb.Append($"`Link:` <{item["permalink"].ToString()}>");
await e.Channel.SendMessage(sb.ToString()).ConfigureAwait(false);
} }
catch catch
{ {

View File

@ -117,7 +117,7 @@ namespace NadekoBot
Client = new DiscordClient(new DiscordConfigBuilder() Client = new DiscordClient(new DiscordConfigBuilder()
{ {
MessageCacheSize = 10, MessageCacheSize = 10,
ConnectionTimeout = 180000, ConnectionTimeout = 200000,
LogLevel = LogSeverity.Warning, LogLevel = LogSeverity.Warning,
LogHandler = (s, e) => LogHandler = (s, e) =>
Console.WriteLine($"Severity: {e.Severity}" + Console.WriteLine($"Severity: {e.Severity}" +
@ -197,7 +197,7 @@ namespace NadekoBot
return; return;
} }
#if NADEKO_RELEASE #if NADEKO_RELEASE
await Task.Delay(150000).ConfigureAwait(false); await Task.Delay(180000).ConfigureAwait(false);
#else #else
await Task.Delay(1000).ConfigureAwait(false); await Task.Delay(1000).ConfigureAwait(false);
#endif #endif
@ -228,6 +228,12 @@ namespace NadekoBot
if (string.IsNullOrWhiteSpace(request.Content)) if (string.IsNullOrWhiteSpace(request.Content))
e.Cancel = true; e.Cancel = true;
}; };
#if NADEKO_RELEASE
Client.ClientAPI.SentRequest += (s, e) =>
{
Console.WriteLine($"[Request sent in {e.Milliseconds}]");
};
#endif
PermissionsHandler.Initialize(); PermissionsHandler.Initialize();
NadekoBot.Ready = true; NadekoBot.Ready = true;
NadekoBot.OnReady(); NadekoBot.OnReady();