2018-04-28 01:01:48 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2018-05-13 21:06:41 +02:00
|
|
|
|
using Geekbot.net.Database;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.ErrorHandling;
|
2018-05-13 21:06:41 +02:00
|
|
|
|
using Geekbot.net.Lib.Extensions;
|
2018-04-28 01:01:48 +02:00
|
|
|
|
using HtmlAgilityPack;
|
|
|
|
|
using WikipediaApi;
|
|
|
|
|
using WikipediaApi.Page;
|
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
namespace Geekbot.net.Commands.Integrations
|
2018-04-28 01:01:48 +02:00
|
|
|
|
{
|
|
|
|
|
public class Wikipedia : ModuleBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
|
|
|
|
private readonly IWikipediaClient _wikipediaClient;
|
2018-05-13 21:06:41 +02:00
|
|
|
|
private readonly DatabaseContext _database;
|
2018-04-28 17:38:45 +02:00
|
|
|
|
|
2018-05-13 21:06:41 +02:00
|
|
|
|
public Wikipedia(IErrorHandler errorHandler, IWikipediaClient wikipediaClient, DatabaseContext database)
|
2018-04-28 01:01:48 +02:00
|
|
|
|
{
|
|
|
|
|
_errorHandler = errorHandler;
|
|
|
|
|
_wikipediaClient = wikipediaClient;
|
2018-05-13 21:06:41 +02:00
|
|
|
|
_database = database;
|
2018-04-28 01:01:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Command("wiki", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Get an article from wikipedia.")]
|
|
|
|
|
public async Task GetPreview([Remainder] [Summary("Article")] string articleName)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-13 21:06:41 +02:00
|
|
|
|
var wikiLang = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(Context.Guild.Id.AsLong()))?.WikiLang;
|
2018-04-28 17:38:45 +02:00
|
|
|
|
if (string.IsNullOrEmpty(wikiLang))
|
|
|
|
|
{
|
|
|
|
|
wikiLang = "en";
|
|
|
|
|
}
|
|
|
|
|
var article = await _wikipediaClient.GetPreview(articleName.Replace(" ", "_"), wikiLang);
|
2018-04-28 01:01:48 +02:00
|
|
|
|
|
|
|
|
|
if (article.Type != PageTypes.Standard)
|
|
|
|
|
{
|
|
|
|
|
switch (article.Type)
|
|
|
|
|
{
|
|
|
|
|
case PageTypes.Disambiguation:
|
|
|
|
|
await ReplyAsync($"**__Disambiguation__**\r\n{DisambiguationExtractor(article.ExtractHtml)}");
|
|
|
|
|
break;
|
|
|
|
|
case PageTypes.MainPage:
|
|
|
|
|
await ReplyAsync("The main page is not supported");
|
|
|
|
|
break;
|
|
|
|
|
case PageTypes.NoExtract:
|
|
|
|
|
await ReplyAsync($"This page has no summary, here is the link: {article.ContentUrls.Desktop.Page}");
|
|
|
|
|
break;
|
|
|
|
|
case PageTypes.Standard:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
await ReplyAsync($"This page type is currently not supported, here is the link: {article.ContentUrls.Desktop.Page}");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var eb = new EmbedBuilder
|
|
|
|
|
{
|
|
|
|
|
Title = article.Title,
|
2018-04-28 17:38:45 +02:00
|
|
|
|
Description = article.Description,
|
2018-04-28 02:46:30 +02:00
|
|
|
|
ImageUrl = article.Thumbnail?.Source.ToString(),
|
2018-04-28 17:38:45 +02:00
|
|
|
|
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"
|
|
|
|
|
}
|
2018-04-28 01:01:48 +02:00
|
|
|
|
};
|
2018-04-28 17:38:45 +02:00
|
|
|
|
|
|
|
|
|
eb.AddField("Description", article.Extract);
|
2018-04-30 23:44:19 +02:00
|
|
|
|
if (article.Coordinates != null) eb.AddField("Coordinates", $"{article.Coordinates.Lat} Lat {article.Coordinates.Lon} Lon");
|
2018-04-28 01:01:48 +02:00
|
|
|
|
await ReplyAsync("", false, eb.Build());
|
|
|
|
|
}
|
2018-04-28 02:46:30 +02:00
|
|
|
|
catch (HttpRequestException)
|
2018-04-28 01:01:48 +02:00
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("I couldn't find that article");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
await _errorHandler.HandleCommandException(e, Context);
|
2018-04-28 01:01:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string DisambiguationExtractor(string extractHtml)
|
|
|
|
|
{
|
|
|
|
|
var doc = new HtmlDocument();
|
|
|
|
|
doc.LoadHtml(extractHtml);
|
|
|
|
|
var nodes = doc.DocumentNode.SelectNodes("//li");
|
2018-04-28 02:46:30 +02:00
|
|
|
|
if (nodes == null) return "(List is to long to show)";
|
2018-04-28 01:01:48 +02:00
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
foreach (var node in nodes)
|
|
|
|
|
{
|
|
|
|
|
var split = node.InnerText.Split(',');
|
|
|
|
|
var title = split.First();
|
|
|
|
|
var desc = string.Join(",", split.Skip(1));
|
2018-04-28 17:38:45 +02:00
|
|
|
|
sb.AppendLine($"• **{title}** -{desc}");
|
2018-04-28 01:01:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|