2013-04-07 09:30:37 +02:00
|
|
|
using System;
|
2013-02-23 05:37:23 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using NLog;
|
2013-04-10 02:47:04 +02:00
|
|
|
using NzbDrone.Common.Eventing;
|
2013-02-23 05:37:23 +01:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-03-02 19:25:39 +01:00
|
|
|
|
2013-04-07 09:30:37 +02:00
|
|
|
namespace NzbDrone.Core.Indexers.Newznab
|
2013-02-23 05:37:23 +01:00
|
|
|
{
|
|
|
|
public interface INewznabService
|
|
|
|
{
|
|
|
|
List<NewznabDefinition> All();
|
|
|
|
List<NewznabDefinition> Enabled();
|
|
|
|
NewznabDefinition Insert(NewznabDefinition definition);
|
|
|
|
void Update(NewznabDefinition definition);
|
|
|
|
}
|
|
|
|
|
2013-04-10 02:47:04 +02:00
|
|
|
public class NewznabService : INewznabService, IHandle<ApplicationStartedEvent>
|
2013-02-23 05:37:23 +01:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2013-04-07 09:30:37 +02:00
|
|
|
|
2013-02-23 05:37:23 +01:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-10 02:47:04 +02:00
|
|
|
public void Handle(ApplicationStartedEvent message)
|
2013-02-23 05:37:23 +01:00
|
|
|
{
|
|
|
|
var newznabIndexers = new List<NewznabDefinition>
|
|
|
|
{
|
2013-04-07 09:30:37 +02:00
|
|
|
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" }
|
2013-02-23 05:37:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
_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();
|
2013-02-26 04:58:57 +01:00
|
|
|
brokenIndexers.ForEach(e => Delete(e.Id));
|
2013-02-23 05:37:23 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2013-03-25 05:36:24 +01:00
|
|
|
Enable = false,
|
2013-02-23 05:37:23 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|