StudiosImageProvider.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Common.Net;
  8. using MediaBrowser.Common.Progress;
  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 IHttpClient _httpClient;
  21. private readonly IFileSystem _fileSystem;
  22. public StudiosImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
  23. {
  24. _config = config;
  25. _httpClient = httpClient;
  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, _fileSystem);
  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, _httpClient, _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, _httpClient, _fileSystem, cancellationToken);
  92. }
  93. public int Order => 0;
  94. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  95. {
  96. return _httpClient.GetResponse(new HttpRequestOptions
  97. {
  98. CancellationToken = cancellationToken,
  99. Url = url,
  100. BufferContent = false
  101. });
  102. }
  103. /// <summary>
  104. /// Ensures the list.
  105. /// </summary>
  106. /// <param name="url">The URL.</param>
  107. /// <param name="file">The file.</param>
  108. /// <param name="httpClient">The HTTP client.</param>
  109. /// <param name="fileSystem">The file system.</param>
  110. /// <param name="cancellationToken">The cancellation token.</param>
  111. /// <returns>Task.</returns>
  112. public async Task<string> EnsureList(string url, string file, IHttpClient httpClient, IFileSystem fileSystem, CancellationToken cancellationToken)
  113. {
  114. var fileInfo = fileSystem.GetFileInfo(file);
  115. if (!fileInfo.Exists || (DateTime.UtcNow - fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays > 1)
  116. {
  117. var temp = await httpClient.GetTempFile(new HttpRequestOptions
  118. {
  119. CancellationToken = cancellationToken,
  120. Progress = new SimpleProgress<double>(),
  121. Url = url
  122. }).ConfigureAwait(false);
  123. Directory.CreateDirectory(Path.GetDirectoryName(file));
  124. try
  125. {
  126. File.Copy(temp, file, true);
  127. }
  128. catch
  129. {
  130. }
  131. return temp;
  132. }
  133. return file;
  134. }
  135. public string FindMatch(BaseItem item, IEnumerable<string> images)
  136. {
  137. var name = GetComparableName(item.Name);
  138. return images.FirstOrDefault(i => string.Equals(name, GetComparableName(i), StringComparison.OrdinalIgnoreCase));
  139. }
  140. private string GetComparableName(string name)
  141. {
  142. return name.Replace(" ", string.Empty)
  143. .Replace(".", string.Empty)
  144. .Replace("&", string.Empty)
  145. .Replace("!", string.Empty)
  146. .Replace(",", string.Empty)
  147. .Replace("/", string.Empty);
  148. }
  149. public IEnumerable<string> GetAvailableImages(string file, IFileSystem fileSystem)
  150. {
  151. using (var fileStream = fileSystem.GetFileStream(file, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
  152. {
  153. using (var reader = new StreamReader(fileStream))
  154. {
  155. var lines = new List<string>();
  156. while (!reader.EndOfStream)
  157. {
  158. var text = reader.ReadLine();
  159. if (!string.IsNullOrWhiteSpace(text))
  160. {
  161. lines.Add(text);
  162. }
  163. }
  164. return lines;
  165. }
  166. }
  167. }
  168. }
  169. }