StudiosImageProvider.cs 7.1 KB

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