OmdbImageProvider.cs 3.4 KB

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