From b97fca787ce6262f1a084135757066e12968da2c Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 28 Apr 2018 17:38:45 +0200 Subject: [PATCH] Ability to change wikipedia instance and make !wiki nicer --- Geekbot.net/Commands/Admin.cs | 18 ++++++++++++++++++ Geekbot.net/Commands/Wikipedia.cs | 31 ++++++++++++++++++++++++------- WikipediaApi/IWikipediaClient.cs | 2 +- WikipediaApi/WikipediaClient.cs | 11 ++++------- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/Geekbot.net/Commands/Admin.cs b/Geekbot.net/Commands/Admin.cs index 681ff99..4e94673 100644 --- a/Geekbot.net/Commands/Admin.cs +++ b/Geekbot.net/Commands/Admin.cs @@ -138,6 +138,24 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } + + [Command("wiki", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Admin)] + [Summary("Change the wikipedia instance (use lang code in xx.wikipedia.org)")] + public async Task setWikiLanguage([Summary("language")] string languageRaw) + { + try + { + var language = languageRaw.ToLower(); + _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("WikiLang", language) }); + + await ReplyAsync($"Now using the {language} wikipedia"); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } [Command("lang", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] diff --git a/Geekbot.net/Commands/Wikipedia.cs b/Geekbot.net/Commands/Wikipedia.cs index e7b36cf..77fb41d 100644 --- a/Geekbot.net/Commands/Wikipedia.cs +++ b/Geekbot.net/Commands/Wikipedia.cs @@ -5,9 +5,9 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Discord.Net; using Geekbot.net.Lib; using HtmlAgilityPack; +using StackExchange.Redis; using WikipediaApi; using WikipediaApi.Page; @@ -17,11 +17,13 @@ namespace Geekbot.net.Commands { private readonly IErrorHandler _errorHandler; private readonly IWikipediaClient _wikipediaClient; - - public Wikipedia(IErrorHandler errorHandler, IWikipediaClient wikipediaClient) + private readonly IDatabase _redis; + + public Wikipedia(IErrorHandler errorHandler, IWikipediaClient wikipediaClient, IDatabase redis) { _errorHandler = errorHandler; _wikipediaClient = wikipediaClient; + _redis = redis; } [Command("wiki", RunMode = RunMode.Async)] @@ -31,7 +33,12 @@ namespace Geekbot.net.Commands { try { - var article = await _wikipediaClient.GetPreview(articleName.Replace(" ", "_")); + var wikiLang = _redis.HashGet($"{Context.Guild.Id}:Settings", "WikiLang").ToString(); + if (string.IsNullOrEmpty(wikiLang)) + { + wikiLang = "en"; + } + var article = await _wikipediaClient.GetPreview(articleName.Replace(" ", "_"), wikiLang); if (article.Type != PageTypes.Standard) { @@ -58,10 +65,20 @@ namespace Geekbot.net.Commands var eb = new EmbedBuilder { Title = article.Title, - Description = article.Extract, + Description = article.Description, ImageUrl = article.Thumbnail?.Source.ToString(), - Url = article.ContentUrls.Desktop.Page.ToString() + Url = article.ContentUrls.Desktop.Page.ToString(), + Color = new Color(246,246,246), + Timestamp = article.Timestamp, + Footer = new EmbedFooterBuilder + { + Text = "Last Edit", + IconUrl = "http://icons.iconarchive.com/icons/sykonist/popular-sites/256/Wikipedia-icon.png" + } }; + + eb.AddField("Description", article.Extract); + if (article.Coordinates != null) eb.AddField("Coordinates", $"{article.Coordinates.lat} Lat {article.Coordinates.lon} Lon"); await ReplyAsync("", false, eb.Build()); } catch (HttpRequestException) @@ -86,7 +103,7 @@ namespace Geekbot.net.Commands var split = node.InnerText.Split(','); var title = split.First(); var desc = string.Join(",", split.Skip(1)); - sb.AppendLine($"**{title}** - {desc}"); + sb.AppendLine($"• **{title}** -{desc}"); } return sb.ToString(); diff --git a/WikipediaApi/IWikipediaClient.cs b/WikipediaApi/IWikipediaClient.cs index 3fad554..4d1dae9 100644 --- a/WikipediaApi/IWikipediaClient.cs +++ b/WikipediaApi/IWikipediaClient.cs @@ -5,6 +5,6 @@ namespace WikipediaApi { public interface IWikipediaClient { - Task GetPreview(string pageName); + Task GetPreview(string pageName, string language = "en"); } } \ No newline at end of file diff --git a/WikipediaApi/WikipediaClient.cs b/WikipediaApi/WikipediaClient.cs index 25bf299..161cbce 100644 --- a/WikipediaApi/WikipediaClient.cs +++ b/WikipediaApi/WikipediaClient.cs @@ -9,17 +9,14 @@ namespace WikipediaApi public class WikipediaClient : IWikipediaClient { private readonly HttpClient _httpClient; - public WikipediaClient(string language = "en") + public WikipediaClient() { - _httpClient = new HttpClient - { - BaseAddress = new Uri($"https://{language}.wikipedia.org") - }; + _httpClient = new HttpClient(); } - public async Task GetPreview(string pageName) + public async Task GetPreview(string pageName, string language = "en") { - var response = await _httpClient.GetAsync($"/api/rest_v1/page/summary/{pageName}"); + var response = await _httpClient.GetAsync($"https://{language}.wikipedia.org/api/rest_v1/page/summary/{pageName}"); response.EnsureSuccessStatusCode(); var stringResponse = await response.Content.ReadAsStringAsync();