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,
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.");
}
}

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 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<bool, string> 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;
}

View File

@ -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
{

View File

@ -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();