Merge pull request #133 from NicolasConstant/topic_detect-saturation-from-api-itself

get rate limit from API
This commit is contained in:
Nicolas Constant 2022-02-03 19:48:52 -05:00 committed by GitHub
commit 446b222881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 10 deletions

View File

@ -47,6 +47,11 @@ namespace BirdsiteLive.Twitter
return user; return user;
} }
public bool IsUserApiRateLimited()
{
return _twitterService.IsUserApiRateLimited();
}
public void PurgeUser(string username) public void PurgeUser(string username)
{ {
_userCache.Remove(username); _userCache.Remove(username);

View File

@ -13,6 +13,7 @@ namespace BirdsiteLive.Twitter
public interface ITwitterUserService public interface ITwitterUserService
{ {
TwitterUser GetUser(string username); TwitterUser GetUser(string username);
bool IsUserApiRateLimited();
} }
public class TwitterUserService : ITwitterUserService public class TwitterUserService : ITwitterUserService
@ -33,13 +34,12 @@ namespace BirdsiteLive.Twitter
public TwitterUser GetUser(string username) public TwitterUser GetUser(string username)
{ {
//Check if API is saturated //Check if API is saturated
var currentCalls = _statisticsHandler.GetCurrentUserCalls(); if (IsUserApiRateLimited()) return null;
var maxCalls = _statisticsHandler.GetStatistics().UserCallsMax;
if (currentCalls > maxCalls) return null;
//Proceed to account retrieval //Proceed to account retrieval
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized(); _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
ExceptionHandler.SwallowWebExceptions = false; ExceptionHandler.SwallowWebExceptions = false;
RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly;
IUser user; IUser user;
try try
@ -76,5 +76,32 @@ namespace BirdsiteLive.Twitter
Protected = user.Protected Protected = user.Protected
}; };
} }
public bool IsUserApiRateLimited()
{
// Retrieve limit from tooling
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
ExceptionHandler.SwallowWebExceptions = false;
RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly;
try
{
var queryRateLimits = RateLimit.GetQueryRateLimit("https://api.twitter.com/1.1/users/show.json?screen_name=mastodon");
if (queryRateLimits != null)
{
return queryRateLimits.Remaining <= 0;
}
}
catch (Exception e)
{
_logger.LogError(e, "Error retrieving rate limits");
}
// Fallback
var currentCalls = _statisticsHandler.GetCurrentUserCalls();
var maxCalls = _statisticsHandler.GetStatistics().UserCallsMax;
return currentCalls >= maxCalls;
}
} }
} }

View File

@ -13,7 +13,6 @@ using BirdsiteLive.Common.Regexes;
using BirdsiteLive.Common.Settings; using BirdsiteLive.Common.Settings;
using BirdsiteLive.Domain; using BirdsiteLive.Domain;
using BirdsiteLive.Models; using BirdsiteLive.Models;
using BirdsiteLive.Statistics.Domain;
using BirdsiteLive.Tools; using BirdsiteLive.Tools;
using BirdsiteLive.Twitter; using BirdsiteLive.Twitter;
using BirdsiteLive.Twitter.Models; using BirdsiteLive.Twitter.Models;
@ -33,10 +32,9 @@ namespace BirdsiteLive.Controllers
private readonly IStatusService _statusService; private readonly IStatusService _statusService;
private readonly InstanceSettings _instanceSettings; private readonly InstanceSettings _instanceSettings;
private readonly ILogger<UsersController> _logger; private readonly ILogger<UsersController> _logger;
private readonly ITwitterStatisticsHandler _twitterStatisticsHandler;
#region Ctor #region Ctor
public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService, ILogger<UsersController> logger, ITwitterStatisticsHandler twitterStatisticsHandler) public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService, ILogger<UsersController> logger)
{ {
_twitterUserService = twitterUserService; _twitterUserService = twitterUserService;
_userService = userService; _userService = userService;
@ -44,7 +42,6 @@ namespace BirdsiteLive.Controllers
_instanceSettings = instanceSettings; _instanceSettings = instanceSettings;
_twitterTweetService = twitterTweetService; _twitterTweetService = twitterTweetService;
_logger = logger; _logger = logger;
_twitterStatisticsHandler = twitterStatisticsHandler;
} }
#endregion #endregion
@ -75,9 +72,7 @@ namespace BirdsiteLive.Controllers
if (!string.IsNullOrWhiteSpace(id) && UserRegexes.TwitterAccount.IsMatch(id) && id.Length <= 15) if (!string.IsNullOrWhiteSpace(id) && UserRegexes.TwitterAccount.IsMatch(id) && id.Length <= 15)
user = _twitterUserService.GetUser(id); user = _twitterUserService.GetUser(id);
var isSaturated = user == null var isSaturated = _twitterUserService.IsUserApiRateLimited();
&& _twitterStatisticsHandler.GetCurrentUserCalls() >=
_twitterStatisticsHandler.GetStatistics().UserCallsMax;
var acceptHeaders = Request.Headers["Accept"]; var acceptHeaders = Request.Headers["Accept"];
if (acceptHeaders.Any()) if (acceptHeaders.Any())