using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Xml;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.IO;
using MediaBrowser.XbmcMetadata.Configuration;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.XbmcMetadata.Savers
{
///
/// Nfo saver for episodes.
///
public class EpisodeNfoSaver : BaseNfoSaver
{
///
/// Initializes a new instance of the class.
///
/// The file system.
/// the server configuration manager.
/// The library manager.
/// The user manager.
/// The user data manager.
/// The logger.
public EpisodeNfoSaver(
IFileSystem fileSystem,
IServerConfigurationManager configurationManager,
ILibraryManager libraryManager,
IUserManager userManager,
IUserDataManager userDataManager,
ILogger logger)
: base(fileSystem, configurationManager, libraryManager, userManager, userDataManager, logger)
{
}
///
protected override string GetLocalSavePath(BaseItem item)
=> Path.ChangeExtension(item.Path, ".nfo");
///
protected override string GetRootElementName(BaseItem item)
=> "episodedetails";
///
public override bool IsEnabledFor(BaseItem item, ItemUpdateType updateType)
=> item.SupportsLocalMetadata && item is Episode && updateType >= MinimumUpdateType;
///
protected override void WriteCustomElements(BaseItem item, XmlWriter writer)
{
var episode = (Episode)item;
writer.WriteElementString("showtitle", episode.SeriesName);
if (episode.IndexNumber.HasValue)
{
writer.WriteElementString("episode", episode.IndexNumber.Value.ToString(CultureInfo.InvariantCulture));
}
if (episode.IndexNumberEnd.HasValue)
{
writer.WriteElementString("episodenumberend", episode.IndexNumberEnd.Value.ToString(CultureInfo.InvariantCulture));
}
if (episode.ParentIndexNumber.HasValue)
{
writer.WriteElementString("season", episode.ParentIndexNumber.Value.ToString(CultureInfo.InvariantCulture));
}
if (episode.PremiereDate.HasValue)
{
var formatString = ConfigurationManager.GetNfoConfiguration().ReleaseDateFormat;
writer.WriteElementString("aired", episode.PremiereDate.Value.ToLocalTime().ToString(formatString, CultureInfo.InvariantCulture));
}
if (!episode.ParentIndexNumber.HasValue || episode.ParentIndexNumber.Value == 0)
{
if (episode.AirsAfterSeasonNumber.HasValue && episode.AirsAfterSeasonNumber.Value != -1)
{
writer.WriteElementString("airsafter_season", episode.AirsAfterSeasonNumber.Value.ToString(CultureInfo.InvariantCulture));
}
if (episode.AirsBeforeEpisodeNumber.HasValue && episode.AirsBeforeEpisodeNumber.Value != -1)
{
writer.WriteElementString("airsbefore_episode", episode.AirsBeforeEpisodeNumber.Value.ToString(CultureInfo.InvariantCulture));
}
if (episode.AirsBeforeSeasonNumber.HasValue && episode.AirsBeforeSeasonNumber.Value != -1)
{
writer.WriteElementString("airsbefore_season", episode.AirsBeforeSeasonNumber.Value.ToString(CultureInfo.InvariantCulture));
}
if (episode.AirsBeforeEpisodeNumber.HasValue && episode.AirsBeforeEpisodeNumber.Value != -1)
{
writer.WriteElementString("displayepisode", episode.AirsBeforeEpisodeNumber.Value.ToString(CultureInfo.InvariantCulture));
}
var specialSeason = episode.AiredSeasonNumber;
if (specialSeason.HasValue && specialSeason.Value != -1)
{
writer.WriteElementString("displayseason", specialSeason.Value.ToString(CultureInfo.InvariantCulture));
}
}
}
///
protected override IEnumerable GetTagsUsed(BaseItem item)
{
foreach (var tag in base.GetTagsUsed(item))
{
yield return tag;
}
yield return "aired";
yield return "season";
yield return "episode";
yield return "episodenumberend";
yield return "airsafter_season";
yield return "airsbefore_episode";
yield return "airsbefore_season";
yield return "displayseason";
yield return "displayepisode";
yield return "showtitle";
}
}
}