diff --git a/src/BirdsiteLive.Twitter/CachedTwitterService.cs b/src/BirdsiteLive.Twitter/CachedTwitterService.cs index 79645dd..c49104a 100644 --- a/src/BirdsiteLive.Twitter/CachedTwitterService.cs +++ b/src/BirdsiteLive.Twitter/CachedTwitterService.cs @@ -36,16 +36,6 @@ namespace BirdsiteLive.Twitter } #endregion - public TwitterUser GetUser(long id) - { - if (!_userCache.TryGetValue(id, out TwitterUser user)) - { - user = _twitterService.GetUser(id); - if(user != null) _userCache.Set(id, user, _cacheEntryOptions); - } - - return user; - } public TwitterUser GetUser(string username) { if (!_userCache.TryGetValue(username, out TwitterUser user)) diff --git a/src/BirdsiteLive.Twitter/TwitterTweetsService.cs b/src/BirdsiteLive.Twitter/TwitterTweetsService.cs index 583f5d9..b685282 100644 --- a/src/BirdsiteLive.Twitter/TwitterTweetsService.cs +++ b/src/BirdsiteLive.Twitter/TwitterTweetsService.cs @@ -9,6 +9,7 @@ using BirdsiteLive.Statistics.Domain; using BirdsiteLive.Twitter.Models; using BirdsiteLive.Twitter.Tools; using Microsoft.Extensions.Logging; +using System.Text.RegularExpressions; namespace BirdsiteLive.Twitter { @@ -129,7 +130,9 @@ namespace BirdsiteLive.Twitter if (first.GetProperty("type").GetString() == "retweeted") { IsRetweet = true; - var originalAuthor = _twitterUserService.GetUser(Int64.Parse(tweet.GetProperty("author_id").GetString())); + var regex = new Regex("RT @([A-Za-z0-9_])+:"); + var match = regex.Match(tweet.GetProperty("text").GetString()); + var originalAuthor = _twitterUserService.GetUser(match.Groups[1].Value); var statusId = Int64.Parse(first.GetProperty("id").GetString()); var extracted = GetTweet(statusId); extracted.IsRetweet = true; diff --git a/src/BirdsiteLive.Twitter/TwitterUserService.cs b/src/BirdsiteLive.Twitter/TwitterUserService.cs index f711e01..4f7e98a 100644 --- a/src/BirdsiteLive.Twitter/TwitterUserService.cs +++ b/src/BirdsiteLive.Twitter/TwitterUserService.cs @@ -14,7 +14,6 @@ namespace BirdsiteLive.Twitter public interface ITwitterUserService { TwitterUser GetUser(string username); - TwitterUser GetUser(long id); bool IsUserApiRateLimited(); } @@ -34,10 +33,6 @@ namespace BirdsiteLive.Twitter } #endregion - public TwitterUser GetUser(long id) - { - return GetUserAsync(id).Result; - } public TwitterUser GetUser(string username) { return GetUserAsync(username).Result; @@ -113,70 +108,6 @@ namespace BirdsiteLive.Twitter }; } - public async Task GetUserAsync(long id) - { - //Check if API is saturated - if (IsUserApiRateLimited()) throw new RateLimitExceededException(); - - //Proceed to account retrieval - await _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized(); - - JsonDocument res; - try - { - using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://api.twitter.com/2/users/"+ id + "?user.fields=name,username,protected,profile_image_url,url,description")) - { - request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _twitterAuthenticationInitializer.Token); - - var httpResponse = await _httpClient.SendAsync(request); - httpResponse.EnsureSuccessStatusCode(); - - var c = await httpResponse.Content.ReadAsStringAsync(); - res = JsonDocument.Parse(c); - } - } - catch (HttpRequestException e) - { - throw; - //if (e.TwitterExceptionInfos.Any(x => x.Message.ToLowerInvariant().Contains("User has been suspended".ToLowerInvariant()))) - //{ - // throw new UserHasBeenSuspendedException(); - //} - //else if (e.TwitterExceptionInfos.Any(x => x.Message.ToLowerInvariant().Contains("User not found".ToLowerInvariant()))) - //{ - // throw new UserNotFoundException(); - //} - //else if (e.TwitterExceptionInfos.Any(x => x.Message.ToLowerInvariant().Contains("Rate limit exceeded".ToLowerInvariant()))) - //{ - // throw new RateLimitExceededException(); - //} - //else - //{ - // throw; - //} - } - catch (Exception e) - { - _logger.LogError(e, "Error retrieving user id {id}", id); - throw; - } - finally - { - _statisticsHandler.CalledUserApi(); - } - return new TwitterUser - { - Id = id, - Acct = res.RootElement.GetProperty("data").GetProperty("username").GetString(), - Name = res.RootElement.GetProperty("data").GetProperty("name").GetString(), - Description = res.RootElement.GetProperty("data").GetProperty("description").GetString(), - Url = res.RootElement.GetProperty("data").GetProperty("url").GetString(), - ProfileImageUrl = res.RootElement.GetProperty("data").GetProperty("profile_image_url").GetString(), - ProfileBackgroundImageUrl = res.RootElement.GetProperty("data").GetProperty("profile_image_url").GetString(), //for now - ProfileBannerURL = res.RootElement.GetProperty("data").GetProperty("profile_image_url").GetString(), //for now - Protected = res.RootElement.GetProperty("data").GetProperty("protected").GetBoolean(), - }; - } public bool IsUserApiRateLimited() {