OmdbImageProvider.cs 3.4 KB

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