2
0

StudiosImageProvider.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Providers;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Providers;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using MediaBrowser.Model.IO;
  13. using System;
  14. using MediaBrowser.Common.Progress;
  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
  29. {
  30. get { return "Emby Designs"; }
  31. }
  32. public bool Supports(BaseItem item)
  33. {
  34. return item is Studio;
  35. }
  36. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  37. {
  38. return new List<ImageType>
  39. {
  40. ImageType.Primary,
  41. ImageType.Thumb
  42. };
  43. }
  44. public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
  45. {
  46. return GetImages(item, true, true, cancellationToken);
  47. }
  48. private async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, bool posters, bool thumbs, CancellationToken cancellationToken)
  49. {
  50. var list = new List<RemoteImageInfo>();
  51. if (posters)
  52. {
  53. var posterPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudioposters.txt");
  54. posterPath = await EnsurePosterList(posterPath, cancellationToken).ConfigureAwait(false);
  55. list.Add(GetImage(item, posterPath, ImageType.Primary, "folder"));
  56. }
  57. cancellationToken.ThrowIfCancellationRequested();
  58. if (thumbs)
  59. {
  60. var thumbsPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudiothumbs.txt");
  61. thumbsPath = await EnsureThumbsList(thumbsPath, cancellationToken).ConfigureAwait(false);
  62. list.Add(GetImage(item, thumbsPath, ImageType.Thumb, "thumb"));
  63. }
  64. return list.Where(i => i != null);
  65. }
  66. private RemoteImageInfo GetImage(BaseItem item, string filename, ImageType type, string remoteFilename)
  67. {
  68. var list = GetAvailableImages(filename, _fileSystem);
  69. var match = FindMatch(item, list);
  70. if (!string.IsNullOrEmpty(match))
  71. {
  72. var url = GetUrl(match, remoteFilename);
  73. return new RemoteImageInfo
  74. {
  75. ProviderName = Name,
  76. Type = type,
  77. Url = url
  78. };
  79. }
  80. return null;
  81. }
  82. private string GetUrl(string image, string filename)
  83. {
  84. return string.Format("https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studios/{0}/{1}.jpg", image, filename);
  85. }
  86. private Task<string> EnsureThumbsList(string file, CancellationToken cancellationToken)
  87. {
  88. const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studiothumbs.txt";
  89. return EnsureList(url, file, _httpClient, _fileSystem, cancellationToken);
  90. }
  91. private Task<string> EnsurePosterList(string file, CancellationToken cancellationToken)
  92. {
  93. const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studioposters.txt";
  94. return EnsureList(url, file, _httpClient, _fileSystem, cancellationToken);
  95. }
  96. public int Order
  97. {
  98. get { return 0; }
  99. }
  100. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  101. {
  102. return _httpClient.GetResponse(new HttpRequestOptions
  103. {
  104. CancellationToken = cancellationToken,
  105. Url = url,
  106. BufferContent = false
  107. });
  108. }
  109. /// <summary>
  110. /// Ensures the list.
  111. /// </summary>
  112. /// <param name="url">The URL.</param>
  113. /// <param name="file">The file.</param>
  114. /// <param name="httpClient">The HTTP client.</param>
  115. /// <param name="fileSystem">The file system.</param>
  116. /// <param name="cancellationToken">The cancellation token.</param>
  117. /// <returns>Task.</returns>
  118. public async Task<string> EnsureList(string url, string file, IHttpClient httpClient, IFileSystem fileSystem, CancellationToken cancellationToken)
  119. {
  120. var fileInfo = fileSystem.GetFileInfo(file);
  121. if (!fileInfo.Exists || (DateTime.UtcNow - fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays > 1)
  122. {
  123. var temp = await httpClient.GetTempFile(new HttpRequestOptions
  124. {
  125. CancellationToken = cancellationToken,
  126. Progress = new SimpleProgress<double>(),
  127. Url = url
  128. }).ConfigureAwait(false);
  129. fileSystem.CreateDirectory(fileSystem.GetDirectoryName(file));
  130. try
  131. {
  132. fileSystem.CopyFile(temp, file, true);
  133. }
  134. catch
  135. {
  136. }
  137. return temp;
  138. }
  139. return file;
  140. }
  141. public string FindMatch(BaseItem item, IEnumerable<string> images)
  142. {
  143. var name = GetComparableName(item.Name);
  144. return images.FirstOrDefault(i => string.Equals(name, GetComparableName(i), StringComparison.OrdinalIgnoreCase));
  145. }
  146. private string GetComparableName(string name)
  147. {
  148. return name.Replace(" ", string.Empty)
  149. .Replace(".", string.Empty)
  150. .Replace("&", string.Empty)
  151. .Replace("!", string.Empty)
  152. .Replace(",", string.Empty)
  153. .Replace("/", string.Empty);
  154. }
  155. public IEnumerable<string> GetAvailableImages(string file, IFileSystem fileSystem)
  156. {
  157. using (var fileStream = fileSystem.GetFileStream(file, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
  158. {
  159. using (var reader = new StreamReader(fileStream))
  160. {
  161. var lines = new List<string>();
  162. while (!reader.EndOfStream)
  163. {
  164. var text = reader.ReadLine();
  165. if (!string.IsNullOrWhiteSpace(text))
  166. {
  167. lines.Add(text);
  168. }
  169. }
  170. return lines;
  171. }
  172. }
  173. }
  174. }
  175. }