StudiosImageProvider.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Providers;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.IO;
  14. using MediaBrowser.Model.Providers;
  15. namespace MediaBrowser.Providers.Studios
  16. {
  17. public class StudiosImageProvider : IRemoteImageProvider
  18. {
  19. private readonly IServerConfigurationManager _config;
  20. private readonly IHttpClientFactory _httpClientFactory;
  21. private readonly IFileSystem _fileSystem;
  22. public StudiosImageProvider(IServerConfigurationManager config, IHttpClientFactory httpClientFactory, IFileSystem fileSystem)
  23. {
  24. _config = config;
  25. _httpClientFactory = httpClientFactory;
  26. _fileSystem = fileSystem;
  27. }
  28. public string Name => "Emby Designs";
  29. public bool Supports(BaseItem item)
  30. {
  31. return item is Studio;
  32. }
  33. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  34. {
  35. return new List<ImageType>
  36. {
  37. ImageType.Primary,
  38. ImageType.Thumb
  39. };
  40. }
  41. public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
  42. {
  43. return GetImages(item, true, true, cancellationToken);
  44. }
  45. private async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, bool posters, bool thumbs, CancellationToken cancellationToken)
  46. {
  47. var list = new List<RemoteImageInfo>();
  48. if (posters)
  49. {
  50. var posterPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudioposters.txt");
  51. posterPath = await EnsurePosterList(posterPath, cancellationToken).ConfigureAwait(false);
  52. list.Add(GetImage(item, posterPath, ImageType.Primary, "folder"));
  53. }
  54. cancellationToken.ThrowIfCancellationRequested();
  55. if (thumbs)
  56. {
  57. var thumbsPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudiothumbs.txt");
  58. thumbsPath = await EnsureThumbsList(thumbsPath, cancellationToken).ConfigureAwait(false);
  59. list.Add(GetImage(item, thumbsPath, ImageType.Thumb, "thumb"));
  60. }
  61. return list.Where(i => i != null);
  62. }
  63. private RemoteImageInfo GetImage(BaseItem item, string filename, ImageType type, string remoteFilename)
  64. {
  65. var list = GetAvailableImages(filename);
  66. var match = FindMatch(item, list);
  67. if (!string.IsNullOrEmpty(match))
  68. {
  69. var url = GetUrl(match, remoteFilename);
  70. return new RemoteImageInfo
  71. {
  72. ProviderName = Name,
  73. Type = type,
  74. Url = url
  75. };
  76. }
  77. return null;
  78. }
  79. private string GetUrl(string image, string filename)
  80. {
  81. return string.Format("https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studios/{0}/{1}.jpg", image, filename);
  82. }
  83. private Task<string> EnsureThumbsList(string file, CancellationToken cancellationToken)
  84. {
  85. const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studiothumbs.txt";
  86. return EnsureList(url, file, _fileSystem, cancellationToken);
  87. }
  88. private Task<string> EnsurePosterList(string file, CancellationToken cancellationToken)
  89. {
  90. const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studioposters.txt";
  91. return EnsureList(url, file, _fileSystem, cancellationToken);
  92. }
  93. public int Order => 0;
  94. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  95. {
  96. var httpClient = _httpClientFactory.CreateClient();
  97. return httpClient.GetAsync(url, cancellationToken);
  98. }
  99. /// <summary>
  100. /// Ensures the list.
  101. /// </summary>
  102. /// <param name="url">The URL.</param>
  103. /// <param name="file">The file.</param>
  104. /// <param name="fileSystem">The file system.</param>
  105. /// <param name="cancellationToken">The cancellation token.</param>
  106. /// <returns>Task.</returns>
  107. public async Task<string> EnsureList(string url, string file, IFileSystem fileSystem, CancellationToken cancellationToken)
  108. {
  109. var fileInfo = fileSystem.GetFileInfo(file);
  110. if (!fileInfo.Exists || (DateTime.UtcNow - fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays > 1)
  111. {
  112. var httpClient = _httpClientFactory.CreateClient();
  113. Directory.CreateDirectory(Path.GetDirectoryName(file));
  114. await using var response = await httpClient.GetStreamAsync(url).ConfigureAwait(false);
  115. await using var fileStream = new FileStream(file, FileMode.Create);
  116. await response.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
  117. }
  118. return file;
  119. }
  120. public string FindMatch(BaseItem item, IEnumerable<string> images)
  121. {
  122. var name = GetComparableName(item.Name);
  123. return images.FirstOrDefault(i => string.Equals(name, GetComparableName(i), StringComparison.OrdinalIgnoreCase));
  124. }
  125. private string GetComparableName(string name)
  126. {
  127. return name.Replace(" ", string.Empty)
  128. .Replace(".", string.Empty)
  129. .Replace("&", string.Empty)
  130. .Replace("!", string.Empty)
  131. .Replace(",", string.Empty)
  132. .Replace("/", string.Empty);
  133. }
  134. public IEnumerable<string> GetAvailableImages(string file)
  135. {
  136. using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
  137. {
  138. using (var reader = new StreamReader(fileStream))
  139. {
  140. var lines = new List<string>();
  141. while (!reader.EndOfStream)
  142. {
  143. var text = reader.ReadLine();
  144. if (!string.IsNullOrWhiteSpace(text))
  145. {
  146. lines.Add(text);
  147. }
  148. }
  149. return lines;
  150. }
  151. }
  152. }
  153. }
  154. }