80 lines
No EOL
2.9 KiB
C#
80 lines
No EOL
2.9 KiB
C#
using System;
|
|
using System.Linq;
|
|
using BirdsiteLive.Common.Settings;
|
|
using BirdsiteLive.Statistics.Domain;
|
|
using BirdsiteLive.Twitter.Models;
|
|
using BirdsiteLive.Twitter.Tools;
|
|
using Microsoft.Extensions.Logging;
|
|
using Tweetinvi;
|
|
using Tweetinvi.Models;
|
|
|
|
namespace BirdsiteLive.Twitter
|
|
{
|
|
public interface ITwitterUserService
|
|
{
|
|
TwitterUser GetUser(string username);
|
|
}
|
|
|
|
public class TwitterUserService : ITwitterUserService
|
|
{
|
|
private readonly ITwitterAuthenticationInitializer _twitterAuthenticationInitializer;
|
|
private readonly ITwitterStatisticsHandler _statisticsHandler;
|
|
private readonly ILogger<TwitterUserService> _logger;
|
|
|
|
#region Ctor
|
|
public TwitterUserService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITwitterStatisticsHandler statisticsHandler, ILogger<TwitterUserService> logger)
|
|
{
|
|
_twitterAuthenticationInitializer = twitterAuthenticationInitializer;
|
|
_statisticsHandler = statisticsHandler;
|
|
_logger = logger;
|
|
}
|
|
#endregion
|
|
|
|
public TwitterUser GetUser(string username)
|
|
{
|
|
//Check if API is saturated
|
|
var currentCalls = _statisticsHandler.GetCurrentUserCalls();
|
|
var maxCalls = _statisticsHandler.GetStatistics().UserCallsMax;
|
|
if (currentCalls > maxCalls) return null;
|
|
|
|
//Proceed to account retrieval
|
|
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
|
|
ExceptionHandler.SwallowWebExceptions = false;
|
|
|
|
IUser user;
|
|
try
|
|
{
|
|
user = User.GetUserFromScreenName(username);
|
|
_statisticsHandler.CalledUserApi();
|
|
if (user == null)
|
|
{
|
|
_logger.LogWarning("User {username} not found", username);
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "Error retrieving user {Username}", username);
|
|
return null;
|
|
}
|
|
|
|
// Expand URLs
|
|
var description = user.Description;
|
|
foreach (var descriptionUrl in user.Entities?.Description?.Urls?.OrderByDescending(x => x.URL.Length))
|
|
description = description.Replace(descriptionUrl.URL, descriptionUrl.ExpandedURL);
|
|
|
|
return new TwitterUser
|
|
{
|
|
Id = user.Id,
|
|
Acct = username,
|
|
Name = user.Name,
|
|
Description = description,
|
|
Url = $"https://twitter.com/{username}",
|
|
ProfileImageUrl = user.ProfileImageUrlFullSize.Replace("http://", "https://"),
|
|
ProfileBackgroundImageUrl = user.ProfileBackgroundImageUrlHttps,
|
|
ProfileBannerURL = user.ProfileBannerURL,
|
|
Protected = user.Protected
|
|
};
|
|
}
|
|
}
|
|
} |