TvdbPersonImageProvider.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. catch (DirectoryNotFoundException)
  75. {
  76. return null;
  77. }
  78. }
  79. private RemoteImageInfo GetImageInfo(string xmlFile, string personName, CancellationToken cancellationToken)
  80. {
  81. var settings = new XmlReaderSettings
  82. {
  83. CheckCharacters = false,
  84. IgnoreProcessingInstructions = true,
  85. IgnoreComments = true,
  86. ValidationType = ValidationType.None
  87. };
  88. using (var streamReader = new StreamReader(xmlFile, Encoding.UTF8))
  89. {
  90. // Use XmlReader for best performance
  91. using (var reader = XmlReader.Create(streamReader, settings))
  92. {
  93. reader.MoveToContent();
  94. // Loop through each element
  95. while (reader.Read())
  96. {
  97. cancellationToken.ThrowIfCancellationRequested();
  98. if (reader.NodeType == XmlNodeType.Element)
  99. {
  100. switch (reader.Name)
  101. {
  102. case "Actor":
  103. {
  104. using (var subtree = reader.ReadSubtree())
  105. {
  106. var info = FetchImageInfoFromActorNode(personName, subtree);
  107. if (info != null)
  108. {
  109. return info;
  110. }
  111. }
  112. break;
  113. }
  114. default:
  115. reader.Skip();
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. return null;
  123. }
  124. /// <summary>
  125. /// Fetches the data from actor node.
  126. /// </summary>
  127. /// <param name="personName">Name of the person.</param>
  128. /// <param name="reader">The reader.</param>
  129. /// <returns>System.String.</returns>
  130. private RemoteImageInfo FetchImageInfoFromActorNode(string personName, XmlReader reader)
  131. {
  132. reader.MoveToContent();
  133. string name = null;
  134. string image = null;
  135. while (reader.Read())
  136. {
  137. if (reader.NodeType == XmlNodeType.Element)
  138. {
  139. switch (reader.Name)
  140. {
  141. case "Name":
  142. {
  143. name = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
  144. break;
  145. }
  146. case "Image":
  147. {
  148. image = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
  149. break;
  150. }
  151. default:
  152. reader.Skip();
  153. break;
  154. }
  155. }
  156. }
  157. if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(image) &&
  158. string.Equals(name, personName, StringComparison.OrdinalIgnoreCase))
  159. {
  160. return new RemoteImageInfo
  161. {
  162. Url = TVUtils.BannerUrl + image,
  163. Type = ImageType.Primary,
  164. ProviderName = Name
  165. };
  166. }
  167. return null;
  168. }
  169. public int Order
  170. {
  171. get { return 1; }
  172. }
  173. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  174. {
  175. return _httpClient.GetResponse(new HttpRequestOptions
  176. {
  177. CancellationToken = cancellationToken,
  178. Url = url,
  179. ResourcePool = TvdbSeriesProvider.Current.TvDbResourcePool
  180. });
  181. }
  182. }
  183. }