MovieNfoParser.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Xml;
  5. using MediaBrowser.Common.Configuration;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Entities.Movies;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Providers;
  10. using MediaBrowser.Model.Entities;
  11. using Microsoft.Extensions.Logging;
  12. namespace MediaBrowser.XbmcMetadata.Parsers
  13. {
  14. /// <summary>
  15. /// Nfo parser for movies.
  16. /// </summary>
  17. public class MovieNfoParser : BaseNfoParser<Video>
  18. {
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="MovieNfoParser"/> class.
  21. /// </summary>
  22. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  23. /// <param name="config">Instance of the <see cref="IConfigurationManager"/> interface.</param>
  24. /// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
  25. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  26. /// <param name="userDataManager">Instance of the <see cref="IUserDataManager"/> interface.</param>
  27. /// <param name="directoryService">Instance of the <see cref="DirectoryService"/> interface.</param>
  28. public MovieNfoParser(
  29. ILogger logger,
  30. IConfigurationManager config,
  31. IProviderManager providerManager,
  32. IUserManager userManager,
  33. IUserDataManager userDataManager,
  34. IDirectoryService directoryService)
  35. : base(logger, config, providerManager, userManager, userDataManager, directoryService)
  36. {
  37. }
  38. /// <inheritdoc />
  39. protected override bool SupportsUrlAfterClosingXmlTag => true;
  40. /// <inheritdoc />
  41. protected override void FetchDataFromXmlNode(XmlReader reader, MetadataResult<Video> itemResult)
  42. {
  43. var item = itemResult.Item;
  44. switch (reader.Name)
  45. {
  46. case "id":
  47. {
  48. // get ids from attributes
  49. string? imdbId = reader.GetAttribute("IMDB");
  50. string? tmdbId = reader.GetAttribute("TMDB");
  51. // read id from content
  52. var contentId = reader.ReadElementContentAsString();
  53. if (contentId.Contains("tt", StringComparison.Ordinal) && string.IsNullOrEmpty(imdbId))
  54. {
  55. imdbId = contentId;
  56. }
  57. else if (string.IsNullOrEmpty(tmdbId))
  58. {
  59. tmdbId = contentId;
  60. }
  61. if (!string.IsNullOrWhiteSpace(imdbId))
  62. {
  63. item.SetProviderId(MetadataProvider.Imdb, imdbId);
  64. }
  65. if (!string.IsNullOrWhiteSpace(tmdbId))
  66. {
  67. item.SetProviderId(MetadataProvider.Tmdb, tmdbId);
  68. }
  69. break;
  70. }
  71. case "set":
  72. {
  73. var movie = item as Movie;
  74. var tmdbcolid = reader.GetAttribute("tmdbcolid");
  75. if (!string.IsNullOrWhiteSpace(tmdbcolid) && movie != null)
  76. {
  77. movie.SetProviderId(MetadataProvider.TmdbCollection, tmdbcolid);
  78. }
  79. var val = reader.ReadInnerXml();
  80. if (!string.IsNullOrWhiteSpace(val) && movie != null)
  81. {
  82. // TODO Handle this better later
  83. if (!val.Contains('<', StringComparison.Ordinal))
  84. {
  85. movie.CollectionName = val;
  86. }
  87. else
  88. {
  89. try
  90. {
  91. ParseSetXml(val, movie);
  92. }
  93. catch (Exception ex)
  94. {
  95. Logger.LogError(ex, "Error parsing set node");
  96. }
  97. }
  98. }
  99. break;
  100. }
  101. case "artist":
  102. {
  103. var val = reader.ReadElementContentAsString();
  104. if (!string.IsNullOrWhiteSpace(val) && item is MusicVideo movie)
  105. {
  106. var list = movie.Artists.ToList();
  107. list.Add(val);
  108. movie.Artists = list.ToArray();
  109. }
  110. break;
  111. }
  112. case "album":
  113. {
  114. var val = reader.ReadElementContentAsString();
  115. if (!string.IsNullOrWhiteSpace(val) && item is MusicVideo movie)
  116. {
  117. movie.Album = val;
  118. }
  119. break;
  120. }
  121. default:
  122. base.FetchDataFromXmlNode(reader, itemResult);
  123. break;
  124. }
  125. }
  126. private void ParseSetXml(string xml, Movie movie)
  127. {
  128. // These are not going to be valid xml so no sense in causing the provider to fail and spamming the log with exceptions
  129. try
  130. {
  131. using (var stringReader = new StringReader("<set>" + xml + "</set>"))
  132. using (var reader = XmlReader.Create(stringReader, GetXmlReaderSettings()))
  133. {
  134. reader.MoveToContent();
  135. reader.Read();
  136. // Loop through each element
  137. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  138. {
  139. if (reader.NodeType == XmlNodeType.Element)
  140. {
  141. switch (reader.Name)
  142. {
  143. case "name":
  144. movie.CollectionName = reader.ReadElementContentAsString();
  145. break;
  146. default:
  147. reader.Skip();
  148. break;
  149. }
  150. }
  151. else
  152. {
  153. reader.Read();
  154. }
  155. }
  156. }
  157. }
  158. catch (XmlException)
  159. {
  160. }
  161. }
  162. }
  163. }