2011-01-29 07:10:22 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using NLog;
|
2011-03-17 08:40:23 +01:00
|
|
|
|
using NzbDrone.Core.Helpers;
|
2011-01-29 07:10:22 +01:00
|
|
|
|
using NzbDrone.Core.Model;
|
2011-01-31 02:30:59 +01:00
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-01-29 07:10:22 +01:00
|
|
|
|
using NzbDrone.Core.Repository.Quality;
|
|
|
|
|
using Rss;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class RssSyncProvider : IRssSyncProvider
|
|
|
|
|
{
|
|
|
|
|
//Sync with RSS feeds to download files as needed
|
|
|
|
|
|
|
|
|
|
private Thread _rssSyncThread;
|
|
|
|
|
private IIndexerProvider _indexerProvider;
|
|
|
|
|
private IRssProvider _rss;
|
|
|
|
|
private ISeriesProvider _series;
|
|
|
|
|
private ISeasonProvider _season;
|
|
|
|
|
private IEpisodeProvider _episode;
|
|
|
|
|
private IHistoryProvider _history;
|
|
|
|
|
private IDownloadProvider _sab;
|
2011-01-31 02:30:59 +01:00
|
|
|
|
private IConfigProvider _configProvider;
|
2011-03-18 16:39:19 +01:00
|
|
|
|
private IRssItemProcessingProvider _rssItemProcessor;
|
2011-01-31 02:30:59 +01:00
|
|
|
|
private readonly INotificationProvider _notificationProvider;
|
2011-01-29 07:10:22 +01:00
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-01-31 02:30:59 +01:00
|
|
|
|
private ProgressNotification _rssSyncNotification;
|
|
|
|
|
|
2011-03-18 16:39:19 +01:00
|
|
|
|
public RssSyncProvider(IIndexerProvider indexerProvider, IRssProvider rss,
|
|
|
|
|
ISeriesProvider series, ISeasonProvider season,
|
|
|
|
|
IEpisodeProvider episode, IHistoryProvider history,
|
|
|
|
|
IDownloadProvider sab, INotificationProvider notificationProvider,
|
|
|
|
|
IConfigProvider configProvider, IRssItemProcessingProvider rssItemProcessor)
|
2011-01-29 07:10:22 +01:00
|
|
|
|
{
|
|
|
|
|
_indexerProvider = indexerProvider;
|
|
|
|
|
_rss = rss;
|
|
|
|
|
_series = series;
|
|
|
|
|
_season = season;
|
|
|
|
|
_episode = episode;
|
|
|
|
|
_history = history;
|
|
|
|
|
_sab = sab;
|
2011-01-31 02:30:59 +01:00
|
|
|
|
_notificationProvider = notificationProvider;
|
|
|
|
|
_configProvider = configProvider;
|
2011-03-18 16:39:19 +01:00
|
|
|
|
_rssItemProcessor = rssItemProcessor;
|
2011-01-29 07:10:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IRssSyncProvider Members
|
|
|
|
|
|
|
|
|
|
public void Begin()
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("RSS Sync Starting");
|
|
|
|
|
if (_rssSyncThread == null || !_rssSyncThread.IsAlive)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Initializing background sync of RSS Feeds.");
|
|
|
|
|
_rssSyncThread = new Thread(SyncWithRss)
|
|
|
|
|
{
|
2011-02-22 07:22:40 +01:00
|
|
|
|
Name = "RssSync",
|
2011-01-29 07:10:22 +01:00
|
|
|
|
Priority = ThreadPriority.Lowest
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_rssSyncThread.Start();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("RSS Sync already in progress. Ignoring request.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private void SyncWithRss()
|
|
|
|
|
{
|
|
|
|
|
//Get all enabled RSS providers
|
|
|
|
|
//Download Feeds
|
|
|
|
|
|
|
|
|
|
var indexers = _indexerProvider.EnabledIndexers();
|
|
|
|
|
|
2011-01-31 02:30:59 +01:00
|
|
|
|
using (_rssSyncNotification = new ProgressNotification("RSS Sync"))
|
2011-01-29 07:10:22 +01:00
|
|
|
|
{
|
2011-01-31 02:30:59 +01:00
|
|
|
|
_notificationProvider.Register(_rssSyncNotification);
|
|
|
|
|
_rssSyncNotification.CurrentStatus = "Starting Scan";
|
|
|
|
|
_rssSyncNotification.ProgressMax = indexers.Count();
|
2011-01-29 07:10:22 +01:00
|
|
|
|
|
2011-01-31 02:30:59 +01:00
|
|
|
|
foreach (var i in indexers)
|
2011-01-29 07:10:22 +01:00
|
|
|
|
{
|
2011-01-31 02:30:59 +01:00
|
|
|
|
Logger.Info("Starting RSS Sync for: {0}", i.IndexerName);
|
|
|
|
|
//Need to insert the users information in the the URL before trying to use it
|
2011-02-16 08:58:39 +01:00
|
|
|
|
GetUsersUrl(i); //Get the new users specific url (with their information) to use for the Sync
|
2011-01-31 02:30:59 +01:00
|
|
|
|
|
2011-02-16 08:58:39 +01:00
|
|
|
|
//If the url still contains '{' & '}' the user probably hasn't configured the indexer settings
|
|
|
|
|
if (i.RssUrl.Contains("{") && i.RssUrl.Contains("}"))
|
2011-01-31 02:30:59 +01:00
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Unable to Sync {0}. User Information has not been configured.", i.IndexerName);
|
|
|
|
|
continue; //Skip this indexer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_rssSyncNotification.CurrentStatus = String.Format("Syncing with RSS Feed: {0}", i.IndexerName);
|
|
|
|
|
|
|
|
|
|
var indexer = new FeedInfoModel(i.IndexerName, i.RssUrl);
|
|
|
|
|
|
2011-03-03 03:12:33 +01:00
|
|
|
|
var feedItems = _rss.GetFeed(indexer);
|
|
|
|
|
|
|
|
|
|
if (feedItems.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
_rssSyncNotification.CurrentStatus = String.Format("Failed to download RSS Feed: {0}", //
|
|
|
|
|
i.IndexerName);
|
|
|
|
|
continue; //No need to process anything else
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (RssItem item in feedItems)
|
2011-01-31 02:30:59 +01:00
|
|
|
|
{
|
|
|
|
|
NzbInfoModel nzb = Parser.ParseNzbInfo(indexer, item);
|
2011-03-22 04:51:03 +01:00
|
|
|
|
_rssItemProcessor.DownloadIfWanted(nzb, i);
|
2011-01-31 02:30:59 +01:00
|
|
|
|
}
|
2011-01-29 07:10:22 +01:00
|
|
|
|
}
|
2011-01-31 02:30:59 +01:00
|
|
|
|
_rssSyncNotification.CurrentStatus = "RSS Sync Completed";
|
|
|
|
|
Logger.Info("RSS Sync has successfully completed.");
|
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
|
_rssSyncNotification.Status = ProgressNotificationStatus.Completed;
|
2011-01-29 07:10:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-16 08:58:39 +01:00
|
|
|
|
private void GetUsersUrl(Indexer indexer)
|
2011-01-31 02:30:59 +01:00
|
|
|
|
{
|
|
|
|
|
if (indexer.IndexerName == "NzbMatrix")
|
|
|
|
|
{
|
|
|
|
|
var nzbMatrixUsername = _configProvider.GetValue("NzbMatrixUsername", String.Empty, false);
|
|
|
|
|
var nzbMatrixApiKey = _configProvider.GetValue("NzbMatrixApiKey", String.Empty, false);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(nzbMatrixUsername) && !String.IsNullOrEmpty(nzbMatrixApiKey))
|
2011-02-16 08:58:39 +01:00
|
|
|
|
indexer.RssUrl = indexer.RssUrl.Replace("{USERNAME}", nzbMatrixUsername).Replace("{APIKEY}", nzbMatrixApiKey);
|
2011-01-31 02:30:59 +01:00
|
|
|
|
|
|
|
|
|
//Todo: Perform validation at the config level so a user is unable to enable a provider until user details are provided
|
2011-02-16 08:58:39 +01:00
|
|
|
|
return;
|
2011-01-31 02:30:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (indexer.IndexerName == "NzbsOrg")
|
|
|
|
|
{
|
|
|
|
|
var nzbsOrgUId = _configProvider.GetValue("NzbsOrgUId", String.Empty, false);
|
|
|
|
|
var nzbsOrgHash = _configProvider.GetValue("NzbsOrgHash", String.Empty, false);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(nzbsOrgUId) && !String.IsNullOrEmpty(nzbsOrgHash))
|
2011-02-16 08:58:39 +01:00
|
|
|
|
indexer.RssUrl = indexer.RssUrl.Replace("{UID}", nzbsOrgUId).Replace("{HASH}", nzbsOrgHash);
|
2011-01-31 02:30:59 +01:00
|
|
|
|
|
|
|
|
|
//Todo: Perform validation at the config level so a user is unable to enable a provider until user details are provided
|
2011-02-16 08:58:39 +01:00
|
|
|
|
return;
|
2011-01-31 02:30:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (indexer.IndexerName == "NzbsOrg")
|
|
|
|
|
{
|
|
|
|
|
var nzbsrusUId = _configProvider.GetValue("NzbsrusUId", String.Empty, false);
|
|
|
|
|
var nzbsrusHash = _configProvider.GetValue("NzbsrusHash", String.Empty, false);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(nzbsrusUId) && !String.IsNullOrEmpty(nzbsrusHash))
|
2011-02-16 08:58:39 +01:00
|
|
|
|
indexer.RssUrl = indexer.RssUrl.Replace("{UID}", nzbsrusUId).Replace("{HASH}", nzbsrusHash);
|
2011-01-31 02:30:59 +01:00
|
|
|
|
|
|
|
|
|
//Todo: Perform validation at the config level so a user is unable to enable a provider until user details are provided
|
2011-02-16 08:58:39 +01:00
|
|
|
|
return;
|
2011-01-31 02:30:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-16 08:58:39 +01:00
|
|
|
|
return; //Currently other providers do not require user information to be substituted, simply return
|
2011-01-31 02:30:59 +01:00
|
|
|
|
}
|
2011-01-29 07:10:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|