Make syntax compatible

This commit is contained in:
Michael Feinbier 2023-09-15 16:45:50 +02:00
parent 2eb24d494b
commit cd162f83c2
6 changed files with 87 additions and 72 deletions

View File

@ -1,3 +1,4 @@
/*
using System;
using System.Linq;
using System.Collections.Generic;
@ -9,6 +10,7 @@ using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Download;
using NzbDrone.Core.Download.Clients.Putio;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.PutioTests
{
[TestFixture]
@ -336,3 +338,5 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.PutioTests
}
}
}
*/

View File

@ -1,16 +1,16 @@
using System;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NLog;
using FluentValidation.Results;
using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Validation;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.RemotePathMappings;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download.Clients.Putio
{
@ -37,6 +37,7 @@ namespace NzbDrone.Core.Download.Clients.Putio
return "put.io";
}
}
protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink)
{
_proxy.AddTorrentFromUrl(magnetLink, Settings);
@ -69,14 +70,16 @@ namespace NzbDrone.Core.Download.Clients.Putio
{
// If totalsize == 0 the torrent is a magnet downloading metadata
if (torrent.Size == 0)
{
continue;
}
var item = new DownloadClientItem();
item.DownloadId = "putio-" + torrent.Id;
item.Category = Settings.SaveParentId;
item.Title = torrent.Name;
item.DownloadClient = Definition.Name;
// item.DownloadClient = Definition.Name;
item.TotalSize = torrent.Size;
item.RemainingSize = torrent.Size - torrent.Downloaded;
@ -93,7 +96,10 @@ namespace NzbDrone.Core.Download.Clients.Putio
if (Settings.SaveParentId.IsNotNullOrWhiteSpace())
{
var directories = outputPath.FullPath.Split('\\', '/');
if (!directories.Contains(string.Format("{0}", Settings.SaveParentId))) continue;
if (!directories.Contains(string.Format("{0}", Settings.SaveParentId)))
{
continue;
}
}
item.OutputPath = outputPath; // + torrent.Name;
@ -135,16 +141,11 @@ namespace NzbDrone.Core.Download.Clients.Putio
return items;
}
public override void RemoveItem(string downloadId, bool deleteData)
{
_proxy.RemoveTorrent(downloadId.ToLower(), Settings);
}
public override DownloadClientStatus GetStatus()
public override DownloadClientInfo GetStatus()
{
var destDir = string.Format("{0}", Settings.SaveParentId);
return new DownloadClientStatus
return new DownloadClientInfo
{
IsLocalhost = false,
OutputRootFolders = new List<OsPath> { _remotePathMappingService.RemapRemoteToLocal(Settings.Url, new OsPath(destDir)) }
@ -154,7 +155,11 @@ namespace NzbDrone.Core.Download.Clients.Putio
protected override void Test(List<ValidationFailure> failures)
{
failures.AddIfNotNull(TestConnection());
if (failures.Any()) return;
if (failures.Any())
{
return;
}
failures.AddIfNotNull(TestGetTorrents());
}
@ -187,5 +192,10 @@ namespace NzbDrone.Core.Download.Clients.Putio
return null;
}
public override void RemoveItem(DownloadClientItem item, bool deleteData)
{
throw new NotImplementedException();
}
}
}

View File

@ -1,5 +1,3 @@
using System;
namespace NzbDrone.Core.Download.Clients.Putio
{
public class PutioException : DownloadClientException
@ -7,7 +5,6 @@ namespace NzbDrone.Core.Download.Clients.Putio
public PutioException(string message)
: base(message)
{
}
}
}

View File

