MovieNfoParser.cs 6.7 KB

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