using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security;
using System.Text;
using System.Threading;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.XbmcMetadata.Savers
{
public class SeriesXmlSaver : IMetadataFileSaver
{
private readonly ILibraryManager _libraryManager;
private readonly IUserManager _userManager;
private readonly IUserDataManager _userDataRepo;
private readonly IFileSystem _fileSystem;
private readonly IServerConfigurationManager _config;
public SeriesXmlSaver(IServerConfigurationManager config, ILibraryManager libraryManager, IUserManager userManager, IUserDataManager userDataRepo, IFileSystem fileSystem)
{
_config = config;
_libraryManager = libraryManager;
_userManager = userManager;
_userDataRepo = userDataRepo;
_fileSystem = fileSystem;
}
public string Name
{
get
{
return "Xbmc Nfo";
}
}
public string GetSavePath(IHasMetadata item)
{
return Path.Combine(item.Path, "tvshow.nfo");
}
public void Save(IHasMetadata item, CancellationToken cancellationToken)
{
var series = (Series)item;
var builder = new StringBuilder();
builder.Append("");
XmlSaverHelpers.AddCommonNodes(series, builder, _libraryManager, _userManager, _userDataRepo, _fileSystem, _config);
var tvdb = item.GetProviderId(MetadataProviders.Tvdb);
if (!string.IsNullOrEmpty(tvdb))
{
builder.Append("" + SecurityElement.Escape(tvdb) + "");
builder.AppendFormat("http://www.thetvdb.com/api/1D62F2F90030C444/series/{0}/all/{1}.zip",
tvdb,
string.IsNullOrEmpty(_config.Configuration.PreferredMetadataLanguage) ? "en" : _config.Configuration.PreferredMetadataLanguage);
}
builder.Append("-1");
builder.Append("-1");
if (series.Status.HasValue)
{
builder.Append("" + SecurityElement.Escape(series.Status.Value.ToString()) + "");
}
if (!string.IsNullOrEmpty(series.AirTime))
{
builder.Append("" + SecurityElement.Escape(series.AirTime) + "");
}
if (series.AirDays.Count == 7)
{
builder.Append("" + SecurityElement.Escape("Daily") + "");
}
else if (series.AirDays.Count > 0)
{
builder.Append("" + SecurityElement.Escape(series.AirDays[0].ToString()) + "");
}
if (series.AnimeSeriesIndex.HasValue)
{
builder.Append("" + SecurityElement.Escape(series.AnimeSeriesIndex.Value.ToString(CultureInfo.InvariantCulture)) + "");
}
builder.Append("");
var xmlFilePath = GetSavePath(item);
XmlSaverHelpers.Save(builder, xmlFilePath, new List
{
"id",
"episodeguide",
"season",
"episode",
"status",
"airs_time",
"airs_dayofweek",
"animeseriesindex"
});
}
public bool IsEnabledFor(IHasMetadata item, ItemUpdateType updateType)
{
var locationType = item.LocationType;
if (locationType == LocationType.Remote || locationType == LocationType.Virtual)
{
return false;
}
// If new metadata has been downloaded or metadata was manually edited, proceed
if ((updateType & ItemUpdateType.MetadataDownload) == ItemUpdateType.MetadataDownload
|| (updateType & ItemUpdateType.MetadataEdit) == ItemUpdateType.MetadataEdit)
{
return item is Series;
}
return false;
}
}
}