ManualTvdbPersonImageProvider.cs 6.4 KB

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