From 4c0e8324639522e829d69c643d9bd3e786274bfa Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 3 Mar 2016 10:10:08 +0100 Subject: [PATCH] fixed ~ani and ~mang --- NadekoBot/Classes/NadekoStats.cs | 50 ++++++++++--------- NadekoBot/Classes/SearchHelper.cs | 50 ++++++++++--------- NadekoBot/Commands/LoLCommands.cs | 5 +- NadekoBot/Commands/RequestsCommand.cs | 6 +-- NadekoBot/Modules/Searches.cs | 4 +- NadekoBot/bin/Debug/Discord.Net.Audio.dll | Bin 65536 -> 65536 bytes NadekoBot/bin/Debug/Discord.Net.Commands.dll | Bin 40448 -> 40448 bytes NadekoBot/bin/Debug/Discord.Net.Modules.dll | Bin 35328 -> 35328 bytes NadekoBot/bin/Debug/Discord.Net.dll | Bin 312320 -> 312320 bytes 9 files changed, 60 insertions(+), 55 deletions(-) diff --git a/NadekoBot/Classes/NadekoStats.cs b/NadekoBot/Classes/NadekoStats.cs index d918f51a..d2acb85e 100644 --- a/NadekoBot/Classes/NadekoStats.cs +++ b/NadekoBot/Classes/NadekoStats.cs @@ -13,8 +13,8 @@ namespace NadekoBot { public string BotVersion => $"{Assembly.GetExecutingAssembly().GetName().Name} v{Assembly.GetExecutingAssembly().GetName().Version}"; - private int _commandsRan = 0; - private string _statsCache = ""; + private int commandsRan = 0; + private string statsCache = ""; private readonly Stopwatch statsStopwatch = new Stopwatch(); public int ServerCount { get; private set; } = 0; @@ -98,19 +98,19 @@ namespace NadekoBot { sb.Append($"`Servers: {ServerCount}"); sb.Append($" | TextChannels: {TextChannelsCount}"); sb.AppendLine($" | VoiceChannels: {VoiceChannelsCount}`"); - sb.AppendLine($"`Commands Ran this session: {_commandsRan}`"); + sb.AppendLine($"`Commands Ran this session: {commandsRan}`"); sb.AppendLine($"`Message queue size:{NadekoBot.Client.MessageQueue.Count}`"); sb.AppendLine($"`Greeted {Commands.ServerGreetCommand.Greeted} times.`"); - _statsCache = sb.ToString(); + statsCache = sb.ToString(); }); public string Heap() => Math.Round((double)GC.GetTotalMemory(true) / 1.MiB(), 2).ToString(); public async Task GetStats() { - if (statsStopwatch.Elapsed.Seconds <= 5) return _statsCache; + if (statsStopwatch.Elapsed.Seconds <= 5) return statsCache; await LoadStats(); statsStopwatch.Restart(); - return _statsCache; + return statsCache; } private async Task StartCollecting() { @@ -136,24 +136,26 @@ namespace NadekoBot { } } } - //todo - batch save this - private void StatsCollector_RanCommand(object sender, CommandEventArgs e) { - try { - _commandsRan++; - Classes.DbHandler.Instance.InsertData(new Classes._DataModels.Command { - ServerId = (long)e.Server.Id, - ServerName = e.Server.Name, - ChannelId = (long)e.Channel.Id, - ChannelName = e.Channel.Name, - UserId = (long)e.User.Id, - UserName = e.User.Name, - CommandName = e.Command.Text, - DateAdded = DateTime.Now - }); - } - catch { - Console.WriteLine("Error in ran command DB write."); - } + + private async void StatsCollector_RanCommand(object sender, CommandEventArgs e) { + await Task.Run(() => { + try { + commandsRan++; + Classes.DbHandler.Instance.InsertData(new Classes._DataModels.Command { + ServerId = (long) e.Server.Id, + ServerName = e.Server.Name, + ChannelId = (long) e.Channel.Id, + ChannelName = e.Channel.Name, + UserId = (long) e.User.Id, + UserName = e.User.Name, + CommandName = e.Command.Text, + DateAdded = DateTime.Now + }); + } + catch { + Console.WriteLine("Error in ran command DB write."); + } + }); } } } \ No newline at end of file diff --git a/NadekoBot/Classes/SearchHelper.cs b/NadekoBot/Classes/SearchHelper.cs index 71b30fe2..b3b7baec 100644 --- a/NadekoBot/Classes/SearchHelper.cs +++ b/NadekoBot/Classes/SearchHelper.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; using System.Security.Authentication; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -56,47 +57,51 @@ namespace NadekoBot.Classes { } } - public static async Task GetAnimeQueryResultLink(string query) { + public static async Task GetAnimeData(string query) { if (string.IsNullOrWhiteSpace(query)) throw new ArgumentNullException(nameof(query)); await RefreshAnilistToken(); var link = "http://anilist.co/api/anime/search/" + Uri.EscapeUriString(query); - try { - var headers = new Dictionary {{"'access_token'", "'"+token+"'"}}; - var smallContent = await GetResponseStringAsync(link, headers); - var smallObj = JArray.Parse(smallContent)[0]; - var content = await GetResponseStringAsync("http://anilist.co/api/anime/" + smallObj["id"], headers); + var smallContent = ""; + var cl = new RestSharp.RestClient("http://anilist.co/api"); + var rq = new RestSharp.RestRequest("/anime/search/" + Uri.EscapeUriString(query)); + rq.AddParameter("access_token", token); + smallContent = cl.Execute(rq).Content; + var smallObj = JArray.Parse(smallContent)[0]; - return await Task.Run(() => JsonConvert.DeserializeObject(content)); + rq = new RestSharp.RestRequest("/anime/" + smallObj["id"]); + rq.AddParameter("access_token", token); + var content = cl.Execute(rq).Content; - } - catch (Exception ex) { - Console.WriteLine(ex); - return new AnimeResult(); - } + return await Task.Run(() => JsonConvert.DeserializeObject(content)); } - public static async Task GetMangaQueryResultLink(string query) { + public static async Task GetMangaData(string query) { if (string.IsNullOrWhiteSpace(query)) throw new ArgumentNullException(nameof(query)); await RefreshAnilistToken(); - var link = "http://anilist.co/api/manga/search/" + Uri.EscapeUriString(query); - - var headers = new Dictionary { { "access_token", token } }; - var smallContent = await GetResponseStringAsync(link, headers); + var link = "http://anilist.co/api/anime/search/" + Uri.EscapeUriString(query); + var smallContent = ""; + var cl = new RestSharp.RestClient("http://anilist.co/api"); + var rq = new RestSharp.RestRequest("/manga/search/" + Uri.EscapeUriString(query)); + rq.AddParameter("access_token", token); + smallContent = cl.Execute(rq).Content; var smallObj = JArray.Parse(smallContent)[0]; - var content = await GetResponseStringAsync("http://anilist.co/api/manga/" + smallObj["id"], headers); + + rq = new RestSharp.RestRequest("/manga/" + smallObj["id"]); + rq.AddParameter("access_token", token); + var content = cl.Execute(rq).Content; return await Task.Run(() => JsonConvert.DeserializeObject(content)); } private static async Task RefreshAnilistToken() { - if (DateTime.Now - lastRefreshed > TimeSpan.FromMinutes(29)) - lastRefreshed=DateTime.Now; + if (DateTime.Now - lastRefreshed > TimeSpan.FromMinutes(29)) + lastRefreshed = DateTime.Now; else { return; } @@ -144,10 +149,9 @@ namespace NadekoBot.Classes { if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey)) throw new ArgumentNullException(nameof(query)); - var link = $"https://www.googleapis.com/youtube/v3/search?part=snippet" + - $"&maxResults=1" + + var link = "https://www.googleapis.com/youtube/v3/search?part=snippet" + + "&maxResults=1&type=playlist" + $"&q={Uri.EscapeDataString(query)}" + - $"&type=playlist" + $"&key={NadekoBot.Creds.GoogleAPIKey}"; var response = await GetResponseStringAsync(link); diff --git a/NadekoBot/Commands/LoLCommands.cs b/NadekoBot/Commands/LoLCommands.cs index a41bade0..8705a0ea 100644 --- a/NadekoBot/Commands/LoLCommands.cs +++ b/NadekoBot/Commands/LoLCommands.cs @@ -143,8 +143,7 @@ namespace NadekoBot.Commands { //get skill order data var orderArr = (data["skills"]["mostGames"]["order"] as JArray); - - //todo save this for at least 1 hour + var img = Image.FromFile("data/lol/bg.png"); using (var g = Graphics.FromImage(img)) { g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; @@ -162,7 +161,7 @@ namespace NadekoBot.Commands { champName = "Wukong"; g.DrawString($"{champName}", new Font("Times New Roman", 24, FontStyle.Regular), Brushes.WhiteSmoke, margin + imageSize + margin, margin); //draw champ surname - //todo + //draw skill order float orderFormula = 120 / orderArr.Count; const float orderVerticalSpacing = 10; diff --git a/NadekoBot/Commands/RequestsCommand.cs b/NadekoBot/Commands/RequestsCommand.cs index f5dd5648..7f1037bc 100644 --- a/NadekoBot/Commands/RequestsCommand.cs +++ b/NadekoBot/Commands/RequestsCommand.cs @@ -19,7 +19,7 @@ namespace NadekoBot.Commands { public string GetRequests() { var task = Classes.DbHandler.Instance.GetAllRows(); - string str = "Here are all current requests for NadekoBot:\n\n"; + var str = "Here are all current requests for NadekoBot:\n\n"; foreach (var reqObj in task) { str += $"{reqObj.Id}. by **{reqObj.UserName}** from **{reqObj.ServerName}** at {reqObj.DateAdded.ToLocalTime()}\n" + $"**{reqObj.RequestText}**\n----------\n"; @@ -48,7 +48,7 @@ namespace NadekoBot.Commands { .Description("Requests a feature for nadeko.\n**Usage**: @NadekoBot req new_feature") .Parameter("all", ParameterType.Unparsed) .Do(async e => { - string str = e.Args[0]; + var str = e.Args[0]; try { SaveRequest(e, str); @@ -62,7 +62,7 @@ namespace NadekoBot.Commands { cgb.CreateCommand("lr") .Description("PMs the user all current nadeko requests.") .Do(async e => { - string str = GetRequests(); + var str = await Task.Run(() => GetRequests()); if (str.Trim().Length > 110) await e.User.Send(str); else diff --git a/NadekoBot/Modules/Searches.cs b/NadekoBot/Modules/Searches.cs index cf226373..65c60a32 100644 --- a/NadekoBot/Modules/Searches.cs +++ b/NadekoBot/Modules/Searches.cs @@ -44,7 +44,7 @@ namespace NadekoBot.Modules { if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query")))) return; string result; try { - result = (await SearchHelper.GetAnimeQueryResultLink(e.GetArg("query"))).ToString(); + result = (await SearchHelper.GetAnimeData(e.GetArg("query"))).ToString(); } catch { await e.Channel.SendMessage("Failed to find that anime."); return; @@ -61,7 +61,7 @@ namespace NadekoBot.Modules { if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query")))) return; string result; try { - result = (await SearchHelper.GetMangaQueryResultLink(e.GetArg("query"))).ToString(); + result = (await SearchHelper.GetMangaData(e.GetArg("query"))).ToString(); } catch { await e.Channel.SendMessage("Failed to find that anime."); return; diff --git a/NadekoBot/bin/Debug/Discord.Net.Audio.dll b/NadekoBot/bin/Debug/Discord.Net.Audio.dll index b7c2ab098ea5c8ff00a6093ccd32112a781bd08d..d69a079d82c5dfb452697df3a588d66dca4e16a6 100644 GIT binary patch delta 66 zcmV-I0KNZ!fCPYm1dxaV67jK$`q>Z%HkD8g;7v!Ab9}3X@zXc6l-h_I0uu3)9`za! YrCPRN!7l|xys6ZLs@91uvq<&03h1mKA^-pY delta 66 zcmZo@U}<-SS@RNB_h Wc^0VPy~)-lIgHtN<7SVqJGcPg*&Sy9 diff --git a/NadekoBot/bin/Debug/Discord.Net.Commands.dll b/NadekoBot/bin/Debug/Discord.Net.Commands.dll index 2a9254f9ed577ca68921932f804e66ed5957ba74..d610b8ccf14d346ab36bbfaa522296bad59acf66 100644 GIT binary patch delta 87 zcmZqJ!_=^cX+j5+;Kz+!W{m>3y{mXkUDCa#o)%M2zAo0Z`9htPH)zy!oH pj0_AMn}B!<14B@-OYmi@^S_c02Rp1@`ls>I_YT(0GE<�svdoAS3_) delta 87 zcmZqJ!_=^cX+j56#*K|#W{mmUX37&z0GL@G;s5{u diff --git a/NadekoBot/bin/Debug/Discord.Net.Modules.dll b/NadekoBot/bin/Debug/Discord.Net.Modules.dll index b59068a11c4ac542f203d9f11a81bc49b4bddfe1..50944c6c4e87c2c97e7b67078f49e999c9f9a8eb 100644 GIT binary patch delta 87 zcmZpe!qhN@X+j5+(8rBkO!)%MlJ90Gg?Kr2^UMmnt~&k3<~jK<_;iFmUJqjc0wy4q pVPs&KGY5$47#MS641^||7AgTZW delta 87 zcmZpe!qhN@X+j56#*K|#O!)$TlbWKXIW%2o2xYje+d4mI^PKz_d^#C7u7@!I0TU3* pFfuUAnFGXi3=BcRF2Q*p7N7s#G1qZ{nqrX2N0IxRH`E1i0|1J!Acz0} diff --git a/NadekoBot/bin/Debug/Discord.Net.dll b/NadekoBot/bin/Debug/Discord.Net.dll index 62a21875736f4d81ded51ec04de27b8b29e94953..372ad73e1406012fc2531be6cb4e09046a1bcaf9 100644 GIT binary patch delta 101 zcmZqZ5N_xYp3uQ0__498wTrQ}i>bAXd21Jonyo-e`={6||B9WK>@E&oDj!|8{eUgY zkt!X*kJrN(fPe{zWf&P4=3HZ8n6rb0At=}-_@`O_gTxE|?$dU0C46^rpR#?!4we9J E0N8aV?*IS* delta 101 zcmZqZ5N_xYp3uRRextFgwTrQ}i>bAXd21JonyrA(jJ~r8SH3u`DhPbYJEif-_5-#o zN2+wvZ(I*!00Jf;mSJRIm~)MVVa^T~hM-`V;JfvrC6fhHoz|q=?BAj3sK0%~4we9J E0Q%D=K>z>%