Initial commit for Metadata

This commit is contained in:
Mark McDowall 2012-07-07 16:52:04 -07:00
parent a087c89903
commit cc5b2c3e40
3 changed files with 190 additions and 0 deletions

View File

@ -0,0 +1,52 @@
using System;
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers.Metadata
{
public abstract class MetadataBase
{
protected readonly Logger _logger;
protected readonly ConfigProvider _configProvider;
protected MetadataBase(ConfigProvider configProvider)
{
_configProvider = configProvider;
_logger = LogManager.GetLogger(GetType().ToString());
}
/// <summary>
/// Gets the name for the notification provider
/// </summary>
public abstract string Name { get; }
/// <summary>
/// Performs the on grab action
/// </summary>
/// <param name = "message">The message to send to the receiver</param>
public abstract void OnGrab(string message);
/// <summary>
/// Performs the on download action
/// </summary>
/// <param name = "message">The message to send to the receiver</param>
/// <param name = "series">The Series for the new download</param>
public abstract void OnDownload(string message, Series series);
/// <summary>
/// Performs the on rename action
/// </summary>
/// <param name = "message">The message to send to the receiver</param>
/// <param name = "series">The Series for the new download</param>
public abstract void OnRename(string message, Series series);
/// <summary>
/// Performs the after rename action, this will be handled after all renaming for episode/season/series
/// </summary>
/// <param name = "message">The message to send to the receiver</param>
/// <param name = "series">The Series for the new download</param>
public abstract void AfterRename(string message, Series series);
}
}

View File

@ -0,0 +1,121 @@
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;
private IEnumerable<MetadataBase> _metadataBases;
[Inject]
public MetadataProvider(IDatabase database, IEnumerable<MetadataBase> metadataBases)
{
_database = database;
_metadataBases = metadataBases;
}
public MetadataProvider()
{
}
public virtual List<MetabaseDefinition> All()
{
return _database.Fetch<MetabaseDefinition>();
}
public virtual void SaveSettings(MetabaseDefinition settings)
{
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);
}
}
public virtual MetabaseDefinition GetSettings(Type type)
{
return _database.SingleOrDefault<MetabaseDefinition>("WHERE MetadataProviderType = @0", type.ToString());
}
public virtual IList<MetadataBase> GetEnabledExternalNotifiers()
{
var all = All();
return _metadataBases.Where(i => all.Exists(c => c.MetadataProviderType == i.GetType().ToString() && c.Enable)).ToList();
}
public virtual void InitializeNotifiers(IList<MetadataBase> notifiers)
{
Logger.Debug("Initializing notifiers. Count {0}", notifiers.Count);
_metadataBases = notifiers;
var currentNotifiers = All();
foreach (var notificationProvider in notifiers)
{
MetadataBase metadataProviderLocal = notificationProvider;
if (!currentNotifiers.Exists(c => c.MetadataProviderType == metadataProviderLocal.GetType().ToString()))
{
var settings = new MetabaseDefinition
{
Enable = false,
MetadataProviderType = metadataProviderLocal.GetType().ToString(),
Name = metadataProviderLocal.Name
};
SaveSettings(settings);
}
}
}
public virtual void OnGrab(string message)
{
foreach (var notifier in _metadataBases.Where(i => GetSettings(i.GetType()).Enable))
{
notifier.OnGrab(message);
}
}
public virtual void OnDownload(string message, Series series)
{
foreach (var notifier in _metadataBases.Where(i => GetSettings(i.GetType()).Enable))
{
notifier.OnDownload(message, series);
}
}
public virtual void OnRename(string message, Series series)
{
foreach (var notifier in _metadataBases.Where(i => GetSettings(i.GetType()).Enable))
{
notifier.OnRename(message, series);
}
}
public virtual void AfterRename(string message, Series series)
{
foreach (var notifier in _metadataBases.Where(i => GetSettings(i.GetType()).Enable))
{
notifier.AfterRename(message, series);
}
}
}
}

View File

@ -0,0 +1,17 @@
using PetaPoco;
namespace NzbDrone.Core.Repository
{
[TableName("MetabaseDefinitions")]
[PrimaryKey("Id", autoIncrement = true)]
public class MetabaseDefinition
{
public int Id { get; set; }
public bool Enable { get; set; }
public string MetadataProviderType { get; set; }
public string Name { get; set; }
}
}