OmdbImageProvider.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.Net;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Entities.Movies;
  12. using MediaBrowser.Controller.Entities.TV;
  13. using MediaBrowser.Controller.Providers;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Model.Providers;
  17. namespace MediaBrowser.Providers.Plugins.Omdb
  18. {
  19. public class OmdbImageProvider : IRemoteImageProvider, IHasOrder
  20. {
  21. private readonly IHttpClientFactory _httpClientFactory;
  22. private readonly IFileSystem _fileSystem;
  23. private readonly IServerConfigurationManager _configurationManager;
  24. public OmdbImageProvider(IHttpClientFactory httpClientFactory, IFileSystem fileSystem, IServerConfigurationManager configurationManager)
  25. {
  26. _httpClientFactory = httpClientFactory;
  27. _fileSystem = fileSystem;
  28. _configurationManager = configurationManager;
  29. }
  30. public string Name => "The Open Movie Database";
  31. // After other internet providers, because they're better
  32. // But before fallback providers like screengrab
  33. public int Order => 90;
  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(_httpClientFactory, _fileSystem, _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(NamedClient.Default).GetAsync(url, cancellationToken);
  75. }
  76. public bool Supports(BaseItem item)
  77. {
  78. return item is Movie || item is Trailer || item is Episode;
  79. }
  80. }
  81. }