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

143 lines
4.8 KiB
C#
Raw Normal View History

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.Disk;
using NzbDrone.Common.Extensions;
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.Model;
2019-12-22 22:08:53 +00:00
using NzbDrone.Core.RemotePathMappings;
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
{
2014-09-11 23:49:41 +00:00
private readonly IHttpClient _httpClient;
2012-08-30 00:20:48 +00:00
2014-09-11 23:49:41 +00:00
public Pneumatic(IHttpClient httpClient,
IConfigService configService,
INamingConfigService namingConfigService,
IDiskProvider diskProvider,
IRemotePathMappingService remotePathMappingService,
Logger logger)
: base(configService, namingConfigService, diskProvider, remotePathMappingService, logger)
2012-08-30 00:20:48 +00:00
{
2014-09-11 23:49:41 +00:00
_httpClient = httpClient;
2012-08-30 00:20:48 +00:00
}
2016-12-09 06:54:15 +00:00
public override string Name => "Pneumatic";
2015-04-25 16:22:53 +00:00
2016-12-09 06:54:15 +00:00
public override DownloadProtocol Protocol => DownloadProtocol.Usenet;
2017-02-13 01:07:57 +00:00
public override string Download(RemoteMovie remoteMovie)
{
2017-02-13 01:07:57 +00:00
var url = remoteMovie.Release.DownloadUrl;
var title = remoteMovie.Release.Title;
// We don't have full seasons in movies.
2022-11-20 18:27:45 +00:00
// if (remoteMovie.ParsedEpisodeInfo.FullSeason)
// {
// throw new NotSupportedException("Full season releases are not supported with Pneumatic.");
2022-11-20 18:27:45 +00:00
// }
title = FileNameBuilder.CleanFileName(title);
2022-11-20 18:27:45 +00:00
// Save to the Pneumatic directory (The user will need to ensure its accessible by XBMC)
var nzbFile = Path.Combine(Settings.NzbFolder, title + ".nzb");
_logger.Debug("Downloading NZB from: {0} to: {1}", url, nzbFile);
_httpClient.DownloadFile(url, nzbFile);
_logger.Debug("NZB Download succeeded, saved to: {0}", nzbFile);
var strmFile = WriteStrmFile(title, nzbFile);
2015-06-15 04:08:30 +00:00
return GetDownloadClientId(strmFile);
2012-08-30 00:20:48 +00:00
}
2016-12-09 06:54:15 +00:00
public bool IsConfigured => !string.IsNullOrWhiteSpace(Settings.NzbFolder);
public override IEnumerable<DownloadClientItem> GetItems()
{
foreach (var file in _diskProvider.GetFiles(Settings.StrmFolder, SearchOption.TopDirectoryOnly))
{
if (Path.GetExtension(file) != ".strm")
{
continue;
}
var title = FileNameBuilder.CleanFileName(Path.GetFileName(file));
var historyItem = new DownloadClientItem
{
DownloadClientInfo = DownloadClientItemClientInfo.FromDownloadClient(this),
DownloadId = GetDownloadClientId(file),
Title = title,
CanBeRemoved = true,
CanMoveFiles = true,
TotalSize = _diskProvider.GetFileSize(file),
OutputPath = new OsPath(file)
};
if (_diskProvider.IsFileLocked(file))
{
historyItem.Status = DownloadItemStatus.Downloading;
}
else
{
historyItem.Status = DownloadItemStatus.Completed;
}
yield return historyItem;
}
}
public override void RemoveItem(DownloadClientItem item, bool deleteData)
{
throw new NotSupportedException();
}
public override DownloadClientInfo GetStatus()
{
var status = new DownloadClientInfo
{
IsLocalhost = true
};
return status;
}
protected override void Test(List<ValidationFailure> failures)
2014-02-26 05:40:47 +00:00
{
failures.AddIfNotNull(TestFolder(Settings.NzbFolder, "NzbFolder"));
failures.AddIfNotNull(TestFolder(Settings.StrmFolder, "StrmFolder"));
2014-02-26 05:40:47 +00:00
}
private string WriteStrmFile(string title, string nzbFile)
{
if (Settings.StrmFolder.IsNullOrWhiteSpace())
{
2018-11-23 07:03:32 +00:00
throw new DownloadClientException("Strm Folder needs to be set for Pneumatic Downloader");
}
var contents = string.Format("plugin://plugin.program.pneumatic/?mode=strm&type=add_file&nzb={0}&nzbname={1}", nzbFile, title);
2018-11-23 07:03:32 +00:00
var filename = Path.Combine(Settings.StrmFolder, title + ".strm");
_diskProvider.WriteAllText(filename, contents);
return filename;
}
private string GetDownloadClientId(string filename)
{
return Definition.Name + "_" + Path.GetFileName(filename) + "_" + _diskProvider.FileGetLastWrite(filename).Ticks;
}
2012-08-30 00:20:48 +00:00
}
}