OmdbImageProvider.cs 3.6 KB

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