HttpProvider - Added Download File.

SabProvider - Added AddById (Newzbin)
Fixes to RssItemProcessingProvider
Can either download NZB to file or send to SAB...
This commit is contained in:
Mark McDowall 2011-03-21 20:51:03 -07:00
parent 84f0dfed4e
commit 4f2f5a3d71
11 changed files with 240 additions and 75 deletions

View File

@ -39,5 +39,40 @@ namespace NzbDrone.Core.Providers
return String.Empty;
}
public bool DownloadFile(string request, string filename)
{
try
{
var webClient = new WebClient();
webClient.DownloadFile(request, filename);
return true;
}
catch (Exception ex)
{
Logger.Warn("Failed to get response from: {0}", request);
Logger.TraceException(ex.Message, ex);
}
return false;
}
public bool DownloadFile(string request, string filename, string username, string password)
{
try
{
var webClient = new WebClient();
webClient.Credentials = new NetworkCredential(username, password);
webClient.DownloadFile(request, filename);
return true;
}
catch (Exception ex)
{
Logger.Warn("Failed to get response from: {0}", request);
Logger.TraceException(ex.Message, ex);
}
return false;
}
}
}

View File

@ -4,5 +4,6 @@
{
bool AddByUrl(string url, string title); //Should accept something other than string (NzbInfo?) returns success or failure
bool IsInQueue(string title); //Should accept something other than string (Episode?) returns bool
bool AddById(string id, string title);
}
}

View File

@ -4,5 +4,7 @@
{
string DownloadString(string request);
string DownloadString(string request, string username, string password);
bool DownloadFile(string request, string filename);
bool DownloadFile(string request, string filename, string username, string password);
}
}

View File

@ -11,7 +11,7 @@ namespace NzbDrone.Core.Providers
{
//This interface will contain methods to process individual RSS Feed Items (Queue if wanted)
void QueueIfWanted(NzbInfoModel nzb, Indexer indexer);
bool DownloadIfWanted(NzbInfoModel nzb, Indexer indexer);
string GetTitleFix(List<EpisodeParseResult> episodes, int seriesId);
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NLog;
@ -17,12 +18,15 @@ namespace NzbDrone.Core.Providers
private IHistoryProvider _historyProvider;
private IDownloadProvider _sabProvider;
private IConfigProvider _configProvider;
private IHttpProvider _httpProvider;
private IDiskProvider _diskProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public RssItemProcessingProvider(ISeriesProvider seriesProvider, ISeasonProvider seasonProvider,
IEpisodeProvider episodeProvider, IHistoryProvider historyProvider,
IDownloadProvider sabProvider, IConfigProvider configProvider)
IDownloadProvider sabProvider, IConfigProvider configProvider,
IHttpProvider httpProvider, IDiskProvider diskProvider)
{
_seriesProvider = seriesProvider;
_seasonProvider = seasonProvider;
@ -30,11 +34,13 @@ namespace NzbDrone.Core.Providers
_historyProvider = historyProvider;
_sabProvider = sabProvider;
_configProvider = configProvider;
_httpProvider = httpProvider;
_diskProvider = diskProvider;
}
#region IRssItemProcessingProvider Members
public void QueueIfWanted(NzbInfoModel nzb, Indexer indexer)
public bool DownloadIfWanted(NzbInfoModel nzb, Indexer indexer)
{
//Do we want this item?
try
@ -42,7 +48,7 @@ namespace NzbDrone.Core.Providers
if (nzb.IsPassworded())
{
Logger.Debug("Skipping Passworded Report {0}", nzb.Title);
return;
return false;
}
var episodeParseResults = Parser.ParseEpisodeInfo(nzb.Title);
@ -50,15 +56,15 @@ namespace NzbDrone.Core.Providers
if (episodeParseResults.Count() > 1)
{
ProcessStandardItem(nzb, indexer, episodeParseResults);
return;
return false;
}
//Try to handle Season X style naming
//Todo: Try to handle Season X style naming
if (episodeParseResults.Count() < 1)
{
Logger.Debug("Unsupported Title: {0}", nzb.Title);
return;
return false;
}
}
@ -66,6 +72,7 @@ namespace NzbDrone.Core.Providers
{
Logger.ErrorException("Error Parsing NZB: " + ex.Message, ex);
}
return false;
}
public string GetTitleFix(List<EpisodeParseResult> episodes, int seriesId)
@ -142,18 +149,41 @@ namespace NzbDrone.Core.Providers
var titleFix = GetTitleFix(new List<EpisodeParseResult> { episode }, episodeModel.SeriesId);
titleFix = String.Format("{0} [{1}]", titleFix, nzb.Quality); //Add Quality to the titleFix
if (Convert.ToBoolean(_configProvider.GetValue("UseBlackhole", true, true)))
if (_sabProvider.IsInQueue(titleFix))
return;
}
//If their is more than one episode in this NZB check to see if it has been added as a single NZB
//Do we want to download the NZB Directly or Send to SABnzbd?
if (Convert.ToBoolean(_configProvider.GetValue("UseBlackHole", true, true)))
{
var path = _configProvider.GetValue("BlackholeDirectory", String.Empty, true);
if (String.IsNullOrEmpty(path))
{
//Use the NZBDrone root Directory + /NZBs
path = CentralDispatch.AppPath + Path.DirectorySeparatorChar + "NZBs";
}
if (_diskProvider.FolderExists(path))
_httpProvider.DownloadFile(nzb.Link.ToString(), path);
else
Logger.Error("Blackhole Directory doesn't exist, not saving NZB: '{0}'", path);
}
//Send it to SABnzbd
else
{
if (episodeParseResults.Count > 1)
{
if (_sabProvider.IsInQueue(nzb.TitleFix))
return;
}
//Only add to history if it was added to properly sent to SABnzbd
if (indexer.IndexerName != "Newzbin")
AddByUrl(episodeParseResults, series, nzb, indexer);
@ -163,6 +193,8 @@ namespace NzbDrone.Core.Providers
}
}
}
private void AddByUrl(List<EpisodeParseResult> episodeParseResults, Series series, NzbInfoModel nzb, Indexer indexer)
{
if (_sabProvider.AddByUrl(nzb.Link.ToString(), nzb.TitleFix))

View File

@ -118,7 +118,7 @@ namespace NzbDrone.Core.Providers
foreach (RssItem item in feedItems)
{
NzbInfoModel nzb = Parser.ParseNzbInfo(indexer, item);
_rssItemProcessor.QueueIfWanted(nzb, i);
_rssItemProcessor.DownloadIfWanted(nzb, i);
}
}
_rssSyncNotification.CurrentStatus = "RSS Sync Completed";

