removed dangerous initialization
This commit is contained in:
parent
62a88e40c6
commit
6a867f2305
5 changed files with 86 additions and 11 deletions
|
@ -13,4 +13,8 @@
|
||||||
<ProjectReference Include="..\BirdsiteLive.Common\BirdsiteLive.Common.csproj" />
|
<ProjectReference Include="..\BirdsiteLive.Common\BirdsiteLive.Common.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Tools\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BirdsiteLive.Common.Settings;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Tweetinvi;
|
||||||
|
|
||||||
|
namespace BirdsiteLive.Twitter.Tools
|
||||||
|
{
|
||||||
|
public interface ITwitterAuthenticationInitializer
|
||||||
|
{
|
||||||
|
void EnsureAuthenticationIsInitialized();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TwitterAuthenticationInitializer : ITwitterAuthenticationInitializer
|
||||||
|
{
|
||||||
|
private readonly TwitterSettings _settings;
|
||||||
|
private readonly ILogger<TwitterAuthenticationInitializer> _logger;
|
||||||
|
private static bool _initialized;
|
||||||
|
private readonly SemaphoreSlim _semaphoregate = new SemaphoreSlim(1);
|
||||||
|
|
||||||
|
#region Ctor
|
||||||
|
public TwitterAuthenticationInitializer(TwitterSettings settings, ILogger<TwitterAuthenticationInitializer> logger)
|
||||||
|
{
|
||||||
|
_settings = settings;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public void EnsureAuthenticationIsInitialized()
|
||||||
|
{
|
||||||
|
if (_initialized) return;
|
||||||
|
_semaphoregate.Wait();
|
||||||
|
if (_initialized) return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
InitTwitterCredentials();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_semaphoregate.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitTwitterCredentials()
|
||||||
|
{
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
|
||||||
|
_initialized = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "Twitter Authentication Failed");
|
||||||
|
Thread.Sleep(250);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ using BirdsiteLive.Common.Settings;
|
||||||
using BirdsiteLive.Statistics.Domain;
|
using BirdsiteLive.Statistics.Domain;
|
||||||
using BirdsiteLive.Twitter.Extractors;
|
using BirdsiteLive.Twitter.Extractors;
|
||||||
using BirdsiteLive.Twitter.Models;
|
using BirdsiteLive.Twitter.Models;
|
||||||
|
using BirdsiteLive.Twitter.Tools;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Tweetinvi;
|
using Tweetinvi;
|
||||||
using Tweetinvi.Models;
|
using Tweetinvi.Models;
|
||||||
|
@ -20,21 +21,20 @@ namespace BirdsiteLive.Twitter
|
||||||
|
|
||||||
public class TwitterTweetsService : ITwitterTweetsService
|
public class TwitterTweetsService : ITwitterTweetsService
|
||||||
{
|
{
|
||||||
private readonly TwitterSettings _settings;
|
private readonly ITwitterAuthenticationInitializer _twitterAuthenticationInitializer;
|
||||||
private readonly ITweetExtractor _tweetExtractor;
|
private readonly ITweetExtractor _tweetExtractor;
|
||||||
private readonly ITwitterStatisticsHandler _statisticsHandler;
|
private readonly ITwitterStatisticsHandler _statisticsHandler;
|
||||||
private readonly ITwitterUserService _twitterUserService;
|
private readonly ITwitterUserService _twitterUserService;
|
||||||
private readonly ILogger<TwitterTweetsService> _logger;
|
private readonly ILogger<TwitterTweetsService> _logger;
|
||||||
|
|
||||||
#region Ctor
|
#region Ctor
|
||||||
public TwitterTweetsService(TwitterSettings settings, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger<TwitterTweetsService> logger)
|
public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger<TwitterTweetsService> logger)
|
||||||
{
|
{
|
||||||
_settings = settings;
|
_twitterAuthenticationInitializer = twitterAuthenticationInitializer;
|
||||||
_tweetExtractor = tweetExtractor;
|
_tweetExtractor = tweetExtractor;
|
||||||
_statisticsHandler = statisticsHandler;
|
_statisticsHandler = statisticsHandler;
|
||||||
_twitterUserService = twitterUserService;
|
_twitterUserService = twitterUserService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
@ -42,8 +42,10 @@ namespace BirdsiteLive.Twitter
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
|
||||||
ExceptionHandler.SwallowWebExceptions = false;
|
ExceptionHandler.SwallowWebExceptions = false;
|
||||||
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
|
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
|
||||||
|
|
||||||
var tweet = Tweet.GetTweet(statusId);
|
var tweet = Tweet.GetTweet(statusId);
|
||||||
_statisticsHandler.CalledTweetApi();
|
_statisticsHandler.CalledTweetApi();
|
||||||
if (tweet == null) return null; //TODO: test this
|
if (tweet == null) return null; //TODO: test this
|
||||||
|
@ -58,12 +60,13 @@ namespace BirdsiteLive.Twitter
|
||||||
|
|
||||||
public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1)
|
public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1)
|
||||||
{
|
{
|
||||||
ExceptionHandler.SwallowWebExceptions = false;
|
|
||||||
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
|
|
||||||
|
|
||||||
var tweets = new List<ITweet>();
|
var tweets = new List<ITweet>();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
|
||||||
|
ExceptionHandler.SwallowWebExceptions = false;
|
||||||
|
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
|
||||||
|
|
||||||
var user = _twitterUserService.GetUser(username);
|
var user = _twitterUserService.GetUser(username);
|
||||||
if (user == null || user.Protected) return new ExtractedTweet[0];
|
if (user == null || user.Protected) return new ExtractedTweet[0];
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ using System.Linq;
|
||||||
using BirdsiteLive.Common.Settings;
|
using BirdsiteLive.Common.Settings;
|
||||||
using BirdsiteLive.Statistics.Domain;
|
using BirdsiteLive.Statistics.Domain;
|
||||||
using BirdsiteLive.Twitter.Models;
|
using BirdsiteLive.Twitter.Models;
|
||||||
|
using BirdsiteLive.Twitter.Tools;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Tweetinvi;
|
using Tweetinvi;
|
||||||
using Tweetinvi.Models;
|
using Tweetinvi.Models;
|
||||||
|
@ -16,22 +17,22 @@ namespace BirdsiteLive.Twitter
|
||||||
|
|
||||||
public class TwitterUserService : ITwitterUserService
|
public class TwitterUserService : ITwitterUserService
|
||||||
{
|
{
|
||||||
private readonly TwitterSettings _settings;
|
private readonly ITwitterAuthenticationInitializer _twitterAuthenticationInitializer;
|
||||||
private readonly ITwitterStatisticsHandler _statisticsHandler;
|
private readonly ITwitterStatisticsHandler _statisticsHandler;
|
||||||
private readonly ILogger<TwitterUserService> _logger;
|
private readonly ILogger<TwitterUserService> _logger;
|
||||||
|
|
||||||
#region Ctor
|
#region Ctor
|
||||||
public TwitterUserService(TwitterSettings settings, ITwitterStatisticsHandler statisticsHandler, ILogger<TwitterUserService> logger)
|
public TwitterUserService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITwitterStatisticsHandler statisticsHandler, ILogger<TwitterUserService> logger)
|
||||||
{
|
{
|
||||||
_settings = settings;
|
_twitterAuthenticationInitializer = twitterAuthenticationInitializer;
|
||||||
_statisticsHandler = statisticsHandler;
|
_statisticsHandler = statisticsHandler;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public TwitterUser GetUser(string username)
|
public TwitterUser GetUser(string username)
|
||||||
{
|
{
|
||||||
|
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
|
||||||
ExceptionHandler.SwallowWebExceptions = false;
|
ExceptionHandler.SwallowWebExceptions = false;
|
||||||
|
|
||||||
IUser user;
|
IUser user;
|
||||||
|
|
|
@ -10,6 +10,7 @@ using BirdsiteLive.DAL.Postgres.DataAccessLayers;
|
||||||
using BirdsiteLive.DAL.Postgres.Settings;
|
using BirdsiteLive.DAL.Postgres.Settings;
|
||||||
using BirdsiteLive.Models;
|
using BirdsiteLive.Models;
|
||||||
using BirdsiteLive.Twitter;
|
using BirdsiteLive.Twitter;
|
||||||
|
using BirdsiteLive.Twitter.Tools;
|
||||||
using Lamar;
|
using Lamar;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
@ -87,6 +88,8 @@ namespace BirdsiteLive
|
||||||
services.For<ITwitterUserService>().DecorateAllWith<CachedTwitterUserService>();
|
services.For<ITwitterUserService>().DecorateAllWith<CachedTwitterUserService>();
|
||||||
services.For<ITwitterUserService>().Use<TwitterUserService>().Singleton();
|
services.For<ITwitterUserService>().Use<TwitterUserService>().Singleton();
|
||||||
|
|
||||||
|
services.For<ITwitterAuthenticationInitializer>().Use<TwitterAuthenticationInitializer>().Singleton();
|
||||||
|
|
||||||
services.Scan(_ =>
|
services.Scan(_ =>
|
||||||
{
|
{
|
||||||
_.Assembly("BirdsiteLive.Twitter");
|
_.Assembly("BirdsiteLive.Twitter");
|
||||||
|
|
Loading…
Add table
Reference in a new issue