TvdbPersonImageProvider.cs 6.8 KB

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