2011-01-29 06:10:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Model;
|
2011-01-31 01:30:59 +00:00
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-01-29 06:10:22 +00: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 01:30:59 +00:00
|
|
|
|
private IConfigProvider _configProvider;
|
|
|
|
|
private readonly INotificationProvider _notificationProvider;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-01-31 01:30:59 +00:00
|
|
|
|
private ProgressNotification _rssSyncNotification;
|
|
|
|
|
|
2011-01-29 06:10:22 +00:00
|
|
|
|
public RssSyncProvider(IIndexerProvider indexerProvider, IRssProvider rss, ISeriesProvider series,
|
2011-01-31 01:30:59 +00:00
|
|
|
|
ISeasonProvider season, IEpisodeProvider episode, IHistoryProvider history, IDownloadProvider sab, INotificationProvider notificationProvider, IConfigProvider configProvider)
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
|
|
|
|
_indexerProvider = indexerProvider;
|
|
|
|
|
_rss = rss;
|
|
|
|
|
_series = series;
|
|
|
|
|
_season = season;
|
|
|
|
|
_episode = episode;
|
|
|
|
|
_history = history;
|
|
|
|
|
_sab = sab;
|
2011-01-31 01:30:59 +00:00
|
|
|
|
_notificationProvider = notificationProvider;
|
|
|
|
|
_configProvider = configProvider;
|
2011-01-29 06:10:22 +00: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)
|
|
|
|
|
{
|
|
|
|
|
Name = "SyncUnmappedFolders",
|
|
|
|
|
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 01:30:59 +00:00
|
|
|
|
using (_rssSyncNotification = new ProgressNotification("RSS Sync"))
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-01-31 01:30:59 +00:00
|
|
|
|
_notificationProvider.Register(_rssSyncNotification);
|
|
|
|
|
_rssSyncNotification.CurrentStatus = "Starting Scan";
|
|
|
|
|
_rssSyncNotification.ProgressMax = indexers.Count();
|
2011-01-29 06:10:22 +00:00
|
|
|
|
|
2011-01-31 01:30:59 +00:00
|
|
|
|
foreach (var i in indexers)
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-01-31 01:30:59 +00: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 07:58:39 +00:00
|
|
|
|
GetUsersUrl(i); //Get the new users specific url (with their information) to use for the Sync
|
2011-01-31 01:30:59 +00:00
|
|
|
|
|
2011-02-16 07:58:39 +00: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 01:30:59 +00: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);
|
|
|
|
|
|
|
|
|
|
foreach (RssItem item in _rss.GetFeed(indexer))
|
|
|
|
|
{
|
|
|
|
|
NzbInfoModel nzb = Parser.ParseNzbInfo(indexer, item);
|
2011-02-18 16:36:50 +00:00
|
|
|
|
QueueIfWanted(nzb, i);
|
2011-01-31 01:30:59 +00:00
|
|
|
|
}
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-01-31 01:30:59 +00:00
|
|
|
|
_rssSyncNotification.CurrentStatus = "RSS Sync Completed";
|
|
|
|
|
Logger.Info("RSS Sync has successfully completed.");
|
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
|
_rssSyncNotification.Status = ProgressNotificationStatus.Completed;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 16:36:50 +00:00
|
|
|
|
private void QueueIfWanted(NzbInfoModel nzb, Indexer indexer)
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-01-31 01:30:59 +00:00
|
|
|
|
//Do we want this item?
|
2011-01-29 06:10:22 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (nzb.IsPassworded())
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Skipping Passworded Report {0}", nzb.Title);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var episodeParseResults = Parser.ParseEpisodeInfo(nzb.Title);
|
|
|
|
|
|
|
|
|
|
if (episodeParseResults.Count() < 1)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Unsupported Title: {0}", nzb.Title);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-17 17:45:02 +00:00
|
|
|
|
//Todo: How to determine if we want the show if the FeedTitle is drastically different from the TitleOnDisk (CSI is one that comes to mind)
|
|
|
|
|
var series = _series.FindSeries(episodeParseResults[0].SeriesTitle);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-02-17 17:45:02 +00:00
|
|
|
|
Logger.Debug("Show is not being watched: {0}", episodeParseResults[0].SeriesTitle);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-01-29 06:10:22 +00:00
|
|
|
|
|
2011-02-17 17:45:02 +00:00
|
|
|
|
Logger.Debug("Show is being watched: {0}", series.Title);
|
|
|
|
|
|
|
|
|
|
nzb.TitleFix = GetTitleFix(episodeParseResults, series.SeriesId); //Get the TitleFix so we can use it later
|
|
|
|
|
nzb.Proper = Parser.ParseProper(nzb.Title);
|
2011-02-18 16:36:50 +00:00
|
|
|
|
nzb.Quality = Parser.ParseQuality(nzb.Title);
|
2011-02-17 17:45:02 +00:00
|
|
|
|
|
|
|
|
|
//Loop through the list of the episodeParseResults to ensure that all the episodes are needed)
|
|
|
|
|
foreach (var episode in episodeParseResults)
|
|
|
|
|
{
|
|
|
|
|
//IsEpisodeWanted?
|
|
|
|
|
var episodeModel = new EpisodeModel();
|
|
|
|
|
episodeModel.Proper = nzb.Proper;
|
|
|
|
|
episodeModel.SeriesId = series.SeriesId;
|
|
|
|
|
episodeModel.SeriesTitle = series.Title;
|
2011-02-18 16:36:50 +00:00
|
|
|
|
episodeModel.Quality = nzb.Quality;
|
2011-02-17 17:45:02 +00:00
|
|
|
|
episodeModel.SeasonNumber = episode.SeasonNumber;
|
|
|
|
|
episodeModel.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
|
|
|
|
|
|
if (!_episode.IsNeeded(episodeModel))
|
2011-01-29 06:10:22 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2011-02-17 17:45:02 +00:00
|
|
|
|
var titleFix = GetTitleFix(new List<EpisodeParseResult> { episode }, episodeModel.SeriesId);
|
2011-01-29 06:10:22 +00:00
|
|
|
|
|
2011-02-17 17:45:02 +00:00
|
|
|
|
if (_sab.IsInQueue(titleFix))
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-01-29 06:10:22 +00:00
|
|
|
|
|
2011-02-17 17:45:02 +00:00
|
|
|
|
//If their is more than one episode in this NZB check to see if it has been added as a single NZB
|
|
|
|
|
if (episodeParseResults.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
if (_sab.IsInQueue(nzb.TitleFix))
|
|
|
|
|
return;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-02-17 17:45:02 +00:00
|
|
|
|
|
2011-02-18 16:36:50 +00:00
|
|
|
|
//Only add to history if it was added to properly sent to SABnzbd
|
|
|
|
|
if (_sab.AddByUrl(nzb.Link.ToString(), nzb.TitleFix))
|
|
|
|
|
{
|
|
|
|
|
//We need to loop through the episodeParseResults so each episode in the NZB is properly handled
|
|
|
|
|
foreach (var epr in episodeParseResults)
|
|
|
|
|
{
|
|
|
|
|
var episode = _episode.GetEpisode(series.SeriesId, epr.SeasonNumber, epr.EpisodeNumber);
|
|
|
|
|
|
|
|
|
|
if (episode == null)
|
|
|
|
|
{
|
|
|
|
|
//Not sure how we got this far, so lets throw an exception
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Set episode status to grabbed
|
|
|
|
|
episode.Status = EpisodeStatusType.Grabbed;
|
|
|
|
|
|
|
|
|
|
//Add to History
|
|
|
|
|
var history = new History();
|
|
|
|
|
history.Date = DateTime.Now;
|
|
|
|
|
history.EpisodeId = episode.EpisodeId;
|
|
|
|
|
history.IndexerName = indexer.IndexerName;
|
|
|
|
|
history.IsProper = nzb.Proper;
|
|
|
|
|
history.Quality = nzb.Quality;
|
2011-02-22 01:05:56 +00:00
|
|
|
|
history.NzbTitle = nzb.Title;
|
2011-02-18 16:36:50 +00:00
|
|
|
|
|
|
|
|
|
_history.Insert(history);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error Parsing NZB: " + ex.Message, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetTitleFix(List<EpisodeParseResult> episodes, int seriesId)
|
|
|
|
|
{
|
|
|
|
|
var series = _series.GetSeries(seriesId);
|
|
|
|
|
|
|
|
|
|
int seasonNumber = 0;
|
|
|
|
|
string episodeNumbers = String.Empty;
|
|
|
|
|
string episodeTitles = String.Empty;
|
|
|
|
|
|
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
|
{
|
|
|
|
|
var episodeInDb = _episode.GetEpisode(seriesId, episode.SeasonNumber, episode.EpisodeNumber);
|
2011-01-31 07:42:44 +00:00
|
|
|
|
|
|
|
|
|
if (episodeInDb == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Episode Not found in Database");
|
2011-02-17 17:45:02 +00:00
|
|
|
|
return String.Format("{0} - {1:00}x{2}", series.Title, episode.SeasonNumber, episode.SeriesTitle);
|
2011-01-31 07:42:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-01-29 06:10:22 +00:00
|
|
|
|
seasonNumber = episodeInDb.SeasonNumber;
|
|
|
|
|
episodeNumbers = String.Format("{0}x{1:00}", episodeNumbers, episodeInDb.EpisodeNumber);
|
|
|
|
|
episodeTitles = String.Format("{0} + {1}", episodeTitles, episodeInDb.Title);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-17 17:45:02 +00:00
|
|
|
|
episodeTitles = episodeTitles.Trim(' ', '+');
|
|
|
|
|
|
2011-01-29 06:10:22 +00:00
|
|
|
|
return String.Format("{0} - {1}{2} - {3}", series.Title, seasonNumber, episodeNumbers, episodeTitles);
|
|
|
|
|
}
|
2011-01-31 01:30:59 +00:00
|
|
|
|
|
2011-02-16 07:58:39 +00:00
|
|
|
|
private void GetUsersUrl(Indexer indexer)
|
2011-01-31 01:30:59 +00: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 07:58:39 +00:00
|
|
|
|
indexer.RssUrl = indexer.RssUrl.Replace("{USERNAME}", nzbMatrixUsername).Replace("{APIKEY}", nzbMatrixApiKey);
|
2011-01-31 01:30:59 +00: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 07:58:39 +00:00
|
|
|
|
return;
|
2011-01-31 01:30:59 +00: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 07:58:39 +00:00
|
|
|
|
indexer.RssUrl = indexer.RssUrl.Replace("{UID}", nzbsOrgUId).Replace("{HASH}", nzbsOrgHash);
|
2011-01-31 01:30:59 +00: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 07:58:39 +00:00
|
|
|
|
return;
|
2011-01-31 01:30:59 +00: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 07:58:39 +00:00
|
|
|
|
indexer.RssUrl = indexer.RssUrl.Replace("{UID}", nzbsrusUId).Replace("{HASH}", nzbsrusHash);
|
2011-01-31 01:30:59 +00: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 07:58:39 +00:00
|
|
|
|
return;
|
2011-01-31 01:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-16 07:58:39 +00:00
|
|
|
|
return; //Currently other providers do not require user information to be substituted, simply return
|
2011-01-31 01:30:59 +00:00
|
|
|
|
}
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|