NadekoBot/NadekoBot.Modules.Searches/Common/OmdbProvider.cs

58 lines
2.1 KiB
C#
Raw Normal View History

2017-07-17 19:42:36 +00:00
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Discord;
using NadekoBot.Extensions;
using NadekoBot.Core.Services;
using Newtonsoft.Json;
2016-10-15 21:14:28 +00:00
2017-07-17 19:42:36 +00:00
namespace NadekoBot.Modules.Searches.Common
2016-10-15 21:14:28 +00:00
{
public static class OmdbProvider
{
2017-06-06 00:13:43 +00:00
private const string queryUrl = "https://omdbapi.nadekobot.me/?t={0}&y=&plot=full&r=json";
2016-10-15 21:14:28 +00:00
2017-05-27 08:19:27 +00:00
public static async Task<OmdbMovie> FindMovie(string name, IGoogleApiService google)
2016-10-15 21:14:28 +00:00
{
using (var http = new HttpClient())
{
var res = await http.GetStringAsync(String.Format(queryUrl,name.Trim().Replace(' ','+'))).ConfigureAwait(false);
var movie = JsonConvert.DeserializeObject<OmdbMovie>(res);
2017-01-03 20:12:21 +00:00
if (movie?.Title == null)
return null;
2017-05-27 08:19:27 +00:00
movie.Poster = await google.ShortenUrl(movie.Poster);
2016-10-15 21:14:28 +00:00
return movie;
}
}
}
public class OmdbMovie
{
public string Title { get; set; }
public string Year { get; set; }
public string ImdbRating { get; set; }
public string ImdbId { get; set; }
public string Genre { get; set; }
public string Plot { get; set; }
public string Poster { get; set; }
2016-12-16 18:43:57 +00:00
public EmbedBuilder GetEmbed() =>
new EmbedBuilder().WithOkColor()
.WithTitle(Title)
.WithUrl($"http://www.imdb.com/title/{ImdbId}/")
.WithDescription(Plot.TrimTo(1000))
.AddField(efb => efb.WithName("Rating").WithValue(ImdbRating).WithIsInline(true))
.AddField(efb => efb.WithName("Genre").WithValue(Genre).WithIsInline(true))
.AddField(efb => efb.WithName("Year").WithValue(Year).WithIsInline(true))
2016-12-16 18:43:57 +00:00
.WithImageUrl(Poster);
2016-10-15 21:14:28 +00:00
public override string ToString() =>
$@"`Title:` {Title}
`Year:` {Year}
`Rating:` {ImdbRating}
`Genre:` {Genre}
`Link:` http://www.imdb.com/title/{ImdbId}/
`Plot:` {Plot}";
}
}