|
|
|
@ -1,16 +1,30 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using NzbDrone.Common.Http;
|
|
|
|
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
|
|
|
|
using RestSharp;
|
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common.Serializer;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Indexers.PassThePopcorn
|
|
|
|
|
{
|
|
|
|
|
public class PassThePopcornRequestGenerator : IIndexerRequestGenerator
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public PassThePopcornSettings Settings { get; set; }
|
|
|
|
|
|
|
|
|
|
public ICached<Dictionary<string, string>> AuthCookieCache { get; set; }
|
|
|
|
|
public IHttpClient HttpClient { get; set; }
|
|
|
|
|
public Logger Logger { get; set; }
|
|
|
|
|
|
|
|
|
|
//public PassThePopcornRequestGenerator(ICacheManager cacheManager, IHttpClient httpClient, Logger logger)
|
|
|
|
|
//{
|
|
|
|
|
// _httpClient = httpClient;
|
|
|
|
|
// _logger = logger;
|
|
|
|
|
|
|
|
|
|
// _authCookieCache = cacheManager.GetCache<Dictionary<string, string>>(GetType(), "authCookies");
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
public virtual IndexerPageableRequestChain GetRecentRequests()
|
|
|
|
|
{
|
|
|
|
|
var pageableRequests = new IndexerPageableRequestChain();
|
|
|
|
@ -54,42 +68,70 @@ namespace NzbDrone.Core.Indexers.PassThePopcorn
|
|
|
|
|
|
|
|
|
|
private IEnumerable<IndexerRequest> GetRequest(string searchParameters)
|
|
|
|
|
{
|
|
|
|
|
Authenticate();
|
|
|
|
|
|
|
|
|
|
var request =
|
|
|
|
|
new IndexerRequest(
|
|
|
|
|
$"{Settings.BaseUrl.Trim().TrimEnd('/')}/torrents.php?json=noredirect&searchstr={searchParameters}",
|
|
|
|
|
HttpAccept.Json);
|
|
|
|
|
|
|
|
|
|
var cookies = GetPTPCookies();
|
|
|
|
|
var cookies = AuthCookieCache.Find(Settings.BaseUrl.Trim().TrimEnd('/'));
|
|
|
|
|
foreach (var cookie in cookies)
|
|
|
|
|
{
|
|
|
|
|
request.HttpRequest.Cookies[cookie.Name] = cookie.Value;
|
|
|
|
|
request.HttpRequest.Cookies[cookie.Key] = cookie.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield return request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IList<RestResponseCookie> GetPTPCookies()
|
|
|
|
|
private void Authenticate()
|
|
|
|
|
{
|
|
|
|
|
var client = new RestClient(Settings.BaseUrl.Trim().TrimEnd('/'));
|
|
|
|
|
var request = new RestRequest("/ajax.php?action=login", Method.POST);
|
|
|
|
|
request.AddParameter("username", Settings.Username);
|
|
|
|
|
request.AddParameter("password", Settings.Password);
|
|
|
|
|
request.AddParameter("passkey", Settings.Passkey);
|
|
|
|
|
request.AddParameter("keeplogged", "1");
|
|
|
|
|
request.AddParameter("login", "Log In!");
|
|
|
|
|
request.AddHeader("Content-Type", "multipart/form-data");
|
|
|
|
|
|
|
|
|
|
IRestResponse response = client.Execute(request);
|
|
|
|
|
var content = response.Content;
|
|
|
|
|
|
|
|
|
|
var jsonResponse = JObject.Parse(content);
|
|
|
|
|
if (content != null && (string)jsonResponse["Result"] != "Error")
|
|
|
|
|
var requestBuilder = new HttpRequestBuilder($"{Settings.BaseUrl.Trim().TrimEnd('/')}")
|
|
|
|
|
{
|
|
|
|
|
return response.Cookies;
|
|
|
|
|
}
|
|
|
|
|
LogResponseContent = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
requestBuilder.Method = HttpMethod.POST;
|
|
|
|
|
requestBuilder.Resource("ajax.php?action=login");
|
|
|
|
|
requestBuilder.PostProcess += r => r.RequestTimeout = TimeSpan.FromSeconds(15);
|
|
|
|
|
|
|
|
|
|
var authKey = Settings.BaseUrl.Trim().TrimEnd('/');
|
|
|
|
|
var cookies = AuthCookieCache.Find(authKey);
|
|
|
|
|
|
|
|
|
|
throw new Exception("Error connecting to PTP invalid username, password, or passkey");
|
|
|
|
|
if (cookies == null)
|
|
|
|
|
{
|
|
|
|
|
AuthCookieCache.Remove(authKey);
|
|
|
|
|
var authLoginRequest = requestBuilder
|
|
|
|
|
.AddFormParameter("username", Settings.Username)
|
|
|
|
|
.AddFormParameter("password", Settings.Password)
|
|
|
|
|
.AddFormParameter("passkey", Settings.Passkey)
|
|
|
|
|
.AddFormParameter("keeplogged", "1")
|
|
|
|
|
.AddFormParameter("login", "Log In!")
|
|
|
|
|
.SetHeader("Content-Type", "multipart/form-data")
|
|
|
|
|
.Accept(HttpAccept.Json)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
// authLoginRequest.Method = HttpMethod.POST;
|
|
|
|
|
|
|
|
|
|
var response = HttpClient.Execute(authLoginRequest);
|
|
|
|
|
var result = Json.Deserialize<PassThePopcornAuthResponse>(response.Content);
|
|
|
|
|
|
|
|
|
|
if (result.Result != "Ok" || string.IsNullOrWhiteSpace(result.Result))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("PassThePopcorn authentication failed.");
|
|
|
|
|
throw new Exception("Failed to authenticate with PassThePopcorn.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Debug("PassThePopcorn authentication succeeded.");
|
|
|
|
|
|
|
|
|
|
cookies = response.GetCookies();
|
|
|
|
|
AuthCookieCache.Set(authKey, cookies);
|
|
|
|
|
requestBuilder.SetCookies(cookies);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
requestBuilder.SetCookies(cookies);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|