2020-02-09 02:35:16 +00:00
|
|
|
using System;
|
2018-03-10 08:05:56 +00:00
|
|
|
using System.Text;
|
2017-06-28 05:31:38 +00:00
|
|
|
using System.Threading.Tasks;
|
2018-03-10 08:05:56 +00:00
|
|
|
using Jackett.Common.Services.Interfaces;
|
|
|
|
using Jackett.Common.Utils.Clients;
|
2017-06-29 05:53:25 +00:00
|
|
|
using Newtonsoft.Json;
|
2017-06-28 05:31:38 +00:00
|
|
|
|
2018-03-10 08:05:56 +00:00
|
|
|
namespace Jackett.Common.Services
|
2017-06-28 05:31:38 +00:00
|
|
|
{
|
|
|
|
|
2017-06-29 05:53:25 +00:00
|
|
|
public struct Movie
|
2017-06-28 05:31:38 +00:00
|
|
|
{
|
2017-06-29 05:53:25 +00:00
|
|
|
public string Title;
|
2017-07-01 16:23:57 +00:00
|
|
|
public string Year;
|
2017-06-29 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class OmdbResolver : IImdbResolver
|
|
|
|
{
|
2020-04-03 00:31:39 +00:00
|
|
|
public OmdbResolver(WebClient webClient, string omdbApiKey, string omdbApiUrl)
|
2017-06-28 05:31:38 +00:00
|
|
|
{
|
|
|
|
WebClient = webClient;
|
2020-04-03 00:31:39 +00:00
|
|
|
apiKey = omdbApiKey ?? throw new ArgumentNullException($"{nameof(omdbApiKey)} cannot be null");
|
2018-07-09 10:37:24 +00:00
|
|
|
url = omdbApiUrl;
|
2017-06-28 05:31:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 00:31:39 +00:00
|
|
|
public async Task<Movie> MovieForId(string id)
|
2017-06-28 05:31:38 +00:00
|
|
|
{
|
2020-04-03 00:31:39 +00:00
|
|
|
var imdbId = id ?? throw new ArgumentNullException($"{nameof(id)} cannot be null");
|
2017-06-29 05:53:25 +00:00
|
|
|
|
2017-06-28 05:31:38 +00:00
|
|
|
if (!imdbId.StartsWith("tt", StringComparison.Ordinal))
|
|
|
|
imdbId = "tt" + imdbId;
|
2017-07-01 16:23:57 +00:00
|
|
|
|
2018-07-09 10:37:24 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
|
|
url = "http://omdbapi.com";
|
|
|
|
|
2020-03-26 22:15:28 +00:00
|
|
|
var request = new WebRequest(url + "/?apikey=" + apiKey + "&i=" + imdbId)
|
|
|
|
{
|
|
|
|
Encoding = Encoding.UTF8
|
|
|
|
};
|
2020-06-11 15:09:27 +00:00
|
|
|
var result = await WebClient.GetResultAsync(request);
|
2020-06-09 17:36:57 +00:00
|
|
|
var movie = JsonConvert.DeserializeObject<Movie>(result.ContentString);
|
2017-06-28 05:31:38 +00:00
|
|
|
|
2017-07-01 16:23:57 +00:00
|
|
|
return movie;
|
2017-06-28 05:31:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
private readonly WebClient WebClient;
|
|
|
|
private readonly string apiKey;
|
2018-07-09 10:37:24 +00:00
|
|
|
private string url;
|
2017-06-28 05:31:38 +00:00
|
|
|
}
|
|
|
|
}
|