2017-12-29 03:26:05 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Geekbot.net.Lib;
|
|
|
|
|
using StackExchange.Redis;
|
2018-04-30 23:44:19 +02:00
|
|
|
|
using Utf8Json;
|
2017-12-29 03:26:05 +01:00
|
|
|
|
|
|
|
|
|
namespace Geekbot.net.Commands
|
|
|
|
|
{
|
|
|
|
|
public class Google : ModuleBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
|
|
|
|
private readonly IDatabase _redis;
|
|
|
|
|
|
|
|
|
|
public Google(IErrorHandler errorHandler, IDatabase redis)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler = errorHandler;
|
|
|
|
|
_redis = redis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Command("google", RunMode = RunMode.Async)]
|
|
|
|
|
[Remarks(CommandCategories.Helpers)]
|
|
|
|
|
[Summary("Google Something.")]
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public async Task AskGoogle([Remainder, Summary("SearchText")] string searchText)
|
2017-12-29 03:26:05 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var client = new WebClient())
|
|
|
|
|
{
|
|
|
|
|
var apiKey = _redis.StringGet("googleGraphKey");
|
|
|
|
|
if (!apiKey.HasValue)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("No Google API key has been set, please contact my owner");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var url = new Uri($"https://kgsearch.googleapis.com/v1/entities:search?languages=en&limit=1&query={searchText}&key={apiKey}");
|
|
|
|
|
var responseString = client.DownloadString(url);
|
2018-04-30 23:44:19 +02:00
|
|
|
|
var response = JsonSerializer.Deserialize<GoogleKgApiResponse>(responseString);
|
2017-12-29 03:26:05 +01:00
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
if (!response.ItemListElement.Any())
|
2017-12-29 03:26:05 +01:00
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("No results were found...");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
var data = response.ItemListElement.First().Result;
|
2017-12-29 03:26:05 +01:00
|
|
|
|
var eb = new EmbedBuilder();
|
2018-04-30 23:44:19 +02:00
|
|
|
|
eb.Title = data.Name;
|
|
|
|
|
if(!string.IsNullOrEmpty(data.Description)) eb.WithDescription(data.Description);
|
|
|
|
|
if(!string.IsNullOrEmpty(data.DetailedDescription?.Url)) eb.WithUrl(data.DetailedDescription.Url);
|
|
|
|
|
if(!string.IsNullOrEmpty(data.DetailedDescription?.ArticleBody)) eb.AddField("Details", data.DetailedDescription.ArticleBody);
|
|
|
|
|
if(!string.IsNullOrEmpty(data.Image?.ContentUrl)) eb.WithThumbnailUrl(data.Image.ContentUrl);
|
2017-12-29 03:26:05 +01:00
|
|
|
|
|
|
|
|
|
await ReplyAsync("", false, eb.Build());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public class GoogleKgApiResponse
|
2017-12-29 03:26:05 +01:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public List<GoogleKgApiElement> ItemListElement { get; set; }
|
2017-12-29 03:26:05 +01:00
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public class GoogleKgApiElement
|
2017-12-29 03:26:05 +01:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public GoogleKgApiResult Result { get; set; }
|
|
|
|
|
public double ResultScore { get; set; }
|
2017-12-29 03:26:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public class GoogleKgApiResult
|
2017-12-29 03:26:05 +01:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public string Description { get; set; }
|
|
|
|
|
public GoogleKgApiImage Image { get; set; }
|
|
|
|
|
public GoogleKgApiDetailed DetailedDescription { get; set; }
|
2017-12-29 03:26:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public class GoogleKgApiImage
|
2017-12-29 03:26:05 +01:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public string ContentUrl { get; set; }
|
|
|
|
|
public string Url { get; set; }
|
2017-12-29 03:26:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public class GoogleKgApiDetailed
|
2017-12-29 03:26:05 +01:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public string ArticleBody { get; set; }
|
|
|
|
|
public string Url { get; set; }
|
|
|
|
|
public string License { get; set; }
|
2017-12-29 03:26:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|