Skip to content

Commit

Permalink
Merge pull request #44 from NicolasConstant/develop
Browse files Browse the repository at this point in the history
0.6.0 PR
  • Loading branch information
NicolasConstant authored Jan 13, 2021
2 parents 4ef6b75 + a37ebff commit 59edb5b
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/BirdsiteLive.Twitter/Models/ApiStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace BirdsiteLive.Twitter.Models
{
public class ApiStatistics
{
public int UserCallsCount { get; set; }
public int UserCallsMax { get; set; }
public int TweetCallsCount { get; set; }
public int TweetCallsMax { get; set; }
public int TimelineCallsCount { get; set; }
public int TimelineCallsMax { get; set; }
}
}
80 changes: 80 additions & 0 deletions src/BirdsiteLive.Twitter/Statistics/TwitterStatisticsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Threading;
using System.Timers;
using BirdsiteLive.Twitter.Models;

namespace BirdsiteLive.Statistics.Domain
{
public interface ITwitterStatisticsHandler
{
void CalledUserApi();
void CalledTweetApi();
void CalledTimelineApi();
ApiStatistics GetStatistics();
}

//Rate limits: https://developer.twitter.com/en/docs/twitter-api/v1/rate-limits
public class TwitterStatisticsHandler : ITwitterStatisticsHandler
{
private static int _previousUserCalls;
private static int _previousTweetCalls;
private static int _previousTimelineCalls;

private static int _userCalls;
private static int _tweetCalls;
private static int _timelineCalls;

private static System.Timers.Timer _resetTimer;

#region Ctor
public TwitterStatisticsHandler()
{
if (_resetTimer == null)
{
_resetTimer = new System.Timers.Timer();
_resetTimer.Elapsed += OnTimeResetEvent;
_resetTimer.Interval = 15 * 60 * 1000; // 15"
_resetTimer.Enabled = true;
}
}
#endregion

private void OnTimeResetEvent(object sender, ElapsedEventArgs e)
{
_previousUserCalls = _userCalls;
_previousTweetCalls = _tweetCalls;
_previousTimelineCalls = _timelineCalls;

Interlocked.Exchange(ref _userCalls, 0);
Interlocked.Exchange(ref _tweetCalls, 0);
Interlocked.Exchange(ref _timelineCalls, 0);
}

public void CalledUserApi() //GET users/show - 900/15mins
{
Interlocked.Increment(ref _userCalls);
}

public void CalledTweetApi() //GET statuses/lookup - 300/15mins
{
Interlocked.Increment(ref _tweetCalls);
}

public void CalledTimelineApi() // GET statuses/user_timeline - 1500/15 mins
{
Interlocked.Increment(ref _timelineCalls);
}

public ApiStatistics GetStatistics()
{
return new ApiStatistics
{
UserCallsCount = _previousUserCalls,
UserCallsMax = 900,
TweetCallsCount = _previousTweetCalls,
TweetCallsMax = 300,
TimelineCallsCount = _previousTimelineCalls,
TimelineCallsMax = 1500
};
}
}
}
12 changes: 10 additions & 2 deletions src/BirdsiteLive.Twitter/TwitterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.Statistics.Domain;
using BirdsiteLive.Twitter.Extractors;
using BirdsiteLive.Twitter.Models;
using Tweetinvi;
Expand All @@ -24,19 +25,22 @@ public class TwitterService : ITwitterService
{
private readonly TwitterSettings _settings;
private readonly ITweetExtractor _tweetExtractor;
private readonly ITwitterStatisticsHandler _statisticsHandler;

#region Ctor
public TwitterService(TwitterSettings settings, ITweetExtractor tweetExtractor)
public TwitterService(TwitterSettings settings, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler)
{
_settings = settings;
_tweetExtractor = tweetExtractor;
_statisticsHandler = statisticsHandler;
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
}
#endregion

public TwitterUser GetUser(string username)
{
var user = User.GetUserFromScreenName(username);
_statisticsHandler.CalledUserApi();
if (user == null) return null;

return new TwitterUser
Expand All @@ -55,6 +59,8 @@ public ExtractedTweet GetTweet(long statusId)
{
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
var tweet = Tweet.GetTweet(statusId);
_statisticsHandler.CalledTweetApi();
if (tweet == null) return null; //TODO: test this
return _tweetExtractor.Extract(tweet);
}

Expand All @@ -63,10 +69,12 @@ public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTw
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;

var user = User.GetUserFromScreenName(username);
_statisticsHandler.CalledUserApi();
var tweets = new List<ITweet>();
if (fromTweetId == -1)
{
var timeline = Timeline.GetUserTimeline(user.Id, nberTweets);
_statisticsHandler.CalledTimelineApi();
if (timeline != null) tweets.AddRange(timeline);
}
else
Expand All @@ -77,11 +85,11 @@ public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTw
MaximumNumberOfTweetsToRetrieve = nberTweets
};
var timeline = Timeline.GetUserTimeline(user.Id, timelineRequestParameters);
_statisticsHandler.CalledTimelineApi();
if (timeline != null) tweets.AddRange(timeline);
}

