sonarr-repo-only/NzbDrone.Core/Providers/MetadataProvider.cs

111 lines
3.7 KiB
C#
Raw Normal View History

2012-07-07 23:52:04 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Ninject;
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers.ExternalNotification;
using NzbDrone.Core.Providers.Metadata;
using NzbDrone.Core.Repository;
using PetaPoco;
namespace NzbDrone.Core.Providers
{
public class MetadataProvider
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IDatabase _database;
2012-07-10 04:37:24 +00:00
private IEnumerable<MetadataBase> _metadataProviders;
private readonly TvDbProvider _tvDbProvider;
2012-07-07 23:52:04 +00:00
[Inject]
2012-07-10 04:37:24 +00:00
public MetadataProvider(IDatabase database, IEnumerable<MetadataBase> metadataProviders, TvDbProvider tvDbProvider)
2012-07-07 23:52:04 +00:00
{
_database = database;
2012-07-10 04:37:24 +00:00
_metadataProviders = metadataProviders;
_tvDbProvider = tvDbProvider;
2012-07-07 23:52:04 +00:00
}
public MetadataProvider()
{
}
2012-07-10 04:37:24 +00:00
public virtual List<MetadataDefinition> All()
2012-07-07 23:52:04 +00:00
{
2012-07-10 04:37:24 +00:00
return _database.Fetch<MetadataDefinition>();
2012-07-07 23:52:04 +00:00
}
2012-07-10 04:37:24 +00:00
public virtual void SaveSettings(MetadataDefinition settings)
2012-07-07 23:52:04 +00: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 04:37:24 +00:00
public virtual MetadataDefinition GetSettings(Type type)
2012-07-07 23:52:04 +00:00
{
2012-07-10 04:37:24 +00:00
return _database.SingleOrDefault<MetadataDefinition>("WHERE MetadataProviderType = @0", type.ToString());
2012-07-07 23:52:04 +00:00
}
2012-07-10 04:37:24 +00:00
public virtual IList<MetadataBase> GetEnabledMetabaseProviders()
2012-07-07 23:52:04 +00:00
{
var all = All();
2012-07-10 04:37:24 +00:00
return _metadataProviders.Where(i => all.Exists(c => c.MetadataProviderType == i.GetType().ToString() && c.Enable)).ToList();
2012-07-07 23:52:04 +00:00
}
2012-07-10 04:37:24 +00:00
public virtual void Initialize(IList<MetadataBase> metabaseProviders)
2012-07-07 23:52:04 +00:00
{
2012-07-10 04:37:24 +00:00
Logger.Debug("Initializing metabases. Count {0}", metabaseProviders.Count);
2012-07-07 23:52:04 +00:00
2012-07-10 04:37:24 +00:00
_metadataProviders = metabaseProviders;
2012-07-07 23:52:04 +00:00
var currentNotifiers = All();
2012-07-10 04:37:24 +00:00
foreach (var notificationProvider in metabaseProviders)
2012-07-07 23:52:04 +00:00
{
MetadataBase metadataProviderLocal = notificationProvider;
if (!currentNotifiers.Exists(c => c.MetadataProviderType == metadataProviderLocal.GetType().ToString()))
{
2012-07-10 04:37:24 +00:00
var settings = new MetadataDefinition
2012-07-07 23:52:04 +00:00
{
Enable = false,
MetadataProviderType = metadataProviderLocal.GetType().ToString(),
Name = metadataProviderLocal.Name
};
SaveSettings(settings);
}
}
}
2012-07-10 04:37:24 +00:00
public virtual void CreateForSeries(Series series)
2012-07-07 23:52:04 +00:00
{
2012-07-10 04:37:24 +00:00
var tvDbSeries = _tvDbProvider.GetSeries(series.SeriesId, false, true);
2012-07-07 23:52:04 +00:00
2012-07-10 04:37:24 +00:00
foreach (var provider in _metadataProviders.Where(i => GetSettings(i.GetType()).Enable))
2012-07-07 23:52:04 +00:00
{
2012-07-10 04:37:24 +00:00
provider.ForSeries(series, tvDbSeries);
2012-07-07 23:52:04 +00:00
}
}
2012-07-10 04:37:24 +00:00
public virtual void CreateForEpisodeFile(EpisodeFile episodeFile)
2012-07-07 23:52:04 +00:00
{
2012-07-10 04:37:24 +00:00
var tvDbSeries = _tvDbProvider.GetSeries(episodeFile.SeriesId, true, true);
2012-07-07 23:52:04 +00:00
2012-07-10 04:37:24 +00:00
foreach (var provider in _metadataProviders.Where(i => GetSettings(i.GetType()).Enable))
2012-07-07 23:52:04 +00:00
{
2012-07-10 04:37:24 +00:00
provider.ForEpisodeFile(episodeFile, tvDbSeries);
2012-07-07 23:52:04 +00:00
}
}
}
}