< Summary - Jellyfin

Information
Class: MediaBrowser.XbmcMetadata.Parsers.MovieNfoParser
Assembly: MediaBrowser.XbmcMetadata
File(s): /srv/git/jellyfin/MediaBrowser.XbmcMetadata/Parsers/MovieNfoParser.cs
Line coverage
90%
Covered lines: 48
Uncovered lines: 5
Coverable lines: 53
Total lines: 160
Line coverage: 90.5%
Branch coverage
97%
Covered branches: 39
Total branches: 40
Branch coverage: 97.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_SupportsUrlAfterClosingXmlTag()100%11100%
FetchDataFromXmlNode(...)96.42%292890%
ParseSetXml(...)100%121290%

File(s)

/srv/git/jellyfin/MediaBrowser.XbmcMetadata/Parsers/MovieNfoParser.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using System.Linq;
 4using System.Xml;
 5using MediaBrowser.Common.Configuration;
 6using MediaBrowser.Controller.Entities;
 7using MediaBrowser.Controller.Entities.Movies;
 8using MediaBrowser.Controller.Extensions;
 9using MediaBrowser.Controller.Library;
 10using MediaBrowser.Controller.Providers;
 11using MediaBrowser.Model.Entities;
 12using Microsoft.Extensions.Logging;
 13
 14namespace 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)
 1137            : base(logger, config, providerManager, userManager, userDataManager, directoryService)
 38        {
 1139        }
 40
 41        /// <inheritdoc />
 742        protected override bool SupportsUrlAfterClosingXmlTag => true;
 43
 44        /// <inheritdoc />
 45        protected override void FetchDataFromXmlNode(XmlReader reader, MetadataResult<Video> itemResult)
 46        {
 16247            var item = itemResult.Item;
 48
 16249            switch (reader.Name)
 50            {
 51                case "id":
 52                    {
 53                        // Get ids from attributes
 254                        item.TrySetProviderId(MetadataProvider.Tmdb, reader.GetAttribute("TMDB"));
 255                        item.TrySetProviderId(MetadataProvider.Tvdb, reader.GetAttribute("TVDB"));
 256                        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
 260                        var contentId = reader.ReadElementContentAsString();
 261                        if (string.IsNullOrEmpty(imdbId) && contentId.StartsWith("tt", StringComparison.Ordinal))
 62                        {
 163                            imdbId = contentId;
 64                        }
 65
 266                        item.TrySetProviderId(MetadataProvider.Imdb, imdbId);
 67
 268                        break;
 69                    }
 70
 71                case "set":
 72                    {
 273                        var movie = item as Movie;
 74
 275                        var tmdbcolid = reader.GetAttribute("tmdbcolid");
 276                        movie?.TrySetProviderId(MetadataProvider.TmdbCollection, tmdbcolid);
 77
 278                        var val = reader.ReadInnerXml();
 79
 280                        if (!string.IsNullOrWhiteSpace(val) && movie is not null)
 81                        {
 82                            try
 83                            {
 284                                ParseSetXml(val, movie);
 285                            }
 086                            catch (Exception ex)
 87                            {
 088                                Logger.LogError(ex, "Error parsing set node");
 089                            }
 90                        }
 91
 92                        break;
 93                    }
 94
 95                case "artist":
 196                    var artist = reader.ReadNormalizedString();
 197                    if (!string.IsNullOrEmpty(artist) && item is MusicVideo artistVideo)
 98                    {
 199                        artistVideo.Artists = [..artistVideo.Artists, artist];
 100                    }
 101
 1102                    break;
 103                case "album":
 1104                    var album = reader.ReadNormalizedString();
 1105                    if (!string.IsNullOrEmpty(album) && item is MusicVideo albumVideo)
 106                    {
 1107                        albumVideo.Album = album;
 108                    }
 109
 1110                    break;
 111                default:
 156112                    base.FetchDataFromXmlNode(reader, itemResult);
 113                    break;
 114            }
 158115        }
 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            {
 2122                using (var stringReader = new StringReader("<set>" + xml + "</set>"))
 2123                using (var reader = XmlReader.Create(stringReader, GetXmlReaderSettings()))
 124                {
 2125                    reader.MoveToContent();
 2126                    reader.Read();
 127
 128                    // Loop through each element
 8129                    while (!reader.EOF && reader.ReadState == ReadState.Interactive)
 130                    {
 7131                        if (reader.NodeType == XmlNodeType.Text && reader.Depth == 1)
 132                        {
 1133                            movie.CollectionName = reader.Value;
 1134                            break;
 135                        }
 6136                        else if (reader.NodeType == XmlNodeType.Element)
 137                        {
 2138                            switch (reader.Name)
 139                            {
 140                                case "name":
 1141                                    movie.CollectionName = reader.ReadElementContentAsString();
 1142                                    break;
 143                                default:
 1144                                    reader.Skip();
 1145                                    break;
 146                            }
 147                        }
 148                        else
 149                        {
 4150                            reader.Read();
 151                        }
 152                    }
 2153                }
 2154            }
 0155            catch (XmlException)
 156            {
 0157            }
 2158        }
 159    }
 160}