Lidarr/src/NzbDrone.Core/Download/Clients/Pneumatic/Pneumatic.cs

126 lines
3.8 KiB
C#
Raw Normal View History

2012-08-30 00:20:48 +00:00
using System;
using System.Collections.Generic;
2012-08-30 00:20:48 +00:00
using System.IO;
using FluentValidation.Results;
2012-08-30 00:20:48 +00:00
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Http;
2013-02-24 06:48:52 +00:00
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Organizer;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
2012-08-30 00:20:48 +00:00
namespace NzbDrone.Core.Download.Clients.Pneumatic
2012-08-30 00:20:48 +00:00
{
public class Pneumatic : DownloadClientBase<PneumaticSettings>
2012-08-30 00:20:48 +00:00
{
2013-04-10 23:41:45 +00:00
private readonly IHttpProvider _httpProvider;
2012-08-30 00:20:48 +00:00
public Pneumatic(IHttpProvider httpProvider,
IConfigService configService,
IDiskProvider diskProvider,
IParsingService parsingService,
Logger logger)
: base(configService, diskProvider, parsingService, logger)
2012-08-30 00:20:48 +00:00
{
_httpProvider = httpProvider;
}
public override DownloadProtocol Protocol
{
get
{
return DownloadProtocol.Usenet;
}
}
public override string Download(RemoteEpisode remoteEpisode)
2012-08-30 00:20:48 +00:00
{
2013-09-13 23:17:58 +00:00
var url = remoteEpisode.Release.DownloadUrl;
var title = remoteEpisode.Release.Title;
2013-08-17 23:27:18 +00:00
if (remoteEpisode.ParsedEpisodeInfo.FullSeason)
2012-08-30 00:20:48 +00:00
{
throw new NotSupportedException("Full season releases are not supported with Pneumatic.");
2013-08-17 23:27:18 +00:00
}
title = FileNameBuilder.CleanFileName(title);
2012-08-30 00:20:48 +00:00
2013-08-17 23:27:18 +00:00
//Save to the Pneumatic directory (The user will need to ensure its accessible by XBMC)
var filename = Path.Combine(Settings.NzbFolder, title + ".nzb");
2012-08-30 00:20:48 +00:00
_logger.Debug("Downloading NZB from: {0} to: {1}", url, filename);
2013-08-17 23:27:18 +00:00
_httpProvider.DownloadFile(url, filename);
2012-08-30 00:20:48 +00:00
_logger.Debug("NZB Download succeeded, saved to: {0}", filename);
2013-08-17 23:27:18 +00:00
var contents = String.Format("plugin://plugin.program.pneumatic/?mode=strm&type=add_file&nzb={0}&nzbname={1}", filename, title);
_diskProvider.WriteAllText(Path.Combine(_configService.DownloadedEpisodesFolder, title + ".strm"), contents);
return null;
2012-08-30 00:20:48 +00:00
}
public bool IsConfigured
{
get
{
return !string.IsNullOrWhiteSpace(Settings.NzbFolder);
}
}
public override IEnumerable<DownloadClientItem> GetItems()
{
return new DownloadClientItem[0];
}
public override void RemoveItem(String id)
{
throw new NotSupportedException();
}
public override String RetryDownload(String id)
{
throw new NotSupportedException();
}
public override DownloadClientStatus GetStatus()
{
var status = new DownloadClientStatus
{
IsLocalhost = true
};
return status;
}
protected override void Test(List<ValidationFailure> failures)
2014-02-26 05:40:47 +00:00
{
failures.AddIfNotNull(TestWrite(Settings.NzbFolder, "NzbFolder"));
2012-08-30 00:20:48 +00:00
}
2014-02-26 05:40:47 +00:00
private ValidationFailure TestWrite(String folder, String propertyName)
2014-02-26 05:40:47 +00:00
{
if (!_diskProvider.FolderExists(folder))
{
return new ValidationFailure(propertyName, "Folder does not exist");
}
try
{
var testPath = Path.Combine(folder, "drone_test.txt");
_diskProvider.WriteAllText(testPath, DateTime.Now.ToString());
_diskProvider.DeleteFile(testPath);
}
catch (Exception ex)
{
_logger.ErrorException(ex.Message, ex);
return new ValidationFailure(propertyName, "Unable to write to folder");
}
return null;
2014-02-26 05:40:47 +00:00
}
2012-08-30 00:20:48 +00:00
}
}