so we can have multiple newznab definitions.pull/6/head
parent
50fd2f77b1
commit
96990eabb3
@ -1,38 +1,16 @@
|
|||||||
using System;
|
using NzbDrone.Common;
|
||||||
using NzbDrone.Common.Messaging;
|
|
||||||
|
|
||||||
namespace NzbDrone.Core.Indexers
|
namespace NzbDrone.Core.Indexers
|
||||||
{
|
{
|
||||||
public abstract class IndexerWithSetting<TSetting> :
|
public abstract class IndexerWithSetting<TSetting> : IndexerBase where TSetting : class, IIndexerSetting, new()
|
||||||
Indexer,
|
|
||||||
IHandle<IndexerSettingUpdatedEvent> where TSetting : IIndexerSetting, new()
|
|
||||||
{
|
{
|
||||||
protected IndexerWithSetting(IProviderIndexerSetting settingProvider)
|
|
||||||
{
|
|
||||||
Settings = settingProvider.Get<TSetting>(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool IsConfigured
|
|
||||||
{
|
|
||||||
get { return Settings.IsValid; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool EnabledByDefault
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public TSetting Settings { get; private set; }
|
public TSetting Settings { get; private set; }
|
||||||
|
|
||||||
public void Handle(IndexerSettingUpdatedEvent message)
|
public TSetting ImportSettingsFromJson(string json)
|
||||||
{
|
{
|
||||||
if (message.IndexerName.Equals(Name, StringComparison.InvariantCultureIgnoreCase))
|
Settings = new JsonSerializer().Deserialize<TSetting>(json) ?? new TSetting();
|
||||||
{
|
|
||||||
Settings = (TSetting)message.IndexerSetting;
|
return Settings;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,13 +0,0 @@
|
|||||||
using System;
|
|
||||||
using NzbDrone.Core.Datastore;
|
|
||||||
|
|
||||||
namespace NzbDrone.Core.Indexers.Newznab
|
|
||||||
{
|
|
||||||
public class NewznabDefinition : ModelBase
|
|
||||||
{
|
|
||||||
public Boolean Enable { get; set; }
|
|
||||||
public String Name { get; set; }
|
|
||||||
public String Url { get; set; }
|
|
||||||
public String ApiKey { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using NzbDrone.Core.Datastore;
|
|
||||||
|
|
||||||
namespace NzbDrone.Core.Indexers.Newznab
|
|
||||||
{
|
|
||||||
public interface INewznabRepository : IBasicRepository<NewznabDefinition>
|
|
||||||
{
|
|
||||||
IEnumerable<NewznabDefinition> Enabled();
|
|
||||||
}
|
|
||||||
|
|
||||||
public class NewznabRepository : BasicRepository<NewznabDefinition>, INewznabRepository
|
|
||||||
{
|
|
||||||
public NewznabRepository(IDatabase database)
|
|
||||||
: base(database)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<NewznabDefinition> Enabled()
|
|
||||||
{
|
|
||||||
return Query.Where(n => n.Enable);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,138 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using NLog;
|
|
||||||
using NzbDrone.Common.Messaging;
|
|
||||||
using NzbDrone.Core.Lifecycle;
|
|
||||||
|
|
||||||
namespace NzbDrone.Core.Indexers.Newznab
|
|
||||||
{
|
|
||||||
public interface INewznabService
|
|
||||||
{
|
|
||||||
List<NewznabDefinition> All();
|
|
||||||
List<NewznabDefinition> Enabled();
|
|
||||||
NewznabDefinition Insert(NewznabDefinition definition);
|
|
||||||
void Update(NewznabDefinition definition);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class NewznabService : INewznabService, IHandle<ApplicationStartedEvent>
|
|
||||||
{
|
|
||||||
private readonly INewznabRepository _newznabRepository;
|
|
||||||
private readonly Logger _logger;
|
|
||||||
|
|
||||||
public NewznabService(INewznabRepository newznabRepository, Logger logger)
|
|
||||||
{
|
|
||||||
_newznabRepository = newznabRepository;
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<NewznabDefinition> All()
|
|
||||||
{
|
|
||||||
return _newznabRepository.All().ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<NewznabDefinition> Enabled()
|
|
||||||
{
|
|
||||||
return _newznabRepository.Enabled().ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public NewznabDefinition Insert(NewznabDefinition definition)
|
|
||||||
{
|
|
||||||
definition.Url = definition.Url.ToLower();
|
|
||||||
_logger.Debug("Adding Newznab definition for {0}", definition.Name);
|
|
||||||
return _newznabRepository.Insert(definition);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Update(NewznabDefinition definition)
|
|
||||||
{
|
|
||||||
definition.Url = definition.Url.ToLower();
|
|
||||||
_logger.Debug("Updating Newznab definition for {0}", definition.Name);
|
|
||||||
_newznabRepository.Update(definition);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Delete(int id)
|
|
||||||
{
|
|
||||||
|
|
||||||
_newznabRepository.Delete(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CheckHostname(string url)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var uri = new Uri(url);
|
|
||||||
var hostname = uri.DnsSafeHost;
|
|
||||||
|
|
||||||
Dns.GetHostEntry(hostname);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.Error("Invalid address {0}, please correct the site URL.", url);
|
|
||||||
_logger.TraceException(ex.Message, ex);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Handle(ApplicationStartedEvent message)
|
|
||||||
{
|
|
||||||
var newznabIndexers = new List<NewznabDefinition>
|
|
||||||
{
|
|
||||||
new NewznabDefinition { Enable = false, Name = "Nzbs.org", Url = "http://nzbs.org" },
|
|
||||||
new NewznabDefinition { Enable = false, Name = "Nzb.su", Url = "https://nzb.su" },
|
|
||||||
new NewznabDefinition { Enable = false, Name = "Dognzb.cr", Url = "https://dognzb.cr" }
|
|
||||||
};
|
|
||||||
|
|
||||||
_logger.Debug("Initializing Newznab indexers. Count {0}", newznabIndexers);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var currentIndexers = All();
|
|
||||||
|
|
||||||
_logger.Debug("Deleting broken Newznab indexer");
|
|
||||||
var brokenIndexers = currentIndexers.Where(i => String.IsNullOrEmpty(i.Name) || String.IsNullOrWhiteSpace(i.Url)).ToList();
|
|
||||||
brokenIndexers.ForEach(e => Delete(e.Id));
|
|
||||||
|
|
||||||
currentIndexers = All();
|
|
||||||
|
|
||||||
foreach (var feedProvider in newznabIndexers)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
NewznabDefinition indexerLocal = feedProvider;
|
|
||||||
var currentIndexer = currentIndexers
|
|
||||||
.FirstOrDefault(c => new Uri(c.Url.ToLower()).Host == new Uri(indexerLocal.Url.ToLower()).Host);
|
|
||||||
|
|
||||||
if (currentIndexer == null)
|
|
||||||
{
|
|
||||||
var definition = new NewznabDefinition
|
|
||||||
{
|
|
||||||
Enable = false,
|
|
||||||
Name = indexerLocal.Name,
|
|
||||||
Url = indexerLocal.Url,
|
|
||||||
ApiKey = indexerLocal.ApiKey,
|
|
||||||
};
|
|
||||||
|
|
||||||
Insert(definition);
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
currentIndexer.Url = indexerLocal.Url;
|
|
||||||
Update(currentIndexer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("An error occurred while setting up indexer: " + feedProvider.Name, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("An Error occurred while initializing Newznab Indexers", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Indexers.Newznab
|
||||||
|
{
|
||||||
|
public class NewznabSettings : IIndexerSetting
|
||||||
|
{
|
||||||
|
public String Url { get; set; }
|
||||||
|
public String ApiKey { get; set; }
|
||||||
|
|
||||||
|
public bool IsValid
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return !string.IsNullOrWhiteSpace(Url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue