commit
d8d7aa2861
3 changed files with 65 additions and 32 deletions
|
@ -1,9 +1,11 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BirdsiteLive.Common.Settings;
|
||||
using BirdsiteLive.Statistics.Domain;
|
||||
using BirdsiteLive.Twitter.Extractors;
|
||||
using BirdsiteLive.Twitter.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Tweetinvi;
|
||||
using Tweetinvi.Models;
|
||||
using Tweetinvi.Parameters;
|
||||
|
@ -22,50 +24,69 @@ namespace BirdsiteLive.Twitter
|
|||
private readonly ITweetExtractor _tweetExtractor;
|
||||
private readonly ITwitterStatisticsHandler _statisticsHandler;
|
||||
private readonly ITwitterUserService _twitterUserService;
|
||||
private readonly ILogger<TwitterTweetsService> _logger;
|
||||
|
||||
#region Ctor
|
||||
public TwitterTweetsService(TwitterSettings settings, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService)
|
||||
public TwitterTweetsService(TwitterSettings settings, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger<TwitterTweetsService> logger)
|
||||
{
|
||||
_settings = settings;
|
||||
_tweetExtractor = tweetExtractor;
|
||||
_statisticsHandler = statisticsHandler;
|
||||
_twitterUserService = twitterUserService;
|
||||
_logger = logger;
|
||||
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
|
||||
ExceptionHandler.SwallowWebExceptions = false;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
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);
|
||||
try
|
||||
{
|
||||
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
|
||||
var tweet = Tweet.GetTweet(statusId);
|
||||
_statisticsHandler.CalledTweetApi();
|
||||
if (tweet == null) return null; //TODO: test this
|
||||
return _tweetExtractor.Extract(tweet);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error retrieving tweet {TweetId}", statusId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1)
|
||||
{
|
||||
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
|
||||
|
||||
|
||||
var user = _twitterUserService.GetUser(username);
|
||||
|
||||
var tweets = new List<ITweet>();
|
||||
if (fromTweetId == -1)
|
||||
|
||||
try
|
||||
{
|
||||
var timeline = Timeline.GetUserTimeline(user.Id, nberTweets);
|
||||
_statisticsHandler.CalledTimelineApi();
|
||||
if (timeline != null) tweets.AddRange(timeline);
|
||||
}
|
||||
else
|
||||
{
|
||||
var timelineRequestParameters = new UserTimelineParameters
|
||||
if (fromTweetId == -1)
|
||||
{
|
||||
SinceId = fromTweetId,
|
||||
MaximumNumberOfTweetsToRetrieve = nberTweets
|
||||
};
|
||||
var timeline = Timeline.GetUserTimeline(user.Id, timelineRequestParameters);
|
||||
_statisticsHandler.CalledTimelineApi();
|
||||
if (timeline != null) tweets.AddRange(timeline);
|
||||
var timeline = Timeline.GetUserTimeline(user.Id, nberTweets);
|
||||
_statisticsHandler.CalledTimelineApi();
|
||||
if (timeline != null) tweets.AddRange(timeline);
|
||||
}
|
||||
else
|
||||
{
|
||||
var timelineRequestParameters = new UserTimelineParameters
|
||||
{
|
||||
SinceId = fromTweetId,
|
||||
MaximumNumberOfTweetsToRetrieve = nberTweets
|
||||
};
|
||||
var timeline = Timeline.GetUserTimeline(user.Id, timelineRequestParameters);
|
||||
_statisticsHandler.CalledTimelineApi();
|
||||
if (timeline != null) tweets.AddRange(timeline);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error retrieving timeline from {Username}, from {TweetId}", username, fromTweetId);
|
||||
}
|
||||
|
||||
return tweets.Select(_tweetExtractor.Extract).ToArray();
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
using System.Linq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using BirdsiteLive.Common.Settings;
|
||||
using BirdsiteLive.Statistics.Domain;
|
||||
using BirdsiteLive.Twitter.Extractors;
|
||||
using BirdsiteLive.Twitter.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Tweetinvi;
|
||||
using Tweetinvi.Models;
|
||||
|
||||
namespace BirdsiteLive.Twitter
|
||||
{
|
||||
|
@ -15,24 +17,34 @@ namespace BirdsiteLive.Twitter
|
|||
public class TwitterUserService : ITwitterUserService
|
||||
{
|
||||
private readonly TwitterSettings _settings;
|
||||
private readonly ITweetExtractor _tweetExtractor;
|
||||
private readonly ITwitterStatisticsHandler _statisticsHandler;
|
||||
private readonly ILogger<TwitterUserService> _logger;
|
||||
|
||||
#region Ctor
|
||||
public TwitterUserService(TwitterSettings settings, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler)
|
||||
public TwitterUserService(TwitterSettings settings, ITwitterStatisticsHandler statisticsHandler, ILogger<TwitterUserService> logger)
|
||||
{
|
||||
_settings = settings;
|
||||
_tweetExtractor = tweetExtractor;
|
||||
_statisticsHandler = statisticsHandler;
|
||||
_logger = logger;
|
||||
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
|
||||
ExceptionHandler.SwallowWebExceptions = false;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public TwitterUser GetUser(string username)
|
||||
{
|
||||
var user = User.GetUserFromScreenName(username);
|
||||
_statisticsHandler.CalledUserApi();
|
||||
if (user == null) return null;
|
||||
IUser user;
|
||||
try
|
||||
{
|
||||
user = User.GetUserFromScreenName(username);
|
||||
_statisticsHandler.CalledUserApi();
|
||||
if (user == null) return null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error retrieving user {Username}", username);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Expand URLs
|
||||
var description = user.Description;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<Version>0.11.0</Version>
|
||||
<Version>0.11.1</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
Reference in a new issue