TvdbPersonImageProvider.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.Dto;
  12. using MediaBrowser.Controller.Entities;
  13. using MediaBrowser.Controller.Entities.TV;
  14. using MediaBrowser.Controller.Library;
  15. using MediaBrowser.Controller.Providers;
  16. using MediaBrowser.Model.Entities;
  17. using MediaBrowser.Model.IO;
  18. using MediaBrowser.Model.Providers;
  19. using MediaBrowser.Model.Xml;
  20. using MediaBrowser.Providers.TV;
  21. using MediaBrowser.Providers.TV.TheTVDB;
  22. namespace MediaBrowser.Providers.People
  23. {
  24. public class TvdbPersonImageProvider : IRemoteImageProvider, IHasOrder
  25. {
  26. private readonly IServerConfigurationManager _config;
  27. private readonly ILibraryManager _libraryManager;
  28. private readonly IHttpClient _httpClient;
  29. private readonly IFileSystem _fileSystem;
  30. private readonly IXmlReaderSettingsFactory _xmlSettings;
  31. public TvdbPersonImageProvider(IServerConfigurationManager config, ILibraryManager libraryManager, IHttpClient httpClient, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlSettings)
  32. {
  33. _config = config;
  34. _libraryManager = libraryManager;
  35. _httpClient = httpClient;
  36. _fileSystem = fileSystem;
  37. _xmlSettings = xmlSettings;
  38. }
  39. public string Name => ProviderName;
  40. public static string ProviderName => "TheTVDB";
  41. public bool Supports(BaseItem item)
  42. {
  43. return item is Person;
  44. }
  45. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  46. {
  47. return new List<ImageType>
  48. {
  49. ImageType.Primary
  50. };
  51. }
  52. public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
  53. {
  54. var seriesWithPerson = _libraryManager.GetItemList(new InternalItemsQuery
  55. {
  56. IncludeItemTypes = new[] { typeof(Series).Name },
  57. PersonIds = new[] { item.Id },
  58. DtoOptions = new DtoOptions(false)
  59. {
  60. EnableImages = false
  61. }
  62. }).Cast<Series>()
  63. .Where(i => TvdbSeriesProvider.IsValidSeries(i.ProviderIds))
  64. .ToList();
  65. var infos = seriesWithPerson.Select(i => GetImageFromSeriesData(i, item.Name, cancellationToken))
  66. .Where(i => i != null)
  67. .Take(1);
  68. return Task.FromResult(infos);
  69. }
  70. private RemoteImageInfo GetImageFromSeriesData(Series series, string personName, CancellationToken cancellationToken)
  71. {
  72. //var actorsResult = await _tvDbClient.Series.GetActorsAsync(Convert.ToInt32(tvdbId), cancellationToken);
  73. return null; // GetImageInfo(actorXmlPath, personName, cancellationToken);
  74. }
  75. private RemoteImageInfo GetImageInfo(string xmlFile, string personName, CancellationToken cancellationToken)
  76. {
  77. var settings = _xmlSettings.Create(false);
  78. settings.CheckCharacters = false;
  79. settings.IgnoreProcessingInstructions = true;
  80. settings.IgnoreComments = true;
  81. using (var fileStream = _fileSystem.GetFileStream(xmlFile, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
  82. {
  83. using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
  84. {
  85. // Use XmlReader for best performance
  86. using (var reader = XmlReader.Create(streamReader, settings))
  87. {
  88. reader.MoveToContent();
  89. reader.Read();
  90. // Loop through each element
  91. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  92. {
  93. cancellationToken.ThrowIfCancellationRequested();
  94. if (reader.NodeType == XmlNodeType.Element)
  95. {
  96. switch (reader.Name)
  97. {
  98. case "Actor":
  99. {
  100. if (reader.IsEmptyElement)
  101. {
  102. reader.Read();
  103. continue;
  104. }
  105. using (var subtree = reader.ReadSubtree())
  106. {
  107. var info = FetchImageInfoFromActorNode(personName, subtree);
  108. if (info != null)
  109. {
  110. return info;
  111. }
  112. }
  113. break;
  114. }
  115. default:
  116. reader.Skip();
  117. break;
  118. }
  119. }
  120. else
  121. {
  122. reader.Read();
  123. }
  124. }
  125. }
  126. }
  127. }
  128. return null;
  129. }
  130. /// <summary>
  131. /// Fetches the data from actor node.
  132. /// </summary>
  133. /// <param name="personName">Name of the person.</param>
  134. /// <param name="reader">The reader.</param>
  135. /// <returns>System.String.</returns>
  136. private RemoteImageInfo FetchImageInfoFromActorNode(string personName, XmlReader reader)
  137. {
  138. string name = null;
  139. string image = null;
  140. reader.MoveToContent();
  141. reader.Read();
  142. // Loop through each element
  143. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  144. {
  145. if (reader.NodeType == XmlNodeType.Element)
  146. {
  147. switch (reader.Name)
  148. {
  149. case "Name":
  150. {
  151. name = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
  152. break;
  153. }
  154. case "Image":
  155. {
  156. image = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
  157. break;
  158. }
  159. default:
  160. reader.Skip();
  161. break;
  162. }
  163. }
  164. else
  165. {
  166. reader.Read();
  167. }
  168. }
  169. if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(image) &&
  170. string.Equals(name, personName, StringComparison.OrdinalIgnoreCase))
  171. {
  172. return new RemoteImageInfo
  173. {
  174. Url = TVUtils.BannerUrl + image,
  175. Type = ImageType.Primary,
  176. ProviderName = Name
  177. };
  178. }
  179. return null;
  180. }
  181. public int Order => 1;
  182. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  183. {
  184. return _httpClient.GetResponse(new HttpRequestOptions
  185. {
  186. CancellationToken = cancellationToken,
  187. Url = url
  188. });
  189. }
  190. }
  191. }