MovieNfoParser.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. var list = artistVideo.Artists.ToList();
  107. list.Add(artist);
  108. artistVideo.Artists = list.ToArray();
  109. }
  110. break;
  111. case "album":
  112. var album = reader.ReadNormalizedString();
  113. if (!string.IsNullOrEmpty(album) && item is MusicVideo albumVideo)
  114. {
  115. albumVideo.Album = album;
  116. }
  117. break;
  118. default:
  119. base.FetchDataFromXmlNode(reader, itemResult);
  120. break;
  121. }
  122. }
  123. private void ParseSetXml(string xml, Movie movie)
  124. {
  125. // These are not going to be valid xml so no sense in causing the provider to fail and spamming the log with exceptions
  126. try
  127. {
  128. using (var stringReader = new StringReader("<set>" + xml + "</set>"))
  129. using (var reader = XmlReader.Create(stringReader, GetXmlReaderSettings()))
  130. {
  131. reader.MoveToContent();
  132. reader.Read();
  133. // Loop through each element
  134. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  135. {
  136. if (reader.NodeType == XmlNodeType.Element)
  137. {
  138. switch (reader.Name)
  139. {
  140. case "name":
  141. movie.CollectionName = reader.ReadElementContentAsString();
  142. break;
  143. default:
  144. reader.Skip();
  145. break;
  146. }
  147. }
  148. else
  149. {
  150. reader.Read();
  151. }
  152. }
  153. }
  154. }
  155. catch (XmlException)
  156. {
  157. }
  158. }
  159. }
  160. }