forked from sim0n00ps/OF-DL
Updated subscription lookup to match OF website.
It always increments the offset with the limit before making the next request.
This commit is contained in:
parent
bf29f3534f
commit
ed5f6ad6f7
@ -26,6 +26,9 @@ namespace OF_DL.Helpers;
|
||||
|
||||
public class APIHelper : IAPIHelper
|
||||
{
|
||||
private const int MAX_RETRIES = 10;
|
||||
private const int DELAY_BEFORE_RETRY = 1000;
|
||||
|
||||
private static readonly JsonSerializerSettings m_JsonSerializerSettings;
|
||||
private readonly IDBHelper m_DBHelper;
|
||||
private readonly IDownloadConfig downloadConfig;
|
||||
@ -119,18 +122,31 @@ public class APIHelper : IAPIHelper
|
||||
}
|
||||
|
||||
|
||||
private async Task<string?> BuildHeaderAndExecuteRequests(Dictionary<string, string> getParams, string endpoint, HttpClient client, HttpMethod? method = null)
|
||||
private async Task<string?> BuildHeaderAndExecuteRequests(Dictionary<string, string> getParams, string endpoint, HttpClient client, HttpMethod? method = null, int retryCount = 0)
|
||||
{
|
||||
Log.Debug("Calling BuildHeaderAndExecuteRequests");
|
||||
Log.Debug("Calling BuildHeaderAndExecuteRequests -- Attempt number: {AttemptNumber}", retryCount + 1);
|
||||
|
||||
HttpRequestMessage request = await BuildHttpRequestMessage(getParams, endpoint, method);
|
||||
using var response = await client.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
string body = await response.Content.ReadAsStringAsync();
|
||||
try
|
||||
{
|
||||
HttpRequestMessage request = await BuildHttpRequestMessage(getParams, endpoint, method);
|
||||
using var response = await client.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
string body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Log.Debug(body);
|
||||
Log.Debug(body);
|
||||
|
||||
return body;
|
||||
return body;
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
if (ex.StatusCode == System.Net.HttpStatusCode.TooManyRequests && retryCount < MAX_RETRIES)
|
||||
{
|
||||
await Task.Delay(DELAY_BEFORE_RETRY);
|
||||
return await BuildHeaderAndExecuteRequests(getParams, endpoint, client, method, ++retryCount);
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -304,50 +320,44 @@ public class APIHelper : IAPIHelper
|
||||
try
|
||||
{
|
||||
Dictionary<string, int> users = new();
|
||||
Subscriptions subscriptions = new();
|
||||
|
||||
int limit = 25;
|
||||
int offset = 0;
|
||||
|
||||
getParams["limit"] = limit.ToString();
|
||||
getParams["offset"] = offset.ToString();
|
||||
|
||||
Log.Debug("Calling GetAllSubscrptions");
|
||||
|
||||
string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, httpClient);
|
||||
|
||||
subscriptions = JsonConvert.DeserializeObject<Subscriptions>(body);
|
||||
if (subscriptions != null && subscriptions.hasMore)
|
||||
while (true)
|
||||
{
|
||||
getParams["offset"] = subscriptions.list.Count.ToString();
|
||||
string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, httpClient);
|
||||
|
||||
while (true)
|
||||
if (string.IsNullOrWhiteSpace(body))
|
||||
break;
|
||||
|
||||
Subscriptions? subscriptions = JsonConvert.DeserializeObject<Subscriptions>(body, m_JsonSerializerSettings);
|
||||
|
||||
if (subscriptions?.list is null)
|
||||
break;
|
||||
|
||||
foreach (Subscriptions.List item in subscriptions.list)
|
||||
{
|
||||
Subscriptions newSubscriptions = new();
|
||||
string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, httpClient);
|
||||
if (users.ContainsKey(item.username))
|
||||
continue;
|
||||
|
||||
if (!string.IsNullOrEmpty(loopbody) && (!loopbody.Contains("[]") || loopbody.Trim() != "[]"))
|
||||
{
|
||||
newSubscriptions = JsonConvert.DeserializeObject<Subscriptions>(loopbody, m_JsonSerializerSettings);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
bool isRestricted = item.isRestricted ?? false;
|
||||
bool isRestrictedButAllowed = isRestricted && includeRestricted;
|
||||
|
||||
subscriptions.list.AddRange(newSubscriptions.list);
|
||||
if (!newSubscriptions.hasMore)
|
||||
{
|
||||
break;
|
||||
}
|
||||
getParams["offset"] = subscriptions.list.Count.ToString();
|
||||
if (!isRestricted || isRestrictedButAllowed)
|
||||
users.Add(item.username, item.id);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Subscriptions.List subscription in subscriptions.list)
|
||||
{
|
||||
if (users.ContainsKey(subscription.username))
|
||||
continue;
|
||||
if (!subscriptions.hasMore)
|
||||
break;
|
||||
|
||||
bool isRestricted = subscription.isRestricted ?? false;
|
||||
bool isRestrictedButAllowed = isRestricted && includeRestricted;
|
||||
|
||||
if (!isRestricted || isRestrictedButAllowed)
|
||||
users.Add(subscription.username, subscription.id);
|
||||
offset += limit;
|
||||
getParams["offset"] = offset.ToString();
|
||||
}
|
||||
|
||||
return users;
|
||||
@ -370,12 +380,13 @@ public class APIHelper : IAPIHelper
|
||||
{
|
||||
Dictionary<string, string> getParams = new()
|
||||
{
|
||||
{ "offset", "0" },
|
||||
{ "limit", "50" },
|
||||
{ "type", "active" },
|
||||
{ "format", "infinite"}
|
||||
};
|
||||
|
||||
Log.Debug("Calling GetActiveSubscriptions");
|
||||
AnsiConsole.Markup($"[red]Getting Active Subscriptions (Include Restricted: {includeRestricted})\n[/]");
|
||||
|
||||
return await GetAllSubscriptions(getParams, endpoint, includeRestricted, config);
|
||||
}
|
||||
|
||||
@ -384,13 +395,12 @@ public class APIHelper : IAPIHelper
|
||||
{
|
||||
Dictionary<string, string> getParams = new()
|
||||
{
|
||||
{ "offset", "0" },
|
||||
{ "limit", "50" },
|
||||
{ "type", "expired" },
|
||||
{ "format", "infinite"}
|
||||
};
|
||||
|
||||
Log.Debug("Calling GetExpiredSubscriptions");
|
||||
AnsiConsole.Markup($"[red]Getting Expired Subscriptions (Include Restricted: {includeRestricted})\n[/]");
|
||||
|
||||
return await GetAllSubscriptions(getParams, endpoint, includeRestricted, config);
|
||||
}
|
||||
@ -399,13 +409,12 @@ public class APIHelper : IAPIHelper
|
||||
{
|
||||
Dictionary<string, string> getParams = new()
|
||||
{
|
||||
{ "offset", "0" },
|
||||
{ "limit", "50" },
|
||||
{ "type", "expired" },
|
||||
{ "format", "infinite"}
|
||||
};
|
||||
|
||||
Log.Debug("Calling GetBlockedUsers");
|
||||
AnsiConsole.Markup($"[red]Getting Blocked Users\n[/]");
|
||||
|
||||
return await GetAllSubscriptions(getParams, endpoint, true, config);
|
||||
}
|
||||
@ -1172,7 +1181,7 @@ public class APIHelper : IAPIHelper
|
||||
}
|
||||
break;
|
||||
case VideoResolution._240:
|
||||
if(medium.videoSources != null)
|
||||
if (medium.videoSources != null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(medium.videoSources._240))
|
||||
{
|
||||
@ -1199,7 +1208,7 @@ public class APIHelper : IAPIHelper
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
else if (medium.canView && medium.files != null && medium.files.drm != null)
|
||||
|
@ -893,10 +893,6 @@ public class Program
|
||||
{
|
||||
const string OUTPUT_FILE = "blocked-users.json";
|
||||
|
||||
Log.Debug($"Calling GetBlockedUsers");
|
||||
|
||||
AnsiConsole.Markup($"[red]Getting Blocked Users\n[/]");
|
||||
|
||||
Dictionary<string, int>? blockedUsers = await m_ApiHelper.GetBlockedUsers("/users/blocked", Config);
|
||||
|
||||
if (blockedUsers is null || blockedUsers.Count == 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user