jellyfin/MediaBrowser.Providers/Savers/MovieXmlSaver.cs

144 lines
4.4 KiB
C#
Raw Normal View History

2014-02-10 19:39:41 +01:00
using MediaBrowser.Controller.Entities;
2013-06-09 18:23:06 +02:00
using MediaBrowser.Controller.Entities.Movies;
2013-12-29 06:32:03 +01:00
using MediaBrowser.Controller.Entities.TV;
2013-06-09 18:23:06 +02:00
using MediaBrowser.Controller.Library;
2013-08-12 21:18:31 +02:00
using MediaBrowser.Controller.Persistence;
2014-02-10 19:39:41 +01:00
using MediaBrowser.Model.Entities;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security;
2013-06-23 20:55:30 +02:00
using System.Text;
2013-06-09 18:23:06 +02:00
using System.Threading;
namespace MediaBrowser.Providers.Savers
2013-06-09 18:23:06 +02:00
{
/// <summary>
/// Saves movie.xml for movies, trailers and music videos
/// </summary>
public class MovieXmlSaver : IMetadataFileSaver
2013-06-09 18:23:06 +02:00
{
2013-08-12 21:18:31 +02:00
private readonly IItemRepository _itemRepository;
2013-06-23 20:55:30 +02:00
2014-02-10 19:39:41 +01:00
public MovieXmlSaver(IItemRepository itemRepository)
2013-06-23 20:55:30 +02:00
{
2013-08-12 21:18:31 +02:00
_itemRepository = itemRepository;
2013-06-23 20:55:30 +02:00
}
2014-02-02 14:36:31 +01:00
public string Name
{
get
{
2014-02-03 06:35:43 +01:00
return "Media Browser Xml";
2014-02-02 14:36:31 +01:00
}
}
2013-06-09 18:23:06 +02:00
/// <summary>
/// Determines whether [is enabled for] [the specified item].
2013-06-09 18:23:06 +02:00
/// </summary>
/// <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>
2014-02-02 14:36:31 +01:00
public bool IsEnabledFor(IHasMetadata item, ItemUpdateType updateType)
2013-06-09 18:23:06 +02:00
{
2014-02-10 21:11:46 +01:00
if (!item.SupportsLocalMetadata)
2014-02-08 21:02:35 +01:00
{
return false;
}
2014-02-11 22:41:01 +01:00
var video = item as Video;
2014-02-11 22:41:01 +01:00
// Check parent for null to avoid running this against things like video backdrops
if (video != null && !(item is Episode) && !video.IsOwnedItem)
2013-06-09 18:23:06 +02:00
{
2014-02-11 22:41:01 +01:00
return updateType >= ItemUpdateType.MetadataDownload;
2013-06-09 18:23:06 +02:00
}
return false;
2013-06-09 18:23:06 +02:00
}
2013-07-01 18:00:20 +02:00
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
2013-06-09 18:23:06 +02:00
/// <summary>
/// Saves the specified item.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
2014-02-02 14:36:31 +01:00
public void Save(IHasMetadata item, CancellationToken cancellationToken)
2013-06-09 18:23:06 +02:00
{
2014-02-02 14:36:31 +01:00
var video = (Video)item;
var builder = new StringBuilder();
builder.Append("<Title>");
2014-02-02 14:36:31 +01:00
XmlSaverHelpers.AddCommonNodes(video, builder);
2013-07-01 18:00:20 +02:00
2014-02-02 14:36:31 +01:00
if (video.CommunityRating.HasValue)
2013-07-01 18:00:20 +02:00
{
2014-02-02 14:36:31 +01:00
builder.Append("<IMDBrating>" + SecurityElement.Escape(video.CommunityRating.Value.ToString(UsCulture)) + "</IMDBrating>");
2013-07-01 18:00:20 +02:00
}
2014-02-02 14:36:31 +01:00
if (!string.IsNullOrEmpty(video.Overview))
2013-07-01 18:00:20 +02:00
{
2014-02-02 14:36:31 +01:00
builder.Append("<Description><![CDATA[" + video.Overview + "]]></Description>");
2013-07-01 18:00:20 +02:00
}
var musicVideo = item as MusicVideo;
if (musicVideo != null)
{
if (!string.IsNullOrEmpty(musicVideo.Artist))
{
builder.Append("<Artist>" + SecurityElement.Escape(musicVideo.Artist) + "</Artist>");
}
if (!string.IsNullOrEmpty(musicVideo.Album))
{
builder.Append("<Album>" + SecurityElement.Escape(musicVideo.Album) + "</Album>");
}
}
2013-10-17 17:34:47 +02:00
var movie = item as Movie;
if (movie != null)
{
if (!string.IsNullOrEmpty(movie.TmdbCollectionName))
{
builder.Append("<TmdbCollectionName>" + SecurityElement.Escape(movie.TmdbCollectionName) + "</TmdbCollectionName>");
}
}
2013-08-31 01:55:17 +02:00
XmlSaverHelpers.AddMediaInfo(video, builder, _itemRepository);
2013-08-12 21:18:31 +02:00
builder.Append("</Title>");
2013-06-10 03:40:53 +02:00
var xmlFilePath = GetSavePath(item);
2013-06-10 03:40:53 +02:00
2013-09-25 17:11:23 +02:00
XmlSaverHelpers.Save(builder, xmlFilePath, new List<string>
2013-07-01 18:00:20 +02:00
{
"IMDBrating",
2013-07-12 21:56:40 +02:00
"Description",
"Artist",
"Album",
"TmdbCollectionName"
2013-07-01 18:00:20 +02:00
});
}
2014-02-02 14:36:31 +01:00
public string GetSavePath(IHasMetadata item)
{
2014-02-02 14:36:31 +01:00
return GetMovieSavePath((Video)item);
}
2014-02-02 14:36:31 +01:00
public static string GetMovieSavePath(Video item)
{
if (item.IsInMixedFolder)
{
return Path.ChangeExtension(item.Path, ".xml");
}
return Path.Combine(item.ContainingFolderPath, "movie.xml");
}
2013-06-09 18:23:06 +02:00
}
}