| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.Text; |
| | 4 | | using System.Threading; |
| | 5 | | using System.Xml; |
| | 6 | | using MediaBrowser.Common.Configuration; |
| | 7 | | using MediaBrowser.Controller.Entities.TV; |
| | 8 | | using MediaBrowser.Controller.Extensions; |
| | 9 | | using MediaBrowser.Controller.Library; |
| | 10 | | using MediaBrowser.Controller.Providers; |
| | 11 | | using Microsoft.Extensions.Logging; |
| | 12 | |
|
| | 13 | | namespace MediaBrowser.XbmcMetadata.Parsers |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Nfo parser for episodes. |
| | 17 | | /// </summary> |
| | 18 | | public class EpisodeNfoParser : BaseNfoParser<Episode> |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// Initializes a new instance of the <see cref="EpisodeNfoParser"/> class. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="logger">Instance of the <see cref="ILogger{BaseNfoParser}"/> 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="IDirectoryService"/> interface.</param> |
| | 29 | | public EpisodeNfoParser( |
| | 30 | | ILogger logger, |
| | 31 | | IConfigurationManager config, |
| | 32 | | IProviderManager providerManager, |
| | 33 | | IUserManager userManager, |
| | 34 | | IUserDataManager userDataManager, |
| | 35 | | IDirectoryService directoryService) |
| 6 | 36 | | : base(logger, config, providerManager, userManager, userDataManager, directoryService) |
| | 37 | | { |
| 6 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <inheritdoc /> |
| | 41 | | protected override void Fetch(MetadataResult<Episode> item, string metadataFile, XmlReaderSettings settings, Can |
| | 42 | | { |
| 4 | 43 | | item.ResetPeople(); |
| | 44 | |
|
| 4 | 45 | | var xmlFile = File.ReadAllText(metadataFile); |
| | 46 | |
|
| 4 | 47 | | var srch = "</episodedetails>"; |
| 4 | 48 | | var index = xmlFile.IndexOf(srch, StringComparison.OrdinalIgnoreCase); |
| | 49 | |
|
| 4 | 50 | | var xml = xmlFile; |
| | 51 | |
|
| 4 | 52 | | if (index != -1) |
| | 53 | | { |
| 4 | 54 | | xml = xmlFile.Substring(0, index + srch.Length); |
| 4 | 55 | | xmlFile = xmlFile.Substring(index + srch.Length); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | // These are not going to be valid xml so no sense in causing the provider to fail and spamming the log with |
| | 59 | | try |
| | 60 | | { |
| | 61 | | // Extract episode details from the first episodedetails block |
| 4 | 62 | | ReadEpisodeDetailsFromXml(item, xml, settings, cancellationToken); |
| | 63 | |
|
| | 64 | | // Extract the last episode number from nfo |
| | 65 | | // Retrieves all additional episodedetails blocks from the rest of the nfo and concatenates the name, or |
| | 66 | | // This is needed because XBMC metadata uses multiple episodedetails blocks instead of episodenumberend |
| 4 | 67 | | var name = new StringBuilder(item.Item.Name); |
| 4 | 68 | | var originalTitle = new StringBuilder(item.Item.OriginalTitle); |
| 4 | 69 | | var overview = new StringBuilder(item.Item.Overview); |
| 8 | 70 | | while ((index = xmlFile.IndexOf(srch, StringComparison.OrdinalIgnoreCase)) != -1) |
| | 71 | | { |
| 4 | 72 | | xml = xmlFile.Substring(0, index + srch.Length); |
| 4 | 73 | | xmlFile = xmlFile.Substring(index + srch.Length); |
| | 74 | |
|
| 4 | 75 | | var additionalEpisode = new MetadataResult<Episode>() |
| 4 | 76 | | { |
| 4 | 77 | | Item = new Episode() |
| 4 | 78 | | }; |
| | 79 | |
|
| | 80 | | // Extract episode details from additional episodedetails block |
| 4 | 81 | | ReadEpisodeDetailsFromXml(additionalEpisode, xml, settings, cancellationToken); |
| | 82 | |
|
| 4 | 83 | | if (!string.IsNullOrEmpty(additionalEpisode.Item.Name)) |
| | 84 | | { |
| 3 | 85 | | name.Append(" / ").Append(additionalEpisode.Item.Name); |
| | 86 | | } |
| | 87 | |
|
| 4 | 88 | | if (!string.IsNullOrEmpty(additionalEpisode.Item.Overview)) |
| | 89 | | { |
| 1 | 90 | | overview.Append(" / ").Append(additionalEpisode.Item.Overview); |
| | 91 | | } |
| | 92 | |
|
| 4 | 93 | | if (!string.IsNullOrEmpty(additionalEpisode.Item.OriginalTitle)) |
| | 94 | | { |
| 3 | 95 | | originalTitle.Append(" / ").Append(additionalEpisode.Item.OriginalTitle); |
| | 96 | | } |
| | 97 | |
|
| 4 | 98 | | if (additionalEpisode.Item.IndexNumber is not null) |
| | 99 | | { |
| 4 | 100 | | item.Item.IndexNumberEnd = Math.Max((int)additionalEpisode.Item.IndexNumber, item.Item.IndexNumb |
| | 101 | | } |
| | 102 | | } |
| | 103 | |
|
| 4 | 104 | | item.Item.Name = name.ToString(); |
| 4 | 105 | | item.Item.OriginalTitle = originalTitle.ToString(); |
| 4 | 106 | | item.Item.Overview = overview.ToString(); |
| 4 | 107 | | } |
| 0 | 108 | | catch (XmlException) |
| | 109 | | { |
| 0 | 110 | | } |
| 4 | 111 | | } |
| | 112 | |
|
| | 113 | | /// <inheritdoc /> |
| | 114 | | protected override void FetchDataFromXmlNode(XmlReader reader, MetadataResult<Episode> itemResult) |
| | 115 | | { |
| 119 | 116 | | var item = itemResult.Item; |
| | 117 | |
|
| 119 | 118 | | switch (reader.Name) |
| | 119 | | { |
| | 120 | | case "season": |
| 8 | 121 | | if (reader.TryReadInt(out var seasonNumber)) |
| | 122 | | { |
| 8 | 123 | | item.ParentIndexNumber = seasonNumber; |
| | 124 | | } |
| | 125 | |
|
| 8 | 126 | | break; |
| | 127 | | case "episode": |
| 8 | 128 | | if (reader.TryReadInt(out var episodeNumber)) |
| | 129 | | { |
| 8 | 130 | | item.IndexNumber = episodeNumber; |
| | 131 | | } |
| | 132 | |
|
| 8 | 133 | | break; |
| | 134 | | case "episodenumberend": |
| 1 | 135 | | if (reader.TryReadInt(out var episodeNumberEnd)) |
| | 136 | | { |
| 1 | 137 | | item.IndexNumberEnd = episodeNumberEnd; |
| | 138 | | } |
| | 139 | |
|
| 1 | 140 | | break; |
| | 141 | | case "airsbefore_episode": |
| | 142 | | case "displayepisode": |
| 2 | 143 | | if (reader.TryReadInt(out var airsBeforeEpisode)) |
| | 144 | | { |
| 2 | 145 | | item.AirsBeforeEpisodeNumber = airsBeforeEpisode; |
| | 146 | | } |
| | 147 | |
|
| 2 | 148 | | break; |
| | 149 | | case "airsafter_season": |
| | 150 | | case "displayafterseason": |
| 1 | 151 | | if (reader.TryReadInt(out var airsAfterSeason)) |
| | 152 | | { |
| 1 | 153 | | item.AirsAfterSeasonNumber = airsAfterSeason; |
| | 154 | | } |
| | 155 | |
|
| 1 | 156 | | break; |
| | 157 | | case "airsbefore_season": |
| | 158 | | case "displayseason": |
| 2 | 159 | | if (reader.TryReadInt(out var airsBeforeSeason)) |
| | 160 | | { |
| 2 | 161 | | item.AirsBeforeSeasonNumber = airsBeforeSeason; |
| | 162 | | } |
| | 163 | |
|
| 2 | 164 | | break; |
| | 165 | | case "showtitle": |
| 1 | 166 | | item.SeriesName = reader.ReadNormalizedString(); |
| 1 | 167 | | break; |
| | 168 | | default: |
| 96 | 169 | | base.FetchDataFromXmlNode(reader, itemResult); |
| | 170 | | break; |
| | 171 | | } |
| 96 | 172 | | } |
| | 173 | |
|
| | 174 | | /// <summary> |
| | 175 | | /// Reads the episode details from the given xml and saves the result in the provided result item. |
| | 176 | | /// </summary> |
| | 177 | | private void ReadEpisodeDetailsFromXml(MetadataResult<Episode> item, string xml, XmlReaderSettings settings, Can |
| | 178 | | { |
| 8 | 179 | | using (var stringReader = new StringReader(xml)) |
| 8 | 180 | | using (var reader = XmlReader.Create(stringReader, settings)) |
| | 181 | | { |
| 8 | 182 | | reader.MoveToContent(); |
| 8 | 183 | | reader.Read(); |
| | 184 | |
|
| | 185 | | // Loop through each element |
| 287 | 186 | | while (!reader.EOF && reader.ReadState == ReadState.Interactive) |
| | 187 | | { |
| 279 | 188 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 189 | |
|
| 279 | 190 | | if (reader.NodeType == XmlNodeType.Element) |
| | 191 | | { |
| 119 | 192 | | FetchDataFromXmlNode(reader, item); |
| | 193 | | } |
| | 194 | | else |
| | 195 | | { |
| 160 | 196 | | reader.Read(); |
| | 197 | | } |
| | 198 | | } |
| 8 | 199 | | } |
| 8 | 200 | | } |
| | 201 | | } |
| | 202 | | } |