You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs

1095 lines
38 KiB

using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.XbmcMetadata.Configuration;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
10 years ago
using System.Threading;
using System.Xml;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Xml;
namespace MediaBrowser.XbmcMetadata.Savers
{
10 years ago
public abstract class BaseNfoSaver : IMetadataFileSaver
{
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
private static readonly Dictionary<string, string> CommonTags = new[] {
"plot",
"customrating",
"lockdata",
"type",
"dateadded",
"title",
"rating",
"year",
"sorttitle",
"mpaa",
"mpaadescription",
"aspectratio",
"website",
"collectionnumber",
"tmdbid",
"rottentomatoesid",
"language",
"tvcomid",
"budget",
"revenue",
"tagline",
"studio",
"genre",
"tag",
"runtime",
"actor",
"criticratingsummary",
"criticrating",
"fileinfo",
"director",
"writer",
"trailer",
"premiered",
"releasedate",
"outline",
"id",
"votes",
"credits",
"originaltitle",
"watched",
"playcount",
"lastplayed",
"art",
"resume",
"biography",
"formed",
"review",
"style",
"imdbid",
"imdb_id",
"plotkeyword",
"country",
"audiodbalbumid",
"audiodbartistid",
"awardsummary",
"enddate",
"lockedfields",
"metascore",
"zap2itid",
"tvrageid",
"gamesdbid",
"musicbrainzartistid",
"musicbrainzalbumartistid",
"musicbrainzalbumid",
"musicbrainzreleasegroupid",
"tvdbid",
"collectionitem",
"isuserfavorite",
"userrating",
"countrycode"
}.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
protected BaseNfoSaver(IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, IUserManager userManager, IUserDataManager userDataManager, ILogger logger, IXmlReaderSettingsFactory xmlReaderSettingsFactory)
10 years ago
{
Logger = logger;
XmlReaderSettingsFactory = xmlReaderSettingsFactory;
10 years ago
UserDataManager = userDataManager;
UserManager = userManager;
LibraryManager = libraryManager;
ConfigurationManager = configurationManager;
FileSystem = fileSystem;
}
protected IFileSystem FileSystem { get; private set; }
protected IServerConfigurationManager ConfigurationManager { get; private set; }
protected ILibraryManager LibraryManager { get; private set; }
protected IUserManager UserManager { get; private set; }
protected IUserDataManager UserDataManager { get; private set; }
protected ILogger Logger { get; private set; }
protected IXmlReaderSettingsFactory XmlReaderSettingsFactory { get; private set; }
10 years ago
protected ItemUpdateType MinimumUpdateType
{
get
{
if (ConfigurationManager.GetNfoConfiguration().SaveImagePathsInNfo)
{
return ItemUpdateType.ImageUpdate;
}
return ItemUpdateType.MetadataDownload;
}
}
10 years ago
public string Name
{
get
{
return SaverName;
}
}
public static string SaverName
10 years ago
{
get
{
return "Nfo";
10 years ago
}
}
public string GetSavePath(IHasMetadata item)
{
return GetLocalSavePath(item);
}
10 years ago
/// <summary>
/// Gets the save path.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>System.String.</returns>
protected abstract string GetLocalSavePath(IHasMetadata item);
10 years ago
/// <summary>
/// Gets the name of the root element.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>System.String.</returns>
protected abstract string GetRootElementName(IHasMetadata item);
/// <summary>
10 years ago
/// Determines whether [is enabled for] [the specified item].
/// </summary>
10 years ago
/// <param name="item">The item.</param>
/// <param name="updateType">Type of the update.</param>
/// <returns><c>true</c> if [is enabled for] [the specified item]; otherwise, <c>false</c>.</returns>
public abstract bool IsEnabledFor(IHasMetadata item, ItemUpdateType updateType);
protected virtual List<string> GetTagsUsed()
{
return new List<string>();
}
public void Save(IHasMetadata item, CancellationToken cancellationToken)
{
10 years ago
var path = GetSavePath(item);
10 years ago
using (var memoryStream = new MemoryStream())
{
10 years ago
Save(item, memoryStream, path);
10 years ago
memoryStream.Position = 0;
10 years ago
cancellationToken.ThrowIfCancellationRequested();
10 years ago
SaveToFile(memoryStream, path);
}
}
10 years ago
private void SaveToFile(Stream stream, string path)
{
FileSystem.CreateDirectory(Path.GetDirectoryName(path));
var file = FileSystem.GetFileInfo(path);
var wasHidden = false;
// This will fail if the file is hidden
if (file.Exists)
{
if (file.IsHidden)
{
FileSystem.SetHidden(path, false);
wasHidden = true;
}
if (file.IsReadOnly)
{
FileSystem.SetReadOnly(path, false);
}
}
using (var filestream = FileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
{
10 years ago
stream.CopyTo(filestream);
}
if (wasHidden || ConfigurationManager.Configuration.SaveMetadataHidden)
{
FileSystem.SetHidden(path, true);
}
}
10 years ago
private void Save(IHasMetadata item, Stream stream, string xmlPath)
{
10 years ago
var settings = new XmlWriterSettings
{
10 years ago
Indent = true,
Encoding = Encoding.UTF8,
CloseOutput = false
};
10 years ago
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
10 years ago
var root = GetRootElementName(item);
writer.WriteStartDocument(true);
writer.WriteStartElement(root);
var baseItem = item as BaseItem;
if (baseItem != null)
{
10 years ago
AddCommonNodes(baseItem, writer, LibraryManager, UserManager, UserDataManager, FileSystem, ConfigurationManager);
}
10 years ago
WriteCustomElements(item, writer);
10 years ago
var hasMediaSources = baseItem as IHasMediaSources;
if (hasMediaSources != null)
{
AddMediaInfo(hasMediaSources, writer);
}
10 years ago
var tagsUsed = GetTagsUsed();
try
{
AddCustomTags(xmlPath, tagsUsed, writer, Logger, FileSystem);
10 years ago
}
catch (FileNotFoundException)
{
10 years ago
}
catch (IOException)
{
}
catch (XmlException ex)
{
Logger.ErrorException("Error reading existng nfo", ex);
}
10 years ago
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
10 years ago
protected abstract void WriteCustomElements(IHasMetadata item, XmlWriter writer);
public static void AddMediaInfo<T>(T item, XmlWriter writer)
where T : IHasMediaSources
{
10 years ago
writer.WriteStartElement("fileinfo");
writer.WriteStartElement("streamdetails");
10 years ago
var mediaSource = item.GetMediaSources(false).First();
foreach (var stream in mediaSource.MediaStreams)
{
10 years ago
writer.WriteStartElement(stream.Type.ToString().ToLower());
if (!string.IsNullOrEmpty(stream.Codec))
{
var codec = stream.Codec;
if ((stream.CodecTag ?? string.Empty).IndexOf("xvid", StringComparison.OrdinalIgnoreCase) != -1)
{
codec = "xvid;";
}
else if ((stream.CodecTag ?? string.Empty).IndexOf("divx", StringComparison.OrdinalIgnoreCase) != -1)
{
codec = "divx;";
}
writer.WriteElementString("codec", codec);
writer.WriteElementString("micodec", codec);
}
if (stream.BitRate.HasValue)
{
10 years ago
writer.WriteElementString("bitrate", stream.BitRate.Value.ToString(UsCulture));
}
if (stream.Width.HasValue)
{
10 years ago
writer.WriteElementString("width", stream.Width.Value.ToString(UsCulture));
}
if (stream.Height.HasValue)
{
10 years ago
writer.WriteElementString("height", stream.Height.Value.ToString(UsCulture));
}
if (!string.IsNullOrEmpty(stream.AspectRatio))
{
10 years ago
writer.WriteElementString("aspect", stream.AspectRatio);
writer.WriteElementString("aspectratio", stream.AspectRatio);
}
var framerate = stream.AverageFrameRate ?? stream.RealFrameRate;
if (framerate.HasValue)
{
10 years ago
writer.WriteElementString("framerate", framerate.Value.ToString(UsCulture));
}
if (!string.IsNullOrEmpty(stream.Language))
{
10 years ago
writer.WriteElementString("language", stream.Language);
}
var scanType = stream.IsInterlaced ? "interlaced" : "progressive";
if (!string.IsNullOrEmpty(scanType))
{
10 years ago
writer.WriteElementString("scantype", scanType);
}
if (stream.Channels.HasValue)
{
10 years ago
writer.WriteElementString("channels", stream.Channels.Value.ToString(UsCulture));
}
if (stream.SampleRate.HasValue)
{
10 years ago
writer.WriteElementString("samplingrate", stream.SampleRate.Value.ToString(UsCulture));
}
10 years ago
writer.WriteElementString("default", stream.IsDefault.ToString());
writer.WriteElementString("forced", stream.IsForced.ToString());
if (stream.Type == MediaStreamType.Video)
{
10 years ago
if (mediaSource.RunTimeTicks.HasValue)
{
10 years ago
var timespan = TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value);
10 years ago
writer.WriteElementString("duration", Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture));
writer.WriteElementString("durationinseconds", Convert.ToInt32(timespan.TotalSeconds).ToString(UsCulture));
}
var video = item as Video;
if (video != null)
{
//AddChapters(video, builder, itemRepository);
if (video.Video3DFormat.HasValue)
{
switch (video.Video3DFormat.Value)
{
case Video3DFormat.FullSideBySide:
10 years ago
writer.WriteElementString("format3d", "FSBS");
break;
case Video3DFormat.FullTopAndBottom:
10 years ago
writer.WriteElementString("format3d", "FTAB");
break;
case Video3DFormat.HalfSideBySide:
10 years ago
writer.WriteElementString("format3d", "HSBS");
break;
case Video3DFormat.HalfTopAndBottom:
10 years ago
writer.WriteElementString("format3d", "HTAB");
break;
case Video3DFormat.MVC:
writer.WriteElementString("format3d", "MVC");
break;
}
}
}
}
10 years ago
writer.WriteEndElement();
}
10 years ago
writer.WriteEndElement();
writer.WriteEndElement();
}
public const string DateAddedFormat = "yyyy-MM-dd HH:mm:ss";
/// <summary>
/// Adds the common nodes.
/// </summary>
/// <returns>Task.</returns>
10 years ago
public static void AddCommonNodes(BaseItem item, XmlWriter writer, ILibraryManager libraryManager, IUserManager userManager, IUserDataManager userDataRepo, IFileSystem fileSystem, IServerConfigurationManager config)
{
var writtenProviderIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var overview = (item.Overview ?? string.Empty)
.StripHtml()
.Replace("&quot;", "'");
var options = config.GetNfoConfiguration();
if (item is MusicArtist)
{
10 years ago
writer.WriteElementString("biography", overview);
}
else if (item is MusicAlbum)
{
10 years ago
writer.WriteElementString("review", overview);
}
else
{
10 years ago
writer.WriteElementString("plot", overview);
}
if (item is Video)
{
var outline = (item.ShortOverview ?? string.Empty)
.StripHtml()
.Replace("&quot;", "'");
10 years ago
writer.WriteElementString("outline", outline);
}
else
{
10 years ago
writer.WriteElementString("outline", overview);
}
if (!string.IsNullOrWhiteSpace(item.CustomRating))
{
writer.WriteElementString("customrating", item.CustomRating);
}
10 years ago
writer.WriteElementString("lockdata", item.IsLocked.ToString().ToLower());
if (item.LockedFields.Count > 0)
{
10 years ago
writer.WriteElementString("lockedfields", string.Join("|", item.LockedFields.Select(i => i.ToString()).ToArray()));
}
10 years ago
if (!string.IsNullOrEmpty(item.DisplayMediaType))
{
10 years ago
writer.WriteElementString("type", item.DisplayMediaType);
}
writer.WriteElementString("dateadded", item.DateCreated.ToLocalTime().ToString(DateAddedFormat));
10 years ago
writer.WriteElementString("title", item.Name ?? string.Empty);
if (!string.IsNullOrWhiteSpace(item.OriginalTitle))
{
writer.WriteElementString("originaltitle", item.OriginalTitle);
}
var people = libraryManager.GetPeople(item);
var directors = people
.Where(i => IsPersonType(i, PersonType.Director))
.Select(i => i.Name)
.ToList();
foreach (var person in directors)
{
10 years ago
writer.WriteElementString("director", person);
}
var writers = people
.Where(i => IsPersonType(i, PersonType.Writer))
.Select(i => i.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
foreach (var person in writers)
{
10 years ago
writer.WriteElementString("writer", person);
}
foreach (var person in writers)
{
10 years ago
writer.WriteElementString("credits", person);
}
var hasTrailer = item as IHasTrailers;
if (hasTrailer != null)
{
foreach (var trailer in hasTrailer.RemoteTrailers)
{
10 years ago
writer.WriteElementString("trailer", GetOutputTrailerUrl(trailer.Url));
}
}
if (item.CommunityRating.HasValue)
{
10 years ago
writer.WriteElementString("rating", item.CommunityRating.Value.ToString(UsCulture));
}
if (item.ProductionYear.HasValue)
{
10 years ago
writer.WriteElementString("year", item.ProductionYear.Value.ToString(UsCulture));
}
if (!string.IsNullOrEmpty(item.ForcedSortName))
{
10 years ago
writer.WriteElementString("sorttitle", item.ForcedSortName);
}
if (!string.IsNullOrEmpty(item.OfficialRating))
{
10 years ago
writer.WriteElementString("mpaa", item.OfficialRating);
}
if (!string.IsNullOrEmpty(item.OfficialRatingDescription))
{
10 years ago
writer.WriteElementString("mpaadescription", item.OfficialRatingDescription);
}
var hasAspectRatio = item as IHasAspectRatio;
if (hasAspectRatio != null)
{
if (!string.IsNullOrEmpty(hasAspectRatio.AspectRatio))
{
10 years ago
writer.WriteElementString("aspectratio", hasAspectRatio.AspectRatio);
}
}
if (!string.IsNullOrEmpty(item.HomePageUrl))
{
10 years ago
writer.WriteElementString("website", item.HomePageUrl);
}
var rt = item.GetProviderId(MetadataProviders.RottenTomatoes);
if (!string.IsNullOrEmpty(rt))
{
10 years ago
writer.WriteElementString("rottentomatoesid", rt);
writtenProviderIds.Add(MetadataProviders.RottenTomatoes.ToString());
}
var tmdbCollection = item.GetProviderId(MetadataProviders.TmdbCollection);
if (!string.IsNullOrEmpty(tmdbCollection))
{
10 years ago
writer.WriteElementString("collectionnumber", tmdbCollection);
writtenProviderIds.Add(MetadataProviders.TmdbCollection.ToString());
}
var imdb = item.GetProviderId(MetadataProviders.Imdb);
if (!string.IsNullOrEmpty(imdb))
{
if (item is Series)
{
10 years ago
writer.WriteElementString("imdb_id", imdb);
}
else
{
10 years ago
writer.WriteElementString("imdbid", imdb);
}
writtenProviderIds.Add(MetadataProviders.Imdb.ToString());
}
// Series xml saver already saves this
if (!(item is Series))
{
var tvdb = item.GetProviderId(MetadataProviders.Tvdb);
if (!string.IsNullOrEmpty(tvdb))
{
10 years ago
writer.WriteElementString("tvdbid", tvdb);
writtenProviderIds.Add(MetadataProviders.Tvdb.ToString());
}
}
var tmdb = item.GetProviderId(MetadataProviders.Tmdb);
if (!string.IsNullOrEmpty(tmdb))
{
10 years ago
writer.WriteElementString("tmdbid", tmdb);
writtenProviderIds.Add(MetadataProviders.Tmdb.ToString());
}
var tvcom = item.GetProviderId(MetadataProviders.Tvcom);
if (!string.IsNullOrEmpty(tvcom))
{
10 years ago
writer.WriteElementString("tvcomid", tvcom);
writtenProviderIds.Add(MetadataProviders.Tvcom.ToString());
}
if (!string.IsNullOrEmpty(item.PreferredMetadataLanguage))
{
writer.WriteElementString("language", item.PreferredMetadataLanguage);
}
if (!string.IsNullOrEmpty(item.PreferredMetadataCountryCode))
{
writer.WriteElementString("countrycode", item.PreferredMetadataCountryCode);
}
if (item.PremiereDate.HasValue && !(item is Episode))
{
var formatString = options.ReleaseDateFormat;
if (item is MusicArtist)
{
writer.WriteElementString("formed", item.PremiereDate.Value.ToLocalTime().ToString(formatString));
}
else
{
writer.WriteElementString("premiered", item.PremiereDate.Value.ToLocalTime().ToString(formatString));
writer.WriteElementString("releasedate", item.PremiereDate.Value.ToLocalTime().ToString(formatString));
}
}
if (item.EndDate.HasValue)
{
if (!(item is Episode))
{
var formatString = options.ReleaseDateFormat;
writer.WriteElementString("enddate", item.EndDate.Value.ToLocalTime().ToString(formatString));
}
}
if (item.CriticRating.HasValue)
{
writer.WriteElementString("criticrating", item.CriticRating.Value.ToString(UsCulture));
}
if (!string.IsNullOrEmpty(item.CriticRatingSummary))
{
writer.WriteElementString("criticratingsummary", item.CriticRatingSummary);
}
var hasDisplayOrder = item as IHasDisplayOrder;
if (hasDisplayOrder != null)
{
if (!string.IsNullOrEmpty(hasDisplayOrder.DisplayOrder))
{
10 years ago
writer.WriteElementString("displayorder", hasDisplayOrder.DisplayOrder);
}
}
if (item.VoteCount.HasValue)
{
10 years ago
writer.WriteElementString("votes", item.VoteCount.Value.ToString(UsCulture));
}
var hasBudget = item as IHasBudget;
if (hasBudget != null)
{
if (hasBudget.Budget.HasValue)
{
10 years ago
writer.WriteElementString("budget", hasBudget.Budget.Value.ToString(UsCulture));
}
if (hasBudget.Revenue.HasValue)
{
10 years ago
writer.WriteElementString("revenue", hasBudget.Revenue.Value.ToString(UsCulture));
}
}
var hasMetascore = item as IHasMetascore;
if (hasMetascore != null && hasMetascore.Metascore.HasValue)
{
10 years ago
writer.WriteElementString("metascore", hasMetascore.Metascore.Value.ToString(UsCulture));
}
// Use original runtime here, actual file runtime later in MediaInfo
var runTimeTicks = item.RunTimeTicks;
if (runTimeTicks.HasValue)
{
var timespan = TimeSpan.FromTicks(runTimeTicks.Value);
10 years ago
writer.WriteElementString("runtime", Convert.ToInt32(timespan.TotalMinutes).ToString(UsCulture));
}
if (!string.IsNullOrWhiteSpace(item.Tagline))
{
writer.WriteElementString("tagline", item.Tagline);
}
foreach (var country in item.ProductionLocations)
{
writer.WriteElementString("country", country);
}
foreach (var genre in item.Genres)
{
10 years ago
writer.WriteElementString("genre", genre);
}
foreach (var studio in item.Studios)
{
10 years ago
writer.WriteElementString("studio", studio);
}
foreach (var tag in item.Tags)
{
if (item is MusicAlbum || item is MusicArtist)
{
writer.WriteElementString("style", tag);
}
else
{
writer.WriteElementString("tag", tag);
}
}
foreach (var tag in item.Keywords)
{
writer.WriteElementString("plotkeyword", tag);
}
var hasAwards = item as IHasAwards;
if (hasAwards != null && !string.IsNullOrEmpty(hasAwards.AwardSummary))
{
10 years ago
writer.WriteElementString("awardsummary", hasAwards.AwardSummary);
}
var externalId = item.GetProviderId(MetadataProviders.AudioDbArtist);
if (!string.IsNullOrEmpty(externalId))
{
10 years ago
writer.WriteElementString("audiodbartistid", externalId);
writtenProviderIds.Add(MetadataProviders.AudioDbArtist.ToString());
}
externalId = item.GetProviderId(MetadataProviders.AudioDbAlbum);
if (!string.IsNullOrEmpty(externalId))
{
10 years ago
writer.WriteElementString("audiodbalbumid", externalId);
writtenProviderIds.Add(MetadataProviders.AudioDbAlbum.ToString());
}
externalId = item.GetProviderId(MetadataProviders.Zap2It);
if (!string.IsNullOrEmpty(externalId))
{
10 years ago
writer.WriteElementString("zap2itid", externalId);
writtenProviderIds.Add(MetadataProviders.Zap2It.ToString());
}
externalId = item.GetProviderId(MetadataProviders.MusicBrainzAlbum);
if (!string.IsNullOrEmpty(externalId))
{
10 years ago
writer.WriteElementString("musicbrainzalbumid", externalId);
writtenProviderIds.Add(MetadataProviders.MusicBrainzAlbum.ToString());
}
externalId = item.GetProviderId(MetadataProviders.MusicBrainzAlbumArtist);
if (!string.IsNullOrEmpty(externalId))
{
10 years ago
writer.WriteElementString("musicbrainzalbumartistid", externalId);
writtenProviderIds.Add(MetadataProviders.MusicBrainzAlbumArtist.ToString());
}
externalId = item.GetProviderId(MetadataProviders.MusicBrainzArtist);
if (!string.IsNullOrEmpty(externalId))
{
10 years ago
writer.WriteElementString("musicbrainzartistid", externalId);
writtenProviderIds.Add(MetadataProviders.MusicBrainzArtist.ToString());
}
externalId = item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
if (!string.IsNullOrEmpty(externalId))
{
10 years ago
writer.WriteElementString("musicbrainzreleasegroupid", externalId);
writtenProviderIds.Add(MetadataProviders.MusicBrainzReleaseGroup.ToString());
}
externalId = item.GetProviderId(MetadataProviders.Gamesdb);
if (!string.IsNullOrEmpty(externalId))
{
10 years ago
writer.WriteElementString("gamesdbid", externalId);
writtenProviderIds.Add(MetadataProviders.Gamesdb.ToString());
}
externalId = item.GetProviderId(MetadataProviders.TvRage);
if (!string.IsNullOrEmpty(externalId))
{
10 years ago
writer.WriteElementString("tvrageid", externalId);
writtenProviderIds.Add(MetadataProviders.TvRage.ToString());
}
if (item.ProviderIds != null)
{
foreach (var providerKey in item.ProviderIds.Keys)
{
var providerId = item.ProviderIds[providerKey];
if (!string.IsNullOrEmpty(providerId) && !writtenProviderIds.Contains(providerKey))
{
writer.WriteElementString(providerKey.ToLower() + "id", providerId);
}
}
}
if (options.SaveImagePathsInNfo)
{
AddImages(item, writer, libraryManager, config);
}
10 years ago
AddUserData(item, writer, userManager, userDataRepo, options);
AddActors(people, writer, libraryManager, fileSystem, config);
var folder = item as BoxSet;
if (folder != null)
{
10 years ago
AddCollectionItems(folder, writer);
}
}
10 years ago
public static void AddChapters(Video item, XmlWriter writer, IItemRepository repository)
{
var chapters = repository.GetChapters(item.Id);
foreach (var chapter in chapters)
{
10 years ago
writer.WriteStartElement("chapter");
writer.WriteElementString("name", chapter.Name);
var time = TimeSpan.FromTicks(chapter.StartPositionTicks);
var ms = Convert.ToInt64(time.TotalMilliseconds);
10 years ago
writer.WriteElementString("startpositionms", ms.ToString(UsCulture));
writer.WriteEndElement();
}
}
10 years ago
private static void AddCollectionItems(Folder item, XmlWriter writer)
{
var items = item.LinkedChildren
.Where(i => i.Type == LinkedChildType.Manual)
.ToList();
foreach (var link in items)
{
10 years ago
writer.WriteStartElement("collectionitem");
if (!string.IsNullOrWhiteSpace(link.Path))
{
writer.WriteElementString("path", link.Path);
}
10 years ago
writer.WriteEndElement();
}
}
/// <summary>
/// Gets the output trailer URL.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns>System.String.</returns>
private static string GetOutputTrailerUrl(string url)
{
// This is what xbmc expects
return url.Replace("https://www.youtube.com/watch?v=",
"plugin://plugin.video.youtube/?action=play_video&videoid=",
StringComparison.OrdinalIgnoreCase);
}
private static void AddImages(BaseItem item, XmlWriter writer, ILibraryManager libraryManager, IServerConfigurationManager config)
{
10 years ago
writer.WriteStartElement("art");
var image = item.GetImageInfo(ImageType.Primary, 0);
if (image != null)
{
writer.WriteElementString("poster", GetImagePathToSave(image, libraryManager, config));
}
foreach (var backdrop in item.GetImages(ImageType.Backdrop))
{
writer.WriteElementString("fanart", GetImagePathToSave(backdrop, libraryManager, config));
}
10 years ago
writer.WriteEndElement();
}
10 years ago
private static void AddUserData(BaseItem item, XmlWriter writer, IUserManager userManager, IUserDataManager userDataRepo, XbmcMetadataOptions options)
{
var userId = options.UserId;
if (string.IsNullOrWhiteSpace(userId))
{
return;
}
var user = userManager.GetUserById(userId);
if (user == null)
{
return;
}
if (item.IsFolder)
{
return;
}
var userdata = userDataRepo.GetUserData(user, item);
writer.WriteElementString("isuserfavorite", userdata.IsFavorite.ToString().ToLower());
if (userdata.Rating.HasValue)
{
writer.WriteElementString("userrating", userdata.Rating.Value.ToString(CultureInfo.InvariantCulture).ToLower());
}
if (!item.IsFolder)
{
writer.WriteElementString("playcount", userdata.PlayCount.ToString(UsCulture));
writer.WriteElementString("watched", userdata.Played.ToString().ToLower());
if (userdata.LastPlayedDate.HasValue)
{
writer.WriteElementString("lastplayed", userdata.LastPlayedDate.Value.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss").ToLower());
}
writer.WriteStartElement("resume");
var runTimeTicks = item.RunTimeTicks ?? 0;
writer.WriteElementString("position", TimeSpan.FromTicks(userdata.PlaybackPositionTicks).TotalSeconds.ToString(UsCulture));
writer.WriteElementString("total", TimeSpan.FromTicks(runTimeTicks).TotalSeconds.ToString(UsCulture));
}
10 years ago
writer.WriteEndElement();
}
private static void AddActors(List<PersonInfo> people, XmlWriter writer, ILibraryManager libraryManager, IFileSystem fileSystem, IServerConfigurationManager config)
{
var actors = people
.Where(i => !IsPersonType(i, PersonType.Director) && !IsPersonType(i, PersonType.Writer))
.ToList();
foreach (var person in actors)
{
10 years ago
writer.WriteStartElement("actor");
if (!string.IsNullOrWhiteSpace(person.Name))
{
writer.WriteElementString("name", person.Name);
}
if (!string.IsNullOrWhiteSpace(person.Role))
{
writer.WriteElementString("role", person.Role);
}
10 years ago
if (!string.IsNullOrWhiteSpace(person.Type))
{
writer.WriteElementString("type", person.Type);
}
if (person.SortOrder.HasValue)
{
10 years ago
writer.WriteElementString("sortorder", person.SortOrder.Value.ToString(UsCulture));
}
try
{
var personEntity = libraryManager.GetPerson(person.Name);
var image = personEntity.GetImageInfo(ImageType.Primary, 0);
if (image != null)
{
writer.WriteElementString("thumb", GetImagePathToSave(image, libraryManager, config));
}
}
catch (Exception)
{
// Already logged in core
}
10 years ago
writer.WriteEndElement();
}
}
private static string GetImagePathToSave(ItemImageInfo image, ILibraryManager libraryManager, IServerConfigurationManager config)
{
if (!image.IsLocalFile)
{
return image.Path;
}
return libraryManager.GetPathAfterNetworkSubstitution(image.Path);
}
10 years ago
private static bool IsPersonType(PersonInfo person, string type)
{
return string.Equals(person.Type, type, StringComparison.OrdinalIgnoreCase) || string.Equals(person.Role, type, StringComparison.OrdinalIgnoreCase);
}
private void AddCustomTags(string path, List<string> xmlTagsUsed, XmlWriter writer, ILogger logger, IFileSystem fileSystem)
{
var settings = XmlReaderSettingsFactory.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var fileStream = fileSystem.OpenRead(path))
{
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
10 years ago
{
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
try
10 years ago
{
reader.MoveToContent();
}
catch (Exception ex)
{
logger.ErrorException("Error reading existing xml tags from {0}.", ex, path);
return;
}
reader.Read();
// Loop through each element
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element)
10 years ago
{
var name = reader.Name;
if (!CommonTags.ContainsKey(name) && !xmlTagsUsed.Contains(name, StringComparer.OrdinalIgnoreCase))
{
writer.WriteNode(reader, false);
}
else
{
reader.Skip();
}
10 years ago
}
else
{
reader.Read();
}
10 years ago
}
}
}
}
}
}
}