2012-07-08 01:52:04 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-02-19 07:01:03 +01:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2012-07-08 01:52:04 +02:00
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Providers.ExternalNotification;
|
|
|
|
|
using NzbDrone.Core.Providers.Metadata;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
using PetaPoco;
|
2012-10-17 02:08:47 +02:00
|
|
|
|
using TvdbLib.Data;
|
2012-07-08 01:52:04 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class MetadataProvider
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
private readonly IDatabase _database;
|
|
|
|
|
|
2013-01-19 05:46:43 +01:00
|
|
|
|
private IList<MetadataBase> _metadataProviders;
|
2012-07-10 06:37:24 +02:00
|
|
|
|
private readonly TvDbProvider _tvDbProvider;
|
2012-07-08 01:52:04 +02:00
|
|
|
|
|
2012-07-14 10:17:37 +02:00
|
|
|
|
public MetadataProvider(IDatabase database, IEnumerable<MetadataBase> metadataProviders,
|
|
|
|
|
TvDbProvider tvDbProvider)
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
|
|
|
|
_database = database;
|
2013-01-19 05:46:43 +01:00
|
|
|
|
_metadataProviders = metadataProviders.ToList();
|
2012-07-10 06:37:24 +02:00
|
|
|
|
_tvDbProvider = tvDbProvider;
|
2013-01-19 05:46:43 +01:00
|
|
|
|
|
|
|
|
|
Initialize(_metadataProviders);
|
2012-07-08 01:52:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MetadataProvider()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-10 06:37:24 +02:00
|
|
|
|
public virtual List<MetadataDefinition> All()
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
2012-07-10 06:37:24 +02:00
|
|
|
|
return _database.Fetch<MetadataDefinition>();
|
2012-07-08 01:52:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-10 06:37:24 +02:00
|
|
|
|
public virtual void SaveSettings(MetadataDefinition settings)
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
|
|
|
|
if (settings.Id == 0)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Adding Metabase definition for {0}", settings.Name);
|
|
|
|
|
_database.Insert(settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Updating Metabase definition for {0}", settings.Name);
|
|
|
|
|
_database.Update(settings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-10 06:37:24 +02:00
|
|
|
|
public virtual MetadataDefinition GetSettings(Type type)
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
2012-07-10 06:37:24 +02:00
|
|
|
|
return _database.SingleOrDefault<MetadataDefinition>("WHERE MetadataProviderType = @0", type.ToString());
|
2012-07-08 01:52:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-10 06:37:24 +02:00
|
|
|
|
public virtual IList<MetadataBase> GetEnabledMetabaseProviders()
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
|
|
|
|
var all = All();
|
2012-07-10 06:37:24 +02:00
|
|
|
|
return _metadataProviders.Where(i => all.Exists(c => c.MetadataProviderType == i.GetType().ToString() && c.Enable)).ToList();
|
2012-07-08 01:52:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 05:46:43 +01:00
|
|
|
|
private void Initialize(IList<MetadataBase> metabaseProviders)
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
2012-07-10 06:37:24 +02:00
|
|
|
|
Logger.Debug("Initializing metabases. Count {0}", metabaseProviders.Count);
|
2012-07-08 01:52:04 +02:00
|
|
|
|
|
2012-07-10 06:37:24 +02:00
|
|
|
|
_metadataProviders = metabaseProviders;
|
2012-07-08 01:52:04 +02:00
|
|
|
|
|
|
|
|
|
var currentNotifiers = All();
|
|
|
|
|
|
2012-07-10 06:37:24 +02:00
|
|
|
|
foreach (var notificationProvider in metabaseProviders)
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
|
|
|
|
MetadataBase metadataProviderLocal = notificationProvider;
|
|
|
|
|
if (!currentNotifiers.Exists(c => c.MetadataProviderType == metadataProviderLocal.GetType().ToString()))
|
|
|
|
|
{
|
2012-07-10 06:37:24 +02:00
|
|
|
|
var settings = new MetadataDefinition
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
|
|
|
|
Enable = false,
|
|
|
|
|
MetadataProviderType = metadataProviderLocal.GetType().ToString(),
|
|
|
|
|
Name = metadataProviderLocal.Name
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SaveSettings(settings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-10 06:37:24 +02:00
|
|
|
|
public virtual void CreateForSeries(Series series)
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
2012-10-17 02:08:47 +02:00
|
|
|
|
var tvDbSeries = _tvDbProvider.GetSeries(series.SeriesId, false, true);
|
2012-07-08 01:52:04 +02:00
|
|
|
|
|
2012-07-12 20:30:43 +02:00
|
|
|
|
CreateForSeries(series, tvDbSeries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void CreateForSeries(Series series, TvdbSeries tvDbSeries)
|
|
|
|
|
{
|
2012-07-10 06:37:24 +02:00
|
|
|
|
foreach (var provider in _metadataProviders.Where(i => GetSettings(i.GetType()).Enable))
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
2012-07-12 23:08:21 +02:00
|
|
|
|
provider.CreateForSeries(series, tvDbSeries);
|
2012-07-08 01:52:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-10 06:37:24 +02:00
|
|
|
|
public virtual void CreateForEpisodeFile(EpisodeFile episodeFile)
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
2012-10-17 02:08:47 +02:00
|
|
|
|
var tvDbSeries = _tvDbProvider.GetSeries(episodeFile.SeriesId, true, true);
|
2012-07-08 01:52:04 +02:00
|
|
|
|
|
2012-07-12 23:08:21 +02:00
|
|
|
|
CreateForEpisodeFile(episodeFile, tvDbSeries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void CreateForEpisodeFile(EpisodeFile episodeFile, TvdbSeries tvDbSeries)
|
|
|
|
|
{
|
|
|
|
|
foreach (var provider in _metadataProviders.Where(i => GetSettings(i.GetType()).Enable))
|
|
|
|
|
{
|
|
|
|
|
provider.CreateForEpisodeFile(episodeFile, tvDbSeries);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void CreateForEpisodeFiles(List<EpisodeFile> episodeFiles)
|
|
|
|
|
{
|
2012-07-14 10:17:37 +02:00
|
|
|
|
if (episodeFiles == null || !episodeFiles.Any())
|
2012-08-06 20:02:37 +02:00
|
|
|
|
{
|
|
|
|
|
Logger.Warn("No episode files, no metadata will be created.");
|
|
|
|
|
throw new ArgumentException("EpsiodeFiles must not be null or empty", "episodeFiles");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Trace("Creating metadata for {0} files.", episodeFiles.Count);
|
|
|
|
|
|
2012-10-17 02:08:47 +02:00
|
|
|
|
var tvDbSeries = _tvDbProvider.GetSeries(episodeFiles.First().SeriesId, true, true);
|
2012-07-12 23:08:21 +02:00
|
|
|
|
|
|
|
|
|
foreach(var episodeFile in episodeFiles)
|
|
|
|
|
{
|
|
|
|
|
foreach (var provider in _metadataProviders.Where(i => GetSettings(i.GetType()).Enable))
|
|
|
|
|
{
|
2012-08-06 20:02:37 +02:00
|
|
|
|
Logger.Trace("Creating {0} metadata for {1}", provider.Name, episodeFile.EpisodeFileId);
|
2012-07-12 23:08:21 +02:00
|
|
|
|
provider.CreateForEpisodeFile(episodeFile, tvDbSeries);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void RemoveForSeries(Series series)
|
|
|
|
|
{
|
2012-07-10 06:37:24 +02:00
|
|
|
|
foreach (var provider in _metadataProviders.Where(i => GetSettings(i.GetType()).Enable))
|
2012-07-08 01:52:04 +02:00
|
|
|
|
{
|
2012-07-12 23:08:21 +02:00
|
|
|
|
provider.RemoveForSeries(series);
|
2012-07-08 01:52:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-12 23:08:21 +02:00
|
|
|
|
|
|
|
|
|
public virtual void RemoveForEpisodeFile(EpisodeFile episodeFile)
|
|
|
|
|
{
|
|
|
|
|
foreach (var provider in _metadataProviders.Where(i => GetSettings(i.GetType()).Enable))
|
|
|
|
|
{
|
|
|
|
|
provider.RemoveForEpisodeFile(episodeFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void RemoveForEpisodeFiles(List<EpisodeFile> episodeFiles)
|
|
|
|
|
{
|
|
|
|
|
foreach (var episodeFile in episodeFiles)
|
|
|
|
|
{
|
|
|
|
|
foreach (var provider in _metadataProviders.Where(i => GetSettings(i.GetType()).Enable))
|
|
|
|
|
{
|
|
|
|
|
provider.RemoveForEpisodeFile(episodeFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-08 01:52:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|