diff --git a/src/BirdsiteLive.Twitter/CachedTwitterService.cs b/src/BirdsiteLive.Twitter/CachedTwitterService.cs index d2d0da5..877d3ae 100644 --- a/src/BirdsiteLive.Twitter/CachedTwitterService.cs +++ b/src/BirdsiteLive.Twitter/CachedTwitterService.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json; using System.Threading.Tasks; using BirdsiteLive.Common.Settings; using BirdsiteLive.Twitter.Models; @@ -9,6 +10,7 @@ namespace BirdsiteLive.Twitter public interface ICachedTwitterUserService : ITwitterUserService { void PurgeUser(string username); + void AddUser(TwitterUser user); } public class CachedTwitterUserService : ICachedTwitterUserService @@ -53,9 +55,18 @@ namespace BirdsiteLive.Twitter return _twitterService.IsUserApiRateLimited(); } + public TwitterUser Extract(JsonElement result) + { + return _twitterService.Extract(result); + } public void PurgeUser(string username) { _userCache.Remove(username); } + public void AddUser(TwitterUser user) + { + + _userCache.Set(user.Acct, user, _cacheEntryOptions); + } } } \ No newline at end of file diff --git a/src/BirdsiteLive.Twitter/TwitterTweetsService.cs b/src/BirdsiteLive.Twitter/TwitterTweetsService.cs index bd879b9..d252a72 100644 --- a/src/BirdsiteLive.Twitter/TwitterTweetsService.cs +++ b/src/BirdsiteLive.Twitter/TwitterTweetsService.cs @@ -25,12 +25,12 @@ namespace BirdsiteLive.Twitter { private readonly ITwitterAuthenticationInitializer _twitterAuthenticationInitializer; private readonly ITwitterStatisticsHandler _statisticsHandler; - private readonly ITwitterUserService _twitterUserService; + private readonly ICachedTwitterUserService _twitterUserService; private readonly ILogger _logger; private HttpClient _httpClient = new HttpClient(); #region Ctor - public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger logger) + public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITwitterStatisticsHandler statisticsHandler, ICachedTwitterUserService twitterUserService, ILogger logger) { _twitterAuthenticationInitializer = twitterAuthenticationInitializer; _statisticsHandler = statisticsHandler; @@ -126,7 +126,7 @@ namespace BirdsiteLive.Twitter { var extractedTweet = await Extract(tweet); extractedTweets.Add(extractedTweet); - + if (extractedTweet.Id == fromTweetId) break; @@ -137,6 +137,17 @@ namespace BirdsiteLive.Twitter + JsonObject.Create(tweet).ToJsonString(new JsonSerializerOptions { WriteIndented = true })); } + + try + { + JsonElement userDoc = tweet.GetProperty("content").GetProperty("itemContent") + .GetProperty("tweet_results").GetProperty("core").GetProperty("user_results"); + + TwitterUser tweetUser = _twitterUserService.Extract(userDoc); + _twitterUserService.AddUser(tweetUser); + } + catch (Exception e) + {} } } diff --git a/src/BirdsiteLive.Twitter/TwitterUserService.cs b/src/BirdsiteLive.Twitter/TwitterUserService.cs index f286dda..e8e42ea 100644 --- a/src/BirdsiteLive.Twitter/TwitterUserService.cs +++ b/src/BirdsiteLive.Twitter/TwitterUserService.cs @@ -14,6 +14,7 @@ namespace BirdsiteLive.Twitter public interface ITwitterUserService { Task GetUserAsync(string username); + TwitterUser Extract (JsonElement result); bool IsUserApiRateLimited(); } @@ -53,25 +54,7 @@ namespace BirdsiteLive.Twitter res = JsonDocument.Parse(c); } var result = res.RootElement.GetProperty("data").GetProperty("user").GetProperty("result"); - string profileBannerURL = null; - JsonElement profileBannerURLObject; - if (result.GetProperty("legacy").TryGetProperty("profile_banner_url", out profileBannerURLObject)) - { - profileBannerURL = profileBannerURLObject.GetString(); - } - - return new TwitterUser - { - Id = long.Parse(result.GetProperty("rest_id").GetString()), - Acct = username, - Name = result.GetProperty("legacy").GetProperty("name").GetString(), //res.RootElement.GetProperty("data").GetProperty("name").GetString(), - Description = "", //res.RootElement.GetProperty("data").GetProperty("description").GetString(), - Url = "", //res.RootElement.GetProperty("data").GetProperty("url").GetString(), - ProfileImageUrl = result.GetProperty("legacy").GetProperty("profile_image_url_https").GetString().Replace("normal", "400x400"), - ProfileBackgroundImageUrl = profileBannerURL, - ProfileBannerURL = profileBannerURL, - Protected = false, //res.RootElement.GetProperty("data").GetProperty("protected").GetBoolean(), - }; + return Extract(result); } catch (System.Collections.Generic.KeyNotFoundException) { @@ -106,6 +89,30 @@ namespace BirdsiteLive.Twitter } + public TwitterUser Extract(JsonElement result) + { + string profileBannerURL = null; + JsonElement profileBannerURLObject; + if (result.GetProperty("legacy").TryGetProperty("profile_banner_url", out profileBannerURLObject)) + { + profileBannerURL = profileBannerURLObject.GetString(); + } + + return new TwitterUser + { + Id = long.Parse(result.GetProperty("rest_id").GetString()), + Acct = result.GetProperty("legacy").GetProperty("screen_name").GetString(), + Name = result.GetProperty("legacy").GetProperty("name").GetString(), //res.RootElement.GetProperty("data").GetProperty("name").GetString(), + Description = "", //res.RootElement.GetProperty("data").GetProperty("description").GetString(), + Url = "", //res.RootElement.GetProperty("data").GetProperty("url").GetString(), + ProfileImageUrl = result.GetProperty("legacy").GetProperty("profile_image_url_https").GetString().Replace("normal", "400x400"), + ProfileBackgroundImageUrl = profileBannerURL, + ProfileBannerURL = profileBannerURL, + Protected = false, //res.RootElement.GetProperty("data").GetProperty("protected").GetBoolean(), + }; + + } + public bool IsUserApiRateLimited() { return false; diff --git a/src/Tests/BirdsiteLive.Twitter.Tests/TimelineTests.cs b/src/Tests/BirdsiteLive.Twitter.Tests/TimelineTests.cs index 79b170c..4bb6bd2 100644 --- a/src/Tests/BirdsiteLive.Twitter.Tests/TimelineTests.cs +++ b/src/Tests/BirdsiteLive.Twitter.Tests/TimelineTests.cs @@ -19,9 +19,11 @@ namespace BirdsiteLive.ActivityPub.Tests var logger2 = new Mock>(MockBehavior.Strict); var logger3 = new Mock>(); var stats = new Mock(); + var settings = new Mock(); ITwitterAuthenticationInitializer auth = new TwitterAuthenticationInitializer(logger1.Object); ITwitterUserService user = new TwitterUserService(auth, stats.Object, logger2.Object); - _tweetService = new TwitterTweetsService(auth, stats.Object, user, logger3.Object); + ICachedTwitterUserService user2 = new CachedTwitterUserService(user, settings.Object); + _tweetService = new TwitterTweetsService(auth, stats.Object, user2, logger3.Object); } [TestMethod] diff --git a/src/Tests/BirdsiteLive.Twitter.Tests/TweetTests.cs b/src/Tests/BirdsiteLive.Twitter.Tests/TweetTests.cs index 7f57f96..089dc09 100644 --- a/src/Tests/BirdsiteLive.Twitter.Tests/TweetTests.cs +++ b/src/Tests/BirdsiteLive.Twitter.Tests/TweetTests.cs @@ -18,10 +18,12 @@ namespace BirdsiteLive.ActivityPub.Tests var logger1 = new Mock>(MockBehavior.Strict); var logger2 = new Mock>(MockBehavior.Strict); var logger3 = new Mock>(); + var settings = new Mock(); var stats = new Mock(); ITwitterAuthenticationInitializer auth = new TwitterAuthenticationInitializer(logger1.Object); ITwitterUserService user = new TwitterUserService(auth, stats.Object, logger2.Object); - _tweetService = new TwitterTweetsService(auth, stats.Object, user, logger3.Object); + ICachedTwitterUserService user2 = new CachedTwitterUserService(user, settings.Object); + _tweetService = new TwitterTweetsService(auth, stats.Object, user2, logger3.Object); } [TestMethod] diff --git a/src/Tests/BirdsiteLive.Twitter.Tests/UsersTest.cs b/src/Tests/BirdsiteLive.Twitter.Tests/UsersTest.cs index 99c22c1..56c5e73 100644 --- a/src/Tests/BirdsiteLive.Twitter.Tests/UsersTest.cs +++ b/src/Tests/BirdsiteLive.Twitter.Tests/UsersTest.cs @@ -28,6 +28,7 @@ namespace BirdsiteLive.ActivityPub.Tests { var user = await _tweetService.GetUserAsync("kobebryant"); Assert.AreEqual(user.Name, "Kobe Bryant"); + Assert.AreEqual(user.Acct, "kobebryant"); }