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

142 lines
4.7 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;
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,
IDiskProvider diskProvider,
IRemotePathMappingService remotePathMappingService,
Logger logger)
2014-10-13 21:11:35 +00:00
: base(configService, 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;
public override string Download(RemoteAlbum remoteAlbum)
2012-08-30 00:20:48 +00:00
{
var url = remoteAlbum.Release.DownloadUrl;
var title = remoteAlbum.Release.Title;
if (remoteAlbum.ParsedAlbumInfo.Discography)
{
throw new NotSupportedException("Discography releases are not supported with Pneumatic.");
}
title = FileNameBuilder.CleanFileName(title);
2012-08-30 00:20:48 +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");
2012-08-30 00:20:48 +00:00
_logger.Debug("Downloading NZB from: {0} to: {1}", url, nzbFile);
2014-09-11 23:49:41 +00:00
_httpClient.DownloadFile(url, nzbFile);
2012-08-30 00:20:48 +00:00
_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;
}
}
2021-12-24 22:25:17 +00:00
public override void RemoveItem(DownloadClientItem item, bool deleteData)
{
throw new NotSupportedException();
}
2017-10-08 03:54:13 +00:00
public override DownloadClientInfo GetStatus()
{
2017-10-08 03:54:13 +00:00
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())
{
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);
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
}
}