View File

@ -24,9 +24,9 @@ namespace NzbDrone.Core.Providers
public bool AddByUrl(string url, string title)
{
const string mode = "addurl";
//string cat = _config.GetValue("SabTvCategory", String.Empty, true);
string cat = "tv";
string priority = _config.GetValue("SabPriority", String.Empty, false);
string cat = _config.GetValue("SabTvCategory", String.Empty, true);
//string cat = "tv";
string priority = _config.GetValue("SabTvPriority", String.Empty, false);
string name = url.Replace("&", "%26");
string nzbName = HttpUtility.UrlEncode(title);
@ -70,6 +70,32 @@ namespace NzbDrone.Core.Providers
return false; //Not in Queue
}
public bool AddById(string id, string title)
{
//mode=addid&name=333333&pp=3&script=customscript.cmd&cat=Example&priority=-1
const string mode = "addid";
string cat = _config.GetValue("SabTvCategory", String.Empty, true);
//string cat = "tv";
string priority = _config.GetValue("SabTvPriority", String.Empty, false);
string nzbName = HttpUtility.UrlEncode(title);
string action = string.Format("mode={0}&name={1}&priority={2}&cat={3}&nzbname={4}", mode, id, priority, cat, nzbName);
string request = GetSabRequest(action);
Logger.Debug("Adding report [{0}] to the queue.", nzbName);
string response = _http.DownloadString(request).Replace("\n", String.Empty);
Logger.Debug("Queue Repsonse: [{0}]", response);
if (response == "ok")
return true;
return false;
throw new NotImplementedException();
}
#endregion
private string GetSabRequest(string action)

View File

@ -87,7 +87,9 @@ namespace NzbDrone.Web.Controllers
SabUsername = _configProvider.GetValue("SabUsername", String.Empty, true),
SabPassword = _configProvider.GetValue("SabPassword", String.Empty, true),
SabTvCategory = _configProvider.GetValue("SabTvCategory", String.Empty, true),
SabPriority = (SabnzbdPriorityType)Enum.Parse(typeof(SabnzbdPriorityType), _configProvider.GetValue("SabPriority", "Normal", true)),
SabTvPriority = (SabnzbdPriorityType)Enum.Parse(typeof(SabnzbdPriorityType), _configProvider.GetValue("SabTvPriority", "Normal", true)),
UseBlackHole = Convert.ToBoolean(_configProvider.GetValue("UseBlackHole", true, true)),
BlackholeDirectory = _configProvider.GetValue("BlackholeDirectory", String.Empty, true)
};
return View("Index", model);
@ -275,7 +277,9 @@ namespace NzbDrone.Web.Controllers
_configProvider.SetValue("SabUsername", data.SabUsername);
_configProvider.SetValue("SabPassword", data.SabPassword);
_configProvider.SetValue("SabTvCategory", data.SabTvCategory);
_configProvider.SetValue("SabPriority", data.SabPriority.ToString());
_configProvider.SetValue("SabTvPriority", data.SabTvPriority.ToString());
_configProvider.SetValue("UseBlackhole", data.UseBlackHole.ToString());
_configProvider.SetValue("BlackholeDirectory", data.BlackholeDirectory);
return Content(_settingsSaved);
}

