2011-11-13 21:51:15 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2012-05-11 19:26:56 +02:00
|
|
|
|
using System.Net;
|
2011-11-13 21:51:15 +01:00
|
|
|
|
using Ninject;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Providers.Indexer;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
using PetaPoco;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class NewznabProvider
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
private readonly IDatabase _database;
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
public NewznabProvider(IDatabase database)
|
|
|
|
|
{
|
|
|
|
|
_database = database;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NewznabProvider()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual List<NewznabDefinition> Enabled()
|
|
|
|
|
{
|
|
|
|
|
return _database.Fetch<NewznabDefinition>("WHERE Enable = 1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual List<NewznabDefinition> All()
|
|
|
|
|
{
|
|
|
|
|
return _database.Fetch<NewznabDefinition>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual int Save(NewznabDefinition definition)
|
|
|
|
|
{
|
2011-11-16 00:11:17 +01:00
|
|
|
|
//Cleanup the URL if it is not null or whitespace
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(definition.Url))
|
|
|
|
|
definition.Url = (new Uri(definition.Url).ParentUriString());
|
2011-11-13 21:51:15 +01:00
|
|
|
|
|
|
|
|
|
if (definition.Id == 0)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Adding Newznab definitions for {0}", definition.Name);
|
|
|
|
|
return Convert.ToInt32(_database.Insert(definition));
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-01 02:37:36 +01:00
|
|
|
|
Logger.Debug("Updating Newznab definitions for {0}", definition.Name);
|
|
|
|
|
return _database.Update(definition);
|
2011-11-13 21:51:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void SaveAll(IEnumerable<NewznabDefinition> definitions)
|
|
|
|
|
{
|
|
|
|
|
var definitionsList = definitions.ToList();
|
|
|
|
|
|
|
|
|
|
//Cleanup the URL for each definition
|
2012-05-11 19:26:56 +02:00
|
|
|
|
foreach(var newznabDefinition in definitionsList)
|
|
|
|
|
{
|
|
|
|
|
CheckHostname(newznabDefinition.Url);
|
|
|
|
|
newznabDefinition.Url = new Uri(newznabDefinition.Url).ParentUriString();
|
|
|
|
|
}
|
2011-11-13 21:51:15 +01:00
|
|
|
|
|
|
|
|
|
_database.UpdateMany(definitionsList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void InitializeNewznabIndexers(IList<NewznabDefinition> indexers)
|
|
|
|
|
{
|
2012-02-13 08:28:01 +01:00
|
|
|
|
Logger.Debug("Initializing Newznab indexers. Count {0}", indexers.Count);
|
2011-11-13 21:51:15 +01:00
|
|
|
|
|
2012-05-12 18:31:54 +02:00
|
|
|
|
try
|
2011-11-13 21:51:15 +01:00
|
|
|
|
{
|
2012-05-12 18:31:54 +02:00
|
|
|
|
var currentIndexers = All();
|
2012-05-04 17:30:30 +02:00
|
|
|
|
|
2012-05-12 19:16:07 +02:00
|
|
|
|
Logger.Debug("Deleting broken Newznab indexer");
|
|
|
|
|
var brokenIndexers = currentIndexers.Where(i => String.IsNullOrEmpty(i.Name) || String.IsNullOrWhiteSpace(i.Url)).ToList();
|
|
|
|
|
brokenIndexers.ForEach(e => _database.Delete<NewznabDefinition>(e.Id));
|
|
|
|
|
|
|
|
|
|
currentIndexers = All();
|
|
|
|
|
|
2012-05-12 18:31:54 +02:00
|
|
|
|
foreach(var feedProvider in indexers)
|
2011-11-13 21:51:15 +01:00
|
|
|
|
{
|
2012-05-12 18:31:54 +02:00
|
|
|
|
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 settings = new NewznabDefinition
|
|
|
|
|
{
|
|
|
|
|
Enable = false,
|
|
|
|
|
Name = indexerLocal.Name,
|
|
|
|
|
Url = indexerLocal.Url,
|
|
|
|
|
ApiKey = indexerLocal.ApiKey,
|
|
|
|
|
BuiltIn = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Save(settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-08-11 23:55:53 +02:00
|
|
|
|
currentIndexer.Url = indexerLocal.Url;
|
2012-05-12 18:31:54 +02:00
|
|
|
|
currentIndexer.BuiltIn = true;
|
|
|
|
|
Save(currentIndexer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("An error occurred while setting up indexer: " + feedProvider.Name, ex);
|
|
|
|
|
}
|
2012-05-04 17:30:30 +02:00
|
|
|
|
}
|
2011-11-13 21:51:15 +01:00
|
|
|
|
}
|
2012-05-12 18:31:54 +02:00
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("An Error occurred while initializing Newznab Indexers", ex);
|
|
|
|
|
}
|
2011-11-13 21:51:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Delete(int id)
|
|
|
|
|
{
|
|
|
|
|
_database.Delete<NewznabDefinition>(id);
|
|
|
|
|
}
|
2012-05-11 19:26:56 +02:00
|
|
|
|
|
|
|
|
|
public virtual 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);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2011-11-13 21:51:15 +01:00
|
|
|
|
}
|
|
|
|
|
}
|