@ -1,4 +1,3 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Download.Clients.Putio
{
public class PutioFileResponse : PutioGenericResponse

View File

@ -1,10 +1,9 @@
using System;
using System;
using System.Collections.Generic;
using NzbDrone.Core.Rest;
using System.Net;
using NLog;
using RestSharp;
using RestSharp.Deserializers;
using Newtonsoft.Json;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
namespace NzbDrone.Core.Download.Clients.Putio
{
@ -18,90 +17,98 @@ namespace NzbDrone.Core.Download.Clients.Putio
void GetAccountSettings(PutioSettings settings);
}
public class PutioProxy: IPutioProxy
public class PutioProxy : IPutioProxy
{
private readonly Logger _logger;
private readonly IHttpClient _httpClient;
public PutioProxy(Logger logger)
public PutioProxy(Logger logger, IHttpClient client)
{
_logger = logger;
_httpClient = client;
}
public List<PutioTorrent> GetTorrents(PutioSettings settings)
{
var result = ProcessRequest<PutioTransfersResponse>(Method.GET, "transfers/list", null, settings);
return result.Transfers;
// var result = ProcessRequest<PutioTransfersResponse>(Method.GET, "transfers/list", null, settings);
// return result.Transfers;
return new List<PutioTorrent>();
}
public PutioFile GetFile(long fileId, PutioSettings settings)
{
var result = ProcessRequest<PutioFileResponse>(Method.GET, "files/" + fileId, null, settings);
return result.File;
// var result = ProcessRequest<PutioFileResponse>(Method.GET, "files/" + fileId, null, settings);
// return result.File;
return new PutioFile();
}
public void AddTorrentFromUrl(string torrentUrl, PutioSettings settings)
{
var arguments = new Dictionary<string, object>();
arguments.Add("url", torrentUrl);
ProcessRequest<PutioGenericResponse>(Method.POST, "transfers/add", arguments, settings);
// var arguments = new Dictionary<string, object>();
// arguments.Add("url", torrentUrl);
// ProcessRequest<PutioGenericResponse>(Method.POST, "transfers/add", arguments, settings);
}
public void AddTorrentFromData(byte[] torrentData, PutioSettings settings)
{
var arguments = new Dictionary<string, object>();
arguments.Add("metainfo", Convert.ToBase64String(torrentData));
ProcessRequest<PutioGenericResponse>(Method.POST, "transfers/add", arguments, settings);
// var arguments = new Dictionary<string, object>();
// arguments.Add("metainfo", Convert.ToBase64String(torrentData));
// ProcessRequest<PutioGenericResponse>(Method.POST, "transfers/add", arguments, settings);
}
public void RemoveTorrent(string hashString, PutioSettings settings)
{
var arguments = new Dictionary<string, object>();
arguments.Add("transfer_ids", new string[] { hashString });
ProcessRequest<PutioGenericResponse>(Method.POST, "torrents/cancel", arguments, settings);
// var arguments = new Dictionary<string, object>();
// arguments.Add("transfer_ids", new string[] { hashString });
// ProcessRequest<PutioGenericResponse>(Method.POST, "torrents/cancel", arguments, settings);
}
public void GetAccountSettings(PutioSettings settings)
{
ProcessRequest<PutioGenericResponse>(Method.GET, "account/settings", null, settings);
// ProcessRequest<PutioGenericResponse>(Method.GET, "account/settings", null, settings);
}
public TResponseType ProcessRequest<TResponseType>(Method method, string resource, Dictionary<string, object> arguments, PutioSettings settings) where TResponseType : PutioGenericResponse
private HttpRequestBuilder BuildRequest(PutioSettings settings)
{
var client = BuildClient(settings);
var request = new RestRequest(resource, method);
request.RequestFormat = DataFormat.Json;
request.AddQueryParameter("oauth_token", settings.OAuthToken);
if (arguments != null)
var requestBuilder = new HttpRequestBuilder("https://api.put.io/v2")
{
foreach (KeyValuePair<string, object> e in arguments)
LogResponseContent = true
};
requestBuilder.SetHeader("Authorization", "Bearer " + settings.OAuthToken);
return requestBuilder;
}
private string ProcessRequest(HttpRequestBuilder requestBuilder)
{
var request = requestBuilder.Build();
request.LogResponseContent = true;
request.SuppressHttpErrorStatusCodes = new[] { HttpStatusCode.Forbidden };
HttpResponse response;
try
{
response = _httpClient.Execute(request);
if (response.StatusCode == HttpStatusCode.Forbidden)
{
request.AddParameter(e.Key, e.Value);
throw new DownloadClientException("Invalid credentials. Check your OAuthToken");
}
}
_logger.Debug("Method: {0} Url: {1}", method, client.BuildUri(request));
var restResponse = client.Execute(request);
var json = new JsonDeserializer();
TResponseType output = json.Deserialize<TResponseType>(restResponse);
if (output.Status != "OK")
catch (Exception ex)
{
throw new PutioException(output.ErrorMessage);
throw new DownloadClientException("Failed to connect to put.io.", ex);
}
return output;
return response.Content;
}
private IRestClient BuildClient(PutioSettings settings)
private TResult ProcessRequest<TResult>(HttpRequestBuilder requestBuilder)
where TResult : new()
{
var restClient = RestClientFactory.BuildClient(settings.Url);
restClient.FollowRedirects = false;
return restClient;
var responseContent = ProcessRequest(requestBuilder);
return Json.Deserialize<TResult>(responseContent);
}
}
}

View File

@ -1,12 +1,10 @@
using System;
namespace NzbDrone.Core.Download.Clients.Putio
{
public sealed class PutioTorrentStatus
{
public static readonly String Completed = "COMPLETED";
public static readonly String Downloading = "DOWNLOADING";
public static readonly String Error = "ERROR";
public static readonly String InQueue = "IN_QUEUE";
public static readonly string Completed = "COMPLETED";
public static readonly string Downloading = "DOWNLOADING";
public static readonly string Error = "ERROR";
public static readonly string InQueue = "IN_QUEUE";
}
}