View File

@ -93,7 +93,23 @@ namespace NzbDrone.Web.Models
[Required(ErrorMessage = "Please select a valid priority")]
[DisplayName("SABnzbd Priority")]
public SabnzbdPriorityType SabPriority
public SabnzbdPriorityType SabTvPriority
{
get;
set;
}
[DisplayName("Use Blackhole")]
public bool UseBlackHole
{
get;
set;
}
[DataType(DataType.Text)]
[DisplayFormat(ConvertEmptyStringToNull = false)]
[DisplayName("Blackhole Directory")]
public String BlackholeDirectory
{
get;
set;

View File

@ -78,6 +78,7 @@
<ItemGroup>
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\ApiController.cs" />
<Compile Include="Controllers\HistoryController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\LogController.cs" />
<Compile Include="Controllers\NotificationController.cs" />
@ -276,6 +277,7 @@
<Content Include="Scripts\jquery-tgc-countdown-1.0.js" />
<Content Include="Scripts\jquery.simpledropdown.js" />
<Content Include="Scripts\Notification.js" />
<Content Include="Views\History\Index.aspx" />
<Content Include="Views\Home\Test.aspx" />
<Content Include="Views\Log\Index.aspx" />
<Content Include="Views\Series\AddExisting.aspx" />

View File

@ -10,9 +10,24 @@
resetForm: false
};
$('#form').ajaxForm(options);
selectDownloadOption(); //Load either SAB or Blackhole div
$('#save_button').attr('disabled', '');
});
function selectDownloadOption() {
var selected = $("input[name='UseBlackHole']:checked").val();
if (selected == "True") {
document.getElementById('blackhole').style.display = 'block';
document.getElementById('sab').style.display = 'none';
}
else {
document.getElementById('sab').style.display = 'block';
document.getElementById('blackhole').style.display = 'none';
}
}
function showRequest(formData, jqForm, options) {
$("#result").empty().html('Saving...');
$("#form :input").attr("disabled", true);
@ -22,6 +37,10 @@
$("#result").empty().html(responseText);
$("#form :input").attr("disabled", false);
}
$(".blackhole_radio").live("change", function () {
selectDownloadOption(); //Load either SAB or Blackhole div
});
</script>
<% Html.EnableClientValidation(); %>
@ -72,6 +91,19 @@
</div>
</fieldset>
<div>
<div>
<b><%= Html.LabelFor(m => m.UseBlackHole) %></b>
</div>
<div>
<%= Html.RadioButtonFor(m => m.UseBlackHole, true, new { @class = "blackhole_radio" }) %>Blackhole
</div>
<div>
<%= Html.RadioButtonFor(m => m.UseBlackHole, false, new { @class = "blackhole_radio" })%>SABnzbd
</div>
</div>
<div id="sab" style="display:none">
<fieldset class="sub-field">
<legend>SABnzbd</legend>
@ -125,12 +157,27 @@
<div class="config-section">
<div class="config-group">
<div class="config-title"><%= Html.LabelFor(m => m.SabPriority) %></div>
<div class="config-value"><%= Html.DropDownListFor(m => m.SabPriority, Model.PrioritySelectList) %></div>
<div class="config-title"><%= Html.LabelFor(m => m.SabTvPriority) %></div>
<div class="config-value"><%= Html.DropDownListFor(m => m.SabTvPriority, Model.PrioritySelectList)%></div>
</div>
<div class="config-validation"><%= Html.ValidationMessageFor(m => m.SabTvCategory)%></div>
<div class="config-validation"><%= Html.ValidationMessageFor(m => m.SabTvPriority)%></div>
</div>
</fieldset>
</div>
<div id="blackhole" style="display:none">
<fieldset class="sub-field">
<legend>Blackhole</legend>
<div class="config-section">
<div class="config-group">
<div class="config-title"><%= Html.LabelFor(m => m.BlackholeDirectory) %></div>
<div class="config-value"><%= Html.TextBoxFor(m => m.BlackholeDirectory)%></div>
</div>
<div class="config-validation"><%= Html.ValidationMessageFor(m => m.BlackholeDirectory)%></div>
</div>
</fieldset>
</div>
<input type="submit" id="save_button" value="Save" disabled="disabled" />