return tweets.Select(_tweetExtractor.Extract).ToArray();
//return tweets.Where(x => returnReplies || string.IsNullOrWhiteSpace(x.InReplyToScreenName)).ToArray();
}
}
}
2 changes: 1 addition & 1 deletion src/BirdsiteLive/BirdsiteLive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Version>0.5.0</Version>
<Version>0.6.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 7 additions & 4 deletions src/BirdsiteLive/Controllers/StatisticsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Models.StatisticsModels;
using BirdsiteLive.Statistics.Domain;
using Microsoft.AspNetCore.Mvc;

namespace BirdsiteLive.Controllers
Expand All @@ -12,21 +12,24 @@ public class StatisticsController : Controller
{
private readonly ITwitterUserDal _twitterUserDal;
private readonly IFollowersDal _followersDal;
private readonly ITwitterStatisticsHandler _twitterStatistics;

#region Ctor
public StatisticsController(ITwitterUserDal twitterUserDal, IFollowersDal followersDal)
public StatisticsController(ITwitterUserDal twitterUserDal, IFollowersDal followersDal, ITwitterStatisticsHandler twitterStatistics)
{
_twitterUserDal = twitterUserDal;
_followersDal = followersDal;
_twitterStatistics = twitterStatistics;
}
#endregion

public async Task<IActionResult> Index()
{
var stats = new Statistics
var stats = new Models.StatisticsModels.Statistics
{
FollowersCount = await _followersDal.GetFollowersCountAsync(),
TwitterUserCount = await _twitterUserDal.GetTwitterUsersCountAsync()
TwitterUserCount = await _twitterUserDal.GetTwitterUsersCountAsync(),
TwitterStatistics = _twitterStatistics.GetStatistics()
};
return View(stats);
}
Expand Down
5 changes: 4 additions & 1 deletion src/BirdsiteLive/Models/StatisticsModels/Statistics.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace BirdsiteLive.Models.StatisticsModels
using BirdsiteLive.Twitter.Models;

namespace BirdsiteLive.Models.StatisticsModels
{
public class Statistics
{
public int FollowersCount { get; set; }
public int TwitterUserCount { get; set; }
public ApiStatistics TwitterStatistics { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/BirdsiteLive/Views/Statistics/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@

<h2>Statistics</h2>

<h4>Instance</h4>
<ul>
<li>Twitter Users: @Model.TwitterUserCount</li>
<li>Followers: @Model.FollowersCount</li>
</ul>

<h4>Twitter API</h4>
<ul>
<li>Users Calls: @Model.TwitterStatistics.UserCallsCount / @Model.TwitterStatistics.UserCallsMax</li>
<li>Tweets Calls: @Model.TwitterStatistics.TweetCallsCount / @Model.TwitterStatistics.TweetCallsMax</li>
<li>Timeline Calls: @Model.TwitterStatistics.TimelineCallsCount / @Model.TwitterStatistics.TimelineCallsMax</li>
</ul>

0 comments on commit 59edb5b

Please sign in to comment.