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