refactored announce
This commit is contained in:
parent
d0e4a09d3d
commit
34fe552448
3 changed files with 4 additions and 80 deletions
|
@ -36,16 +36,6 @@ namespace BirdsiteLive.Twitter
|
||||||
}
|
}
|
||||||
#endregion
|
#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)
|
public TwitterUser GetUser(string username)
|
||||||
{
|
{
|
||||||
if (!_userCache.TryGetValue(username, out TwitterUser user))
|
if (!_userCache.TryGetValue(username, out TwitterUser user))
|
||||||
|
|
|
@ -9,6 +9,7 @@ using BirdsiteLive.Statistics.Domain;
|
||||||
using BirdsiteLive.Twitter.Models;
|
using BirdsiteLive.Twitter.Models;
|
||||||
using BirdsiteLive.Twitter.Tools;
|
using BirdsiteLive.Twitter.Tools;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace BirdsiteLive.Twitter
|
namespace BirdsiteLive.Twitter
|
||||||
{
|
{
|
||||||
|
@ -129,7 +130,9 @@ namespace BirdsiteLive.Twitter
|
||||||
if (first.GetProperty("type").GetString() == "retweeted")
|
if (first.GetProperty("type").GetString() == "retweeted")
|
||||||
{
|
{
|
||||||
IsRetweet = true;
|
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 statusId = Int64.Parse(first.GetProperty("id").GetString());
|
||||||
var extracted = GetTweet(statusId);
|
var extracted = GetTweet(statusId);
|
||||||
extracted.IsRetweet = true;
|
extracted.IsRetweet = true;
|
||||||
|
|
|
@ -14,7 +14,6 @@ namespace BirdsiteLive.Twitter
|
||||||
public interface ITwitterUserService
|
public interface ITwitterUserService
|
||||||
{
|
{
|
||||||
TwitterUser GetUser(string username);
|
TwitterUser GetUser(string username);
|
||||||
TwitterUser GetUser(long id);
|
|
||||||
bool IsUserApiRateLimited();
|
bool IsUserApiRateLimited();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,10 +33,6 @@ namespace BirdsiteLive.Twitter
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public TwitterUser GetUser(long id)
|
|
||||||
{
|
|
||||||
return GetUserAsync(id).Result;
|
|
||||||
}
|
|
||||||
public TwitterUser GetUser(string username)
|
public TwitterUser GetUser(string username)
|
||||||
{
|
{
|
||||||
return GetUserAsync(username).Result;
|
return GetUserAsync(username).Result;
|
||||||
|
@ -113,70 +108,6 @@ namespace BirdsiteLive.Twitter
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TwitterUser> 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()
|
public bool IsUserApiRateLimited()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue