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 IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  36. {
  37. return new List<ImageType>
  38. {
  39. ImageType.Primary
  40. };
  41. }
  42. public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
  43. {
  44. var imdbId = item.GetProviderId(MetadataProvider.Imdb);
  45. var list = new List<RemoteImageInfo>();
  46. var provider = new OmdbProvider(_jsonSerializer, _httpClientFactory, _fileSystem, _appHost, _configurationManager);
  47. if (!string.IsNullOrWhiteSpace(imdbId))
  48. {
  49. var rootObject = await provider.GetRootObject(imdbId, cancellationToken).ConfigureAwait(false);
  50. if (!string.IsNullOrEmpty(rootObject.Poster))
  51. {
  52. if (item is Episode)
  53. {
  54. // img.omdbapi.com is returning 404's
  55. list.Add(new RemoteImageInfo
  56. {
  57. ProviderName = Name,
  58. Url = rootObject.Poster
  59. });
  60. }
  61. else
  62. {
  63. list.Add(new RemoteImageInfo
  64. {
  65. ProviderName = Name,
  66. Url = string.Format(CultureInfo.InvariantCulture, "https://img.omdbapi.com/?i={0}&apikey=2c9d9507", imdbId)
  67. });
  68. }
  69. }
  70. }
  71. return list;
  72. }
  73. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  74. {
  75. return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
  76. }
  77. public string Name => "The Open Movie Database";
  78. public bool Supports(BaseItem item)
  79. {
  80. return item is Movie || item is Trailer || item is Episode;
  81. }
  82. // After other internet providers, because they're better
  83. // But before fallback providers like screengrab
  84. public int Order => 90;
  85. }
  86. }