auto reset tokens on 429
This commit is contained in:
parent
2d969591b0
commit
219841e016
2 changed files with 28 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BirdsiteLive.Common.Settings;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
@ -14,6 +15,7 @@ namespace BirdsiteLive.Twitter.Tools
|
|||
{
|
||||
Task<HttpClient> MakeHttpClient();
|
||||
HttpRequestMessage MakeHttpRequest(HttpMethod m, string endpoint);
|
||||
Task RefreshClient(HttpRequestMessage client);
|
||||
}
|
||||
|
||||
public class TwitterAuthenticationInitializer : ITwitterAuthenticationInitializer
|
||||
|
@ -45,6 +47,22 @@ namespace BirdsiteLive.Twitter.Tools
|
|||
#endregion
|
||||
|
||||
|
||||
public async Task RefreshClient(HttpRequestMessage req)
|
||||
{
|
||||
string token = req.Headers.GetValues("x-guest-token").First();
|
||||
|
||||
var i = _tokens.IndexOf(token);
|
||||
|
||||
// this is prabably not thread save but yolo
|
||||
try
|
||||
{
|
||||
_twitterClients.RemoveAt(i);
|
||||
_tokens.RemoveAt(i);
|
||||
}
|
||||
catch (IndexOutOfRangeException _) {}
|
||||
|
||||
await RefreshCred();
|
||||
}
|
||||
|
||||
private async Task RefreshCred()
|
||||
{
|
||||
|
@ -113,7 +131,6 @@ namespace BirdsiteLive.Twitter.Tools
|
|||
}
|
||||
public HttpRequestMessage MakeHttpRequest(HttpMethod m, string endpoint)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
var request = new HttpRequestMessage(m, endpoint);
|
||||
int r = rnd.Next(_twitterClients.Count);
|
||||
request.Headers.TryAddWithoutValidation("Authorization", $"Bearer " + BearerToken);
|
||||
|
|
|
@ -102,19 +102,21 @@ namespace BirdsiteLive.Twitter
|
|||
"%22%2C%22count%22%3A40%2C%22includePromotedContent%22%3Atrue%2C%22withQuickPromoteEligibilityTweetFields%22%3Atrue%2C%22withSuperFollowsUserFields%22%3Atrue%2C%22withDownvotePerspective%22%3Afalse%2C%22withReactionsMetadata%22%3Afalse%2C%22withReactionsPerspective%22%3Afalse%2C%22withSuperFollowsTweetFields%22%3Atrue%2C%22withVoice%22%3Atrue%2C%22withV2Timeline%22%3Atrue%7D&features=%7B%22responsive_web_twitter_blue_verified_badge_is_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%2C%22unified_cards_ad_metadata_container_dynamic_card_content_query_enabled%22%3Atrue%2C%22tweetypie_unmention_optimization_enabled%22%3Atrue%2C%22responsive_web_uc_gql_enabled%22%3Atrue%2C%22vibe_api_enabled%22%3Atrue%2C%22responsive_web_edit_tweet_api_enabled%22%3Atrue%2C%22graphql_is_translatable_rweb_tweet_is_translatable_enabled%22%3Afalse%2C%22standardized_nudges_misinfo%22%3Atrue%2C%22tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled%22%3Afalse%2C%22interactive_text_enabled%22%3Atrue%2C%22responsive_web_text_conversations_enabled%22%3Afalse%2C%22responsive_web_enhance_cards_enabled%22%3Atrue%7D";
|
||||
JsonDocument results;
|
||||
List<ExtractedTweet> extractedTweets = new List<ExtractedTweet>();
|
||||
using var request = _twitterAuthenticationInitializer.MakeHttpRequest(new HttpMethod("GET"), reqURL);
|
||||
try
|
||||
{
|
||||
using (var request = _twitterAuthenticationInitializer.MakeHttpRequest(new HttpMethod("GET"), reqURL))
|
||||
{
|
||||
|
||||
var httpResponse = await client.SendAsync(request);
|
||||
httpResponse.EnsureSuccessStatusCode();
|
||||
var c = await httpResponse.Content.ReadAsStringAsync();
|
||||
results = JsonDocument.Parse(c);
|
||||
}
|
||||
var httpResponse = await client.SendAsync(request);
|
||||
httpResponse.EnsureSuccessStatusCode();
|
||||
var c = await httpResponse.Content.ReadAsStringAsync();
|
||||
results = JsonDocument.Parse(c);
|
||||
|
||||
_statisticsHandler.CalledTweetApi();
|
||||
if (results == null) return null; //TODO: test this
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
await _twitterAuthenticationInitializer.RefreshClient(request);
|
||||
return null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
Reference in a new issue