jellyfin/MediaBrowser.Providers/Plugins/Omdb/OmdbImageProvider.cs

101 lines
3.6 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System.Collections.Generic;
2020-08-17 12:10:02 -07:00
using System.Net.Http;
2020-08-07 10:26:28 -07:00
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common;
2020-08-31 10:05:21 -07:00
using MediaBrowser.Common.Net;
2016-06-03 19:56:04 -07:00
using MediaBrowser.Controller.Configuration;
2014-10-06 16:58:46 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
2014-10-06 16:58:46 -07:00
using MediaBrowser.Model.Providers;
2016-06-03 19:56:04 -07:00
using MediaBrowser.Model.Serialization;
2014-10-06 16:58:46 -07:00
2020-03-09 07:36:02 -07:00
namespace MediaBrowser.Providers.Plugins.Omdb
2014-10-06 16:58:46 -07:00
{
public class OmdbImageProvider : IRemoteImageProvider, IHasOrder
{
2020-08-17 12:10:02 -07:00
private readonly IHttpClientFactory _httpClientFactory;
2016-06-03 19:56:04 -07:00
private readonly IJsonSerializer _jsonSerializer;
private readonly IFileSystem _fileSystem;
private readonly IServerConfigurationManager _configurationManager;
2018-09-12 10:26:21 -07:00
private readonly IApplicationHost _appHost;
2014-10-06 16:58:46 -07:00
2020-08-17 12:10:02 -07:00
public OmdbImageProvider(IJsonSerializer jsonSerializer, IApplicationHost appHost, IHttpClientFactory httpClientFactory, IFileSystem fileSystem, IServerConfigurationManager configurationManager)
2014-10-06 16:58:46 -07:00
{
2016-06-03 19:56:04 -07:00
_jsonSerializer = jsonSerializer;
2020-08-17 12:10:02 -07:00
_httpClientFactory = httpClientFactory;
2016-06-03 19:56:04 -07:00
_fileSystem = fileSystem;
_configurationManager = configurationManager;
2018-09-12 10:26:21 -07:00
_appHost = appHost;
2014-10-06 16:58:46 -07:00
}
2020-09-07 04:20:39 -07:00
public string Name => "The Open Movie Database";
// After other internet providers, because they're better
// But before fallback providers like screengrab
public int Order => 90;
2018-09-12 10:26:21 -07:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
2014-10-06 16:58:46 -07:00
{
return new List<ImageType>
{
ImageType.Primary
};
}
2018-09-12 10:26:21 -07:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
2014-10-06 16:58:46 -07:00
{
2020-06-06 12:17:49 -07:00
var imdbId = item.GetProviderId(MetadataProvider.Imdb);
2014-10-06 16:58:46 -07:00
var list = new List<RemoteImageInfo>();
2020-08-17 12:10:02 -07:00
var provider = new OmdbProvider(_jsonSerializer, _httpClientFactory, _fileSystem, _appHost, _configurationManager);
2016-06-03 19:56:04 -07:00
if (!string.IsNullOrWhiteSpace(imdbId))
2014-10-06 16:58:46 -07:00
{
2019-01-13 13:37:13 -07:00
var rootObject = await provider.GetRootObject(imdbId, cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrEmpty(rootObject.Poster))
2014-10-06 16:58:46 -07:00
{
2017-06-08 11:39:04 -07:00
if (item is Episode)
{
2018-09-12 10:26:21 -07:00
// img.omdbapi.com is returning 404's
2017-06-08 11:39:04 -07:00
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = rootObject.Poster
});
}
else
{
list.Add(new RemoteImageInfo
{
ProviderName = Name,
2020-08-07 10:26:28 -07:00
Url = string.Format(CultureInfo.InvariantCulture, "https://img.omdbapi.com/?i={0}&apikey=2c9d9507", imdbId)
2017-06-08 11:39:04 -07:00
});
}
}
2014-10-06 16:58:46 -07:00
}
return list;
2014-10-06 16:58:46 -07:00
}
2020-08-17 12:10:02 -07:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
2014-10-06 16:58:46 -07:00
{
2020-08-31 10:05:21 -07:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
2014-10-06 16:58:46 -07:00
}
2018-09-12 10:26:21 -07:00
public bool Supports(BaseItem item)
2014-10-06 16:58:46 -07:00
{
return item is Movie || item is Trailer || item is Episode;
2014-10-06 16:58:46 -07:00
}
}
}