Brough .wiki command to 2018 standards
This commit is contained in:
parent
13fa7bd17b
commit
86b015115a
2 changed files with 135 additions and 46 deletions
|
@ -169,7 +169,9 @@ public partial class Searches : EllieModule<SearchesService>
|
||||||
.AddField("Rating", movie.ImdbRating, true)
|
.AddField("Rating", movie.ImdbRating, true)
|
||||||
.AddField("Genre", movie.Genre, true)
|
.AddField("Genre", movie.Genre, true)
|
||||||
.AddField("Year", movie.Year, true)
|
.AddField("Year", movie.Year, true)
|
||||||
.WithImageUrl(Uri.IsWellFormedUriString(movie.Poster, UriKind.Absolute) ? movie.Poster : null))
|
.WithImageUrl(Uri.IsWellFormedUriString(movie.Poster, UriKind.Absolute)
|
||||||
|
? movie.Poster
|
||||||
|
: null))
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -432,22 +434,36 @@ public partial class Searches : EllieModule<SearchesService>
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
public async Task Wiki([Leftover] string query = null)
|
public async Task Wiki([Leftover] string query)
|
||||||
{
|
{
|
||||||
query = query?.Trim();
|
query = query?.Trim();
|
||||||
|
|
||||||
if (!await ValidateQuery(query))
|
if (!await ValidateQuery(query))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
using var http = _httpFactory.CreateClient();
|
var maybeRes = await _service.GetWikipediaPageAsync(query);
|
||||||
var result = await http.GetStringAsync(
|
if (!maybeRes.TryPickT0(out var res, out var error))
|
||||||
"https://en.wikipedia.org//w/api.php?action=query&format=json&prop=info&redirects=1&formatversion=2&inprop=url&titles="
|
{
|
||||||
+ Uri.EscapeDataString(query));
|
await HandleErrorAsync(error);
|
||||||
var data = JsonConvert.DeserializeObject<WikipediaApiModel>(result);
|
return;
|
||||||
if (data.Query.Pages[0].Missing || string.IsNullOrWhiteSpace(data.Query.Pages[0].FullUrl))
|
}
|
||||||
await Response().Error(strs.wiki_page_not_found).SendAsync();
|
|
||||||
else
|
var data = res.Data;
|
||||||
await Response().Text(data.Query.Pages[0].FullUrl).SendAsync();
|
await Response().Text(data.Url).SendAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<IUserMessage> HandleErrorAsync(ErrorType error)
|
||||||
|
{
|
||||||
|
var errorKey = error switch
|
||||||
|
{
|
||||||
|
ErrorType.ApiKeyMissing => strs.api_key_missing,
|
||||||
|
ErrorType.InvalidInput => strs.invalid_input,
|
||||||
|
ErrorType.NotFound => strs.not_found,
|
||||||
|
ErrorType.Unknown => strs.error_occured,
|
||||||
|
_ => strs.error_occured,
|
||||||
|
};
|
||||||
|
|
||||||
|
return Response().Error(errorKey).SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using EllieBot.Modules.Searches.Common;
|
using EllieBot.Modules.Searches.Common;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using OneOf.Types;
|
||||||
using SixLabors.Fonts;
|
using SixLabors.Fonts;
|
||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
using SixLabors.ImageSharp.Drawing.Processing;
|
using SixLabors.ImageSharp.Drawing.Processing;
|
||||||
|
@ -454,4 +455,76 @@ public class SearchesService : IEService
|
||||||
|
|
||||||
return gamesMap[key];
|
return gamesMap[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<OneOf.OneOf<WikipediaReply, ErrorType>> GetWikipediaPageAsync(string query)
|
||||||
|
{
|
||||||
|
query = query.Trim();
|
||||||
|
if (string.IsNullOrEmpty(query))
|
||||||
|
{
|
||||||
|
return ErrorType.InvalidInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await _c.GetOrAddAsync($"wikipedia_{query}",
|
||||||
|
async _ =>
|
||||||
|
{
|
||||||
|
using var http = _httpFactory.CreateClient();
|
||||||
|
http.DefaultRequestHeaders.Clear();
|
||||||
|
|
||||||
|
return await http.GetStringAsync(
|
||||||
|
"https://en.wikipedia.org/w/api.php?action=query"
|
||||||
|
+ "&format=json"
|
||||||
|
+ "&prop=info"
|
||||||
|
+ "&redirects=1"
|
||||||
|
+ "&formatversion=2"
|
||||||
|
+ "&inprop=url"
|
||||||
|
+ "&titles="
|
||||||
|
+ Uri.EscapeDataString(query));
|
||||||
|
},
|
||||||
|
TimeSpan.FromHours(1))
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
var data = JsonConvert.DeserializeObject<WikipediaApiModel>(result);
|
||||||
|
|
||||||
|
if (data.Query.Pages is null || !data.Query.Pages.Any() || data.Query.Pages.First().Missing)
|
||||||
|
{
|
||||||
|
return ErrorType.NotFound;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.Information("Sending wikipedia url for: {Query}", query);
|
||||||
|
|
||||||
|
return new WikipediaReply
|
||||||
|
{
|
||||||
|
Data = new()
|
||||||
|
{
|
||||||
|
Url = data.Query.Pages[0].FullUrl,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log.Error(ex, "Error retrieving wikipedia data for: '{Query}'", query);
|
||||||
|
|
||||||
|
return ErrorType.Unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ErrorType
|
||||||
|
{
|
||||||
|
InvalidInput,
|
||||||
|
NotFound,
|
||||||
|
Unknown,
|
||||||
|
ApiKeyMissing
|
||||||
|
}
|
||||||
|
|
||||||
|
public class WikipediaReply
|
||||||
|
{
|
||||||
|
public class Info
|
||||||
|
{
|
||||||
|
public required string Url { get; init; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public required Info Data { get; init; }
|
||||||
}
|
}
|
Loading…
Reference in a new issue