Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GetIndex method #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 57 additions & 38 deletions src/FlareSolverrSharp/Solvers/FlareSolverr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class FlareSolverr
{
private static readonly SemaphoreLocker Locker = new SemaphoreLocker();
private HttpClient _httpClient;
private readonly Uri _flareSolverrIndexUri;
private readonly Uri _flareSolverrUri;

public int MaxTimeout = 60000;
Expand All @@ -26,6 +27,8 @@ public FlareSolverr(string flareSolverrApiUrl)
var apiUrl = flareSolverrApiUrl;
if (!apiUrl.EndsWith("/"))
apiUrl += "/";

_flareSolverrIndexUri = new Uri(apiUrl);
_flareSolverrUri = new Uri(apiUrl + "v1");
}

Expand Down Expand Up @@ -68,19 +71,68 @@ public async Task<FlareSolverrResponse> DestroySession(string sessionId)
return await SendFlareSolverrRequest(GetSolverRequestContent(req));
}

public Task<FlareSolverrIndexResponse> GetIndex()
{
return SendFlareSolverrRequestInternal<FlareSolverrIndexResponse>(null);
}

private async Task<FlareSolverrResponse> SendFlareSolverrRequest(HttpContent flareSolverrRequest)
{
FlareSolverrResponse result = null;
FlareSolverrResponse result = await SendFlareSolverrRequestInternal<FlareSolverrResponse>(flareSolverrRequest);

try
{
Enum.TryParse(result.Status, true, out FlareSolverrStatusCode returnStatusCode);

if (returnStatusCode.Equals(FlareSolverrStatusCode.ok))
{
return result;
}

if (returnStatusCode.Equals(FlareSolverrStatusCode.warning))
{
throw new FlareSolverrException(
"FlareSolverr was able to process the request, but a captcha was detected. Message: "
+ result.Message);
}

if (returnStatusCode.Equals(FlareSolverrStatusCode.error))
{
throw new FlareSolverrException(
"FlareSolverr was unable to process the request, please check FlareSolverr logs. Message: "
+ result.Message);
}

throw new FlareSolverrException("Unable to map FlareSolverr returned status code, received code: "
+ result.Status + ". Message: " + result.Message);
}
catch (ArgumentException)
{
throw new FlareSolverrException("Error parsing status code, check FlareSolverr log. Status: "
+ result.Status + ". Message: " + result.Message);
}
}

private async Task<T> SendFlareSolverrRequestInternal<T>(HttpContent flareSolverrRequest)
{
T result = default;

await Locker.LockAsync(async () =>
{
HttpResponseMessage response;
try
{
_httpClient = new HttpClient();
// wait 5 more seconds to make sure we return the FlareSolverr timeout message
_httpClient.Timeout = TimeSpan.FromMilliseconds(MaxTimeout + 5000);
response = await _httpClient.PostAsync(_flareSolverrUri, flareSolverrRequest);
if (flareSolverrRequest == null)
{
response = await _httpClient.GetAsync(_flareSolverrIndexUri);
}
else
{
// wait 5 more seconds to make sure we return the FlareSolverr timeout message
_httpClient.Timeout = TimeSpan.FromMilliseconds(MaxTimeout + 5000);
response = await _httpClient.PostAsync(_flareSolverrUri, flareSolverrRequest);
}
}
catch (HttpRequestException e)
{
Expand All @@ -104,44 +156,12 @@ await Locker.LockAsync(async () =>
var resContent = await response.Content.ReadAsStringAsync();
try
{
result = JsonConvert.DeserializeObject<FlareSolverrResponse>(resContent);
result = JsonConvert.DeserializeObject<T>(resContent);
}
catch (Exception)
{
throw new FlareSolverrException("Error parsing response, check FlareSolverr. Response: " + resContent);
}

try
{
Enum.TryParse(result.Status, true, out FlareSolverrStatusCode returnStatusCode);

if (returnStatusCode.Equals(FlareSolverrStatusCode.ok))
{
return result;
}

if (returnStatusCode.Equals(FlareSolverrStatusCode.warning))
{
throw new FlareSolverrException(
"FlareSolverr was able to process the request, but a captcha was detected. Message: "
+ result.Message);
}

if (returnStatusCode.Equals(FlareSolverrStatusCode.error))
{
throw new FlareSolverrException(
"FlareSolverr was unable to process the request, please check FlareSolverr logs. Message: "
+ result.Message);
}

throw new FlareSolverrException("Unable to map FlareSolverr returned status code, received code: "
+ result.Status + ". Message: " + result.Message);
}
catch (ArgumentException)
{
throw new FlareSolverrException("Error parsing status code, check FlareSolverr log. Status: "
+ result.Status + ". Message: " + result.Message);
}
});

return result;
Expand Down Expand Up @@ -233,6 +253,5 @@ private HttpContent GenerateFlareSolverrRequest(HttpRequestMessage request, stri

return GetSolverRequestContent(req);
}

}
}
12 changes: 12 additions & 0 deletions src/FlareSolverrSharp/Types/FlareSolverrIndexResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace FlareSolverrSharp.Types
{
public class FlareSolverrIndexResponse
{
[JsonProperty("msg")]
public string Message;
public string Version;
public string UserAgent;
}
}