OmdbImageProvider.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma warning disable CS1591
  2. using System.Collections.Generic;
  3. using System.Net.Http;
  4. using System.Globalization;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Common;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Entities.Movies;
  11. using MediaBrowser.Controller.Entities.TV;
  12. using MediaBrowser.Controller.Providers;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.Providers;
  16. using MediaBrowser.Model.Serialization;
  17. namespace MediaBrowser.Providers.Plugins.Omdb
  18. {
  19. public class OmdbImageProvider : IRemoteImageProvider, IHasOrder
  20. {
  21. private readonly IHttpClientFactory _httpClientFactory;
  22. private readonly IJsonSerializer _jsonSerializer;
  23. private readonly IFileSystem _fileSystem;
  24. private readonly IServerConfigurationManager _configurationManager;
  25. private readonly IApplicationHost _appHost;
  26. public OmdbImageProvider(IJsonSerializer jsonSerializer, IApplicationHost appHost, IHttpClientFactory httpClientFactory, IFileSystem fileSystem, IServerConfigurationManager configurationManager)
  27. {
  28. _jsonSerializer = jsonSerializer;
  29. _httpClientFactory = httpClientFactory;
  30. _fileSystem = fileSystem;
  31. _configurationManager = configurationManager;
  32. _appHost = appHost;
  33. }
  34. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  35. {
  36. return new List<ImageType>
  37. {
  38. ImageType.Primary
  39. };
  40. }
  41. public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
  42. {
  43. var imdbId = item.GetProviderId(MetadataProvider.Imdb);
  44. var list = new List<RemoteImageInfo>();
  45. var provider = new OmdbProvider(_jsonSerializer, _httpClientFactory, _fileSystem, _appHost, _configurationManager);
  46. if (!string.IsNullOrWhiteSpace(imdbId))
  47. {
  48. var rootObject = await provider.GetRootObject(imdbId, cancellationToken).ConfigureAwait(false);
  49. if (!string.IsNullOrEmpty(rootObject.Poster))
  50. {
  51. if (item is Episode)
  52. {
  53. // img.omdbapi.com is returning 404's
  54. list.Add(new RemoteImageInfo
  55. {
  56. ProviderName = Name,
  57. Url = rootObject.Poster
  58. });
  59. }
  60. else
  61. {
  62. list.Add(new RemoteImageInfo
  63. {
  64. ProviderName = Name,
  65. Url = string.Format(CultureInfo.InvariantCulture, "https://img.omdbapi.com/?i={0}&apikey=2c9d9507", imdbId)
  66. });
  67. }
  68. }
  69. }
  70. return list;
  71. }
  72. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  73. {
  74. return _httpClientFactory.CreateClient().GetAsync(url, cancellationToken);
  75. }
  76. public string Name => "The Open Movie Database";
  77. public bool Supports(BaseItem item)
  78. {
  79. return item is Movie || item is Trailer || item is Episode;
  80. }
  81. // After other internet providers, because they're better
  82. // But before fallback providers like screengrab
  83. public int Order => 90;
  84. }
